导入:
后台日常开发中查询最多,故每次的返回结果就会碰到,有resultMap和resultType,日常有四种情形:
//实体类Dept与表字段不匹配
Dept dept=deptService.selectDept(String id)--><select id="selectDept" resultMap="DeptResult">
//实体类Dept与表字段不匹配
List<Dept> lists=deptService.selectDept(String id)--><select id="selectDept" resultMap="DeptResult">
//实体类Role与表字段匹配
List<Role> lists=roleService.selectRole(String id)--><select id="selectRole" resultType="com....Role">
//实体类Role与表字段匹配
List<Map> lists=roleService.selectRole(String id)--><select id="selectRole" resultType="java.util.Map" >
定义:
resultMap: 实体类字段和表字段不匹配
resultType: 实体类字段和表字段匹配
举例:
resultMap使用:
//实体类
@Data
@AllArgsConstructor
@ToString
public class User implements Serializable{
private String userId;
private String userName;
}
//impl调用
List<User> userList=userService.selectUserList(id)
//mapper.xml
<resultMap type="com.....User" id="UserResult">
<result property="userId" column="st_user_id" />
<result property="userName" column="st_user_name" />
</resultMap>
<select id="selectUserList" resultMap="UserResult">
select st_user_id, st_user_name
from user
where 1=1
</select>
resultType使用:
//实体类
@Data
@AllArgsConstructor
@ToString
public class User implements Serializable{
private String userId;
private String userName;
}
//impl调用
List<User> userList=userService.selectUserList(id)
//mapper.xml
<select id="selectUserList" resultType="com....User">
select st_user_id userId, st_user_name userName
from user
where 1=1
</select>
本文只是笔记
本文介绍了MyBatis中resultMap和resultType的使用场景和区别。resultMap用于处理实体类字段与表字段不匹配的情况,通过配置映射关系实现数据转换;而resultType则在实体类字段与表字段匹配时直接将查询结果映射到实体类。文中通过实例展示了如何在mapper.xml中配置resultMap和resultType,并提供了对应的实体类和查询调用。
810

被折叠的 条评论
为什么被折叠?



