resultMap和resultType区别

本文介绍了MyBatis中resultMap和resultType的使用场景和区别。resultMap用于处理实体类字段与表字段不匹配的情况,通过配置映射关系实现数据转换;而resultType则在实体类字段与表字段匹配时直接将查询结果映射到实体类。文中通过实例展示了如何在mapper.xml中配置resultMap和resultType,并提供了对应的实体类和查询调用。

导入:
后台日常开发中查询最多,故每次的返回结果就会碰到,有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>

本文只是笔记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值