【7】Mybatis多对一、一对多处理

11、多对一处理

  • 多个员工,对应一个部门
  • 对于员工而言,关联:多个员工,关联一个部门【多对一】
  • 对于部门而言,集合,一个部门,有很多员工【一对多】

测试环境搭建(重要,相当于对之前的总结!!!)

  1. 导入lombok

  2. 新建实体类Employee、Department

    import lombok.*;
    
    @Data
    public class Department {
          
          
        private int id;
        private String dep_name;
        private String dep_location;
    }
    
    
    import lombok.*;
    
    @Data
    public class Employee {
          
          
        private int id;
        private String name;
        private int age;
    /*    private int dept_id;*/
        private Department department;
    }
    
  3. 创建dao层接口EmployeeMapper、DepartmentMapper接口,写方法

  4. 在resources资源包中创建多级目录包(用.进行分割),和java包中的路径相同,新建EmployeeMapper.xml和DepartmentMapper.xml,进行配置

  5. 在config-mapper.xml核心配置文件绑定,注意mapper标签下里的resource和class的区别

  6. 编写测试类

按照嵌套子查询处理

  • 复杂的属性我们需要单独处理:
  • 对象association,集合collection。
  • 其中javaType="",指定属性的类型
  • 集合中的泛型信息,我们使用ofType获取
<mapper namespace="com.kuber.dao.EmployeeMapper">
    
    <select id="getAllEmployee" resultMap="EmployeeDepartment">
        select * from employee
    </select>
    <resultMap id="EmployeeDepartment" type="Employee">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <!--<result property="dept_id" column="dept_id"/>-->
        <association property="department" column="dept_id" javaType="Department" select="getAllDepartment"/>
    </resultMap>
    
    <select id="getAllDepartment" resultType="department">
        select * from department where id = #{dept_id}
        <!--这里传的值id = #{xxx}可以随便定义,传的都是column="dept_id"的值,相当于子查询-->
    </select>
</mapper>

在这里插入图片描述

按照条件连接查询处理

    <select id="getAllEmployee2" resultMap="EmployeeDepartment2">
        select e.id eid,name ename,age,d.id did,d.dep_name dname,d.dep_location dlocation
        from Employee e,Department d
        where e.dept_id = d.id
    </select>
    
    <resultMap id="EmployeeDepartment2" type="Employee">
        <result property="id" column="eid"/>
        <result property="name" column="ename"/>
        <result property="age" column="age"/>
        <association property="department" javaType="Department">
            <result property="id" column="did"/>
            <result property="dep_name" column="dname"/>
            <result property="dep_location" column="dlocation"/>
        </association>
    </resultMap>

在这里插入图片描述

12、一对多处理

搭建环境

同一对多差不多,不过实体类发生了变化

import lombok.Data;

import java.util.List;

@Data
public class Department {
    
    
    private int id;
    private String dep_name;
    private String dep_location;
    private List<Employee> employee;
}

import lombok.Data;

@Data
public class Employee {
    
    
    private int id;
    private String name;
    private int age;
    private int dept_id;
}

按照嵌套子查询处理

    <select id="getAllEmployeeByDeptid2" resultMap="DepartmentEmployee2">
        select * from Department where id = #{dept_id}
    </select>
    <resultMap id="DepartmentEmployee2" type="Department">
        <result property="id" column="id"/>
        <result property="dep_name" column="dep_name"/>
        <result property="dep_location" column="dep_location"/>
        <collection property="employee" javaType="ArrayList" ofType="Employee" column="id" select="getEmployeeByDeptid"/>
    </resultMap>
    <select id="getEmployeeByDeptid" resultType="Employee">
        select * from employee where dept_id = #{id}
    </select>

按照嵌套连接查询处理

	<select id="getAllEmployeeByDeptid" resultMap="DepartmentEmployee">
        select d.id did,d.dep_name dname,d.dep_location location,
                e.id eid,e.name ename,e.age age,e.dept_id edepid
        from department d left join employee e on d.id = e.dept_id
        where d.id = #{id}
    </select>

    <resultMap id="DepartmentEmployee" type="Department">
        <result property="id" column="did"/>
        <result property="dep_name" column="dname"/>
        <result property="dep_location" column="location"/>
        <collection property="employee" ofType="Employee">
            <result property="id" column="eid"/>
            <result property="name" column="ename"/>
            <result property="age" column="age"/>
            <result property="dept_id" column="edepid"/>
        </collection>
    </resultMap>

小结

  1. 关联-association【多对一】
  2. 集合-collection【一对多】
  3. javaType和ofType
    1. javaType用来指定实体类中属性的类型
    2. ofType用来指定映射到实体类中的属性为List或者集合中的实体类(pojo)类型,即泛型中的约束类型

注意点

  • 保证sql的可读性,尽量保证通俗易懂

  • 注意一对多和多对一中,属性名和字段的问题

  • 如果问题不好排查错误,可以使用日志工厂,可以用Log4j,也可用标准日志工厂STDOUT_LOGGING

  • 连接查询可读性高,方便使用

  • 避免慢SQL,需要经验积累

    面试高频

    • Mysql引擎
    • Innodb底层原理
    • 索引
    • 索引优化

猜你喜欢

转载自blog.csdn.net/weixin_43215322/article/details/109565280