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


    场景重现


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

    实验使用的类和对象

    mapper.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>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    pojo:

    @Data  
    @AllArgsConstructor  
    @NoArgsConstructor  
    public class Emp {  
        private Integer eid;  
        private String empName;  
        private Integer age;  
        private String sex;  
        private String email;  
        private Dept dept;  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    @Data  
    @AllArgsConstructor  
    @NoArgsConstructor  
    public class Dept {  
        private Integer did;  
        private String deptName;  
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

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

    <resultMap id="empAndDeptResultMapOne" type="Emp">  
          
        <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"/>  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    使用类.属性解决


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

    <resultMap id="empAndDeptResultMapOne" type="Emp">  
          
        <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"/>  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    使用 解决

    <resultMap id="empAndDeptResultMapTwo" type="Emp">  
          
        <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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    分布查询 解决


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

    Emp getEmpAndDeptByStepOne(Integer eid);
    
    • 1
    <select id="getEmpAndDeptByStepOne">  
        select *  
        from t_emp             
        left join t_dept on t_emp.did = t_dept.did    
        where t_emp.eid = #{eid}
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    Dept getEmpAndDeptByStepTwo(Integer did);
    
    • 1
    <select id="getEmpAndDeptByStepTwo" resultType="com.zxb.mybatis.pojo.Dept">  
        select * from t_dept where did = #{did}  
    select>
    
    • 1
    • 2
    • 3

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

    <resultMap id="empAndDeptByStepResultMap" type="Emp">  
          
        <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    Leetcode 73 矩阵置0
    瞎琢磨i之 原生sql动态查询实现
    WebRTC系列-网络传输之本地scoket端口
    Android单编模块报FAILED: ninja: unknown target ‘MODULES-IN-vendor错误解决
    javase----内部类
    graphhopper-ios 编译过程详解
    72:第六章:开发文章服务:5:开发【发表文章,接口】;
    PADS出GERBER时 焊盘丢失、焊盘变形问题
    VUE iview DatePicker在Safari浏览器时间转换异常的解决方法
    1.4_17 Axure RP 9 for mac 高保真原型图 - 案例16 【动态面板-滚动条6】手动制作滚动条
  • 原文地址:https://blog.csdn.net/qq_66250368/article/details/138186596