1. resultType:表示把数据封装成什么类型
比如:
<select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee where id = #{id}
</select>
表示把查询出来的数据封装成Employee实体类型,返回到getEmpById方法
在tbl_employee表中字段
Employee实体中的属性
可见数据库中last_name和实体中lastName并不对应,所以查询出的数据中lastName为null
这时就要开启驼峰命名法:
扫描二维码关注公众号,回复:
158304 查看本文章
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
开启驼峰命名法后,数据库中last_name和实体中lastName就可以对应上了。
2.resultMap:自定义结果集映射规则(自定义某个javaBean的封装规则)
<!-- 自定义某个javaBean的封装规则
type:自定义规则的java类型
id:唯一id方便引用-->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MySimpleEmp">
<!-- 指定主键列的封装规则 ,因为已经定义了封装规则,就可以把驼峰命名法关掉
id定义主键,底层会优化
column:指定哪一列
property:指定对应的JavaBean属性
-->
<id column="id" property="id"/>
<!-- 定义普通列封装规则 -->
<result column="last_name" property="lastName"/>
<!-- 其他不指定的列会自动封装:我们只要写resultMap就把全部的映射规则都写上 -->
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</resultMap>
<!-- resultMap:自定义结果集映射规则; -->
<!-- public Employee getEmp·ById(Integer id); -->
<select id="getEmpById" resultMap="MySimpleEmp">
select * from tbl_employee where id=#{id}
</select>
因为我们已经在resultMap中定义了封装规则,所以就可以把驼峰命名法关闭了。