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>
相关推荐
架构文摘JGWZ30 分钟前
Java 23 的12 个新特性!!
java·开发语言·学习
小齿轮lsl35 分钟前
PFC理论基础与Matlab仿真模型学习笔记(1)--PFC电路概述
笔记·学习·matlab
拾光师1 小时前
spring获取当前request
java·后端·spring
aPurpleBerry1 小时前
neo4j安装启动教程+对应的jdk配置
java·neo4j
天玑y1 小时前
算法设计与分析(背包问题
c++·经验分享·笔记·学习·算法·leetcode·蓝桥杯
我是苏苏1 小时前
Web开发:ABP框架2——入门级别的增删改查Demo
java·开发语言
xujinwei_gingko2 小时前
Spring IOC容器Bean对象管理-Java Config方式
java·spring
2301_789985942 小时前
Java语言程序设计基础篇_编程练习题*18.29(某个目录下的文件数目)
java·开发语言·学习
IT学长编程2 小时前
计算机毕业设计 教师科研信息管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·毕业设计·springboot·毕业论文·计算机毕业设计选题·计算机毕业设计开题报告·教师科研管理系统
m0_571957582 小时前
Java | Leetcode Java题解之第406题根据身高重建队列
java·leetcode·题解