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>
相关推荐
llwszx2 小时前
深入理解Java锁原理(一):偏向锁的设计原理与性能优化
java·spring··偏向锁
云泽野3 小时前
【Java|集合类】list遍历的6种方式
java·python·list
二进制person3 小时前
Java SE--方法的使用
java·开发语言·算法
IMPYLH4 小时前
Python 的内置函数 reversed
笔记·python
小阳拱白菜4 小时前
java异常学习
java
FrankYoou6 小时前
Jenkins 与 GitLab CI/CD 的核心对比
java·docker
麦兜*6 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
KK溜了溜了6 小时前
JAVA-springboot 整合Redis
java·spring boot·redis
天河归来6 小时前
使用idea创建springboot单体项目
java·spring boot·intellij-idea
weixin_478689767 小时前
十大排序算法汇总
java·算法·排序算法