resultMap
:针对一些比较复杂的封装场景,在resultMap里在去手动定义封装
resultType
:主要针对从数据库提取出来的相应数据,也就是说直接从数据库里读取出来然后保存到实体类里
如:
<resultMap id="getListAddiress" type="实体类">
<collection property="t_sys_roleList" ofType="t_sys_role">
<id property="id" column="id"></id>
<result property="CODE" column="CODE"></result>
<result property="roleName" column="roleName"></result>
<result property="createdUserId" column="createdUserId"></result>
<result property="createdTime" column="createdTime"></result>
<result property="updatedUserId" column="updatedUserId"></result>
<result property="updatedTime" column="updatedTime"></result>
</collection>
</resultMap>
<select id="getUserListReqsulitMqpAddressesBuUserID" parameterType="SysUser" resultMap="要和resultMap里的id一样">
select u.* , t.id , t.* from t_sys_user u,t_sys_role t where u.roleId=t.id and u.id=#{userid}//查询语句
</select>
result
里的property
里填的是实体类中定义的属性名
result
里的column
里填的是查询出来的表里的字段名
但是有一个缺点就是全部需要手动去封装,适合一些比较复杂的查询
如果不想手动去封装的话可以在你的mybatis-config.xml
文件下进行修改(也就是根目录下的配置文件)
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>
里多加一句:
<setting name="autoMappingBehavior" value="FULL">
最后的结果就是
<settings>
<setting name="logImpl" value="LOG4J" />
<setting name="autoMappingBehavior" value="FULL">
</settings>
resultType
:属性直接指定结果类型,依靠自动映射实现对实体类等复杂数据类型的封装,适用于比较简单,直接的数据封装场景
如:
<select id="getUserListReqsulitMqpAddressesBuUserID" parameterType="SysUser" resultType="返回值类型(全线类名)">
select * from t_sys_user
</select>
resultType
和resultType
的区别:
使用select
元素映射查询语句时,对于返回结果的定义可以使用resultType属性,也可以使用
resultMap
属性(适合处理一些比较复杂场景,需要手动去定义)
resultType
属性直接指定结果类型,依靠自动映射实现对实体类等复杂数据类型的封装,适用于比较简单(主要就是针对数据库里读取出来的数据然后放到实体类里)
直接的数据封装场景,而resultMap
属性是对外部resultMap
定义的引用,可以自由控制结果映射
规则和封装范围,能够处理结果集字段名与实体类属性名不一致,或需要对连接查询结果使用
嵌套映射等较为复杂的问题
注意:使用resultType属性或resultMap属性封装查询结果本质上是一样的,均基于Map数据结构
但是,在select
元素中使用时需要注意,二者不能同时使用