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>
相关推荐
HuashuiMu花水木8 分钟前
PyTorch笔记1----------Tensor(张量):基本概念、创建、属性、算数运算
人工智能·pytorch·笔记
木头没有瓜1 小时前
idea离线安装插件
java·ide·intellij-idea
一勺菠萝丶1 小时前
Spring Boot + MyBatis/MyBatis Plus:XML中循环处理List参数的终极指南
xml·spring boot·mybatis
llwszx1 小时前
Spring中DelayQueue深度解析:从原理到实战(附结构图解析)
java·后端·spring·delayqueue·延迟任务
述雾学java1 小时前
Spring Cloud Feign 整合 Sentinel 实现服务降级与熔断保护
java·spring cloud·sentinel
保持学习ing1 小时前
苍穹外卖day3--公共字段填充+新增菜品
java·阿里云·实战·springboot·前后端·外卖项目·阿里云文件存储
77qqqiqi2 小时前
正则表达式
java·后端·正则表达式
厦门德仔2 小时前
【WPF】WPF(样式)
android·java·wpf
大春儿的试验田2 小时前
高并发收藏功能设计:Redis异步同步与定时补偿机制详解
java·数据库·redis·学习·缓存
Gappsong8742 小时前
【Linux学习】Linux安装并配置Redis
java·linux·运维·网络安全