• 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
  • 相关阅读:
    巧用 background-clip 实现超强的文字动效
    在数据结构与算法中 传值方式(C语言)
    定制化、高可用前台样式处理方案——tailwindcss
    蓝桥等考Python组别十三级008
    ECMAScript新特性
    别再当大冤种了,揭开3D建模报班6个常见套路
    如何为python换源
    Arduino开发实例-DIY酒精浓度检测计
    20230908_python练习_selenium模块爬取网页小说练习
    高效技巧揭秘:Java轻松批量插入或删除Excel行列操作
  • 原文地址:https://blog.csdn.net/qq_66250368/article/details/138186596