MyBatis笔记——多对一映射问题解决

场景重现


当想要查询一个部门下的所有员工时,多个员工 对应一个部门

实验使用的类和对象

mapper.xml

xml 复制代码
<select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo">  
    select * from t_emp 
    left join t_dept on t_emp.did = t_dept.did    
    where t_emp.eid = #{eid}
</select>

pojo:

java 复制代码
@Data  
@AllArgsConstructor  
@NoArgsConstructor  
public class Emp {  
    private Integer eid;  
    private String empName;  
    private Integer age;  
    private String sex;  
    private String email;  
    private Dept dept;  
}
java 复制代码
@Data  
@AllArgsConstructor  
@NoArgsConstructor  
public class Dept {  
    private Integer did;  
    private String deptName;  
}

<result> 中直接使用 类.属性 来代表嵌套的pojo

xml 复制代码
<resultMap id="empAndDeptResultMapOne" type="Emp">  
    <!--id设置主键属性,result设置普通属性-->  
    <id property="eid" column="eid"/>  
    <result property="empName" column="emp_name"/>  
    <result property="age" column="age"/>  
    <result property="sex" column="sex"/>  
    <result property="email" column="email"/>  
    <result property="dept.did" column="did"/>  
    <result property="dept.deptName" column="dept_name"/>  
</resultMap

使用类.属性解决


<result> 中直接使用 类.属性 来代表嵌套的pojo

xml 复制代码
<resultMap id="empAndDeptResultMapOne" type="Emp">  
    <!--id设置主键属性,result设置普通属性-->  
    <id property="eid" column="eid"/>  
    <result property="empName" column="emp_name"/>  
    <result property="age" column="age"/>  
    <result property="sex" column="sex"/>  
    <result property="email" column="email"/>  
    <result property="dept.did" column="did"/>  
    <result property="dept.deptName" column="dept_name"/>  
</resultMap

使用<association> 解决

xml 复制代码
<resultMap id="empAndDeptResultMapTwo" type="Emp">  
    <!--id设置主键属性,result设置普通属性-->  
    <id property="eid" column="eid"/>  
    <result property="empName" column="emp_name"/>  
    <result property="age" column="age"/>  
    <result property="sex" column="sex"/>  
    <result property="email" column="email"/>  
    <association property="dept" javaType="dept">  
        <id property="did" column="did"/>  
        <result property="deptName" column="dept_name"/>  
    </association>  
</resultMap>

分布查询 解决


1. 分别在两个表对应的mapper,使用能关联的字段进行查询

java 复制代码
Emp getEmpAndDeptByStepOne(Integer eid);
xml 复制代码
<select id="getEmpAndDeptByStepOne">  
    select *  
    from t_emp             
    left join t_dept on t_emp.did = t_dept.did    
    where t_emp.eid = #{eid}
</select>
java 复制代码
Dept getEmpAndDeptByStepTwo(Integer did);
xml 复制代码
<select id="getEmpAndDeptByStepTwo" resultType="com.zxb.mybatis.pojo.Dept">  
    select * from t_dept where did = #{did}  
</select>

2. 在主表的映射文件中,添加<association>字段,并用该字段查询子表

xml 复制代码
<resultMap id="empAndDeptByStepResultMap" type="Emp">  
    <!--id设置主键属性,result设置普通属性-->  
    <id property="eid" column="eid"/>  
    <result property="empName" column="emp_name"/>  
    <result property="age" column="age"/>  
    <result property="sex" column="sex"/>  
    <result property="email" column="email"/>  
    <association property="dept" select="com.zxb.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="did"/>  
</resultMap>
xml 复制代码
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">  
    select *  
    from t_emp             
    left join t_dept on t_emp.did = t_dept.did    
    where t_emp.eid = #{eid}
</select>
相关推荐
UpUpUp……4 小时前
HTML简单语法标签(后续实操:云备份项目)
笔记·html
小彭律师4 小时前
门禁人脸识别系统详细技术文档
笔记·python
是孑然呀5 小时前
【小记】word批量生成准考证
笔记·学习·excel
熊大如如7 小时前
Java 反射
java·开发语言
猿来入此小猿7 小时前
基于SSM实现的健身房系统功能实现十六
java·毕业设计·ssm·毕业源码·免费学习·猿来入此·健身平台
goTsHgo8 小时前
Spring Boot 自动装配原理详解
java·spring boot
卑微的Coder8 小时前
JMeter同步定时器 模拟多用户并发访问场景
java·jmeter·压力测试
pjx9878 小时前
微服务的“导航系统”:使用Spring Cloud Eureka实现服务注册与发现
java·spring cloud·微服务·eureka
多多*9 小时前
算法竞赛相关 Java 二分模版
java·开发语言·数据结构·数据库·sql·算法·oracle
爱喝酸奶的桃酥9 小时前
MYSQL数据库集群高可用和数据监控平台
java·数据库·mysql