一、一对多映射关系

  POJO 中的属性可能会是一个对象的集合,当我们查询一方的信息时,还想要知道多方的信息,就需要用到一对多查询。(如:查询部门信息,同时查询部门中的员工信息)
  使用 collection 标签来定义对象的封装规则。
  两个 JavaBean 类:
  Employee 类
public class Employee {
    private Integer id;
    private String lastName;
    private String gender;
    private String email;
    private Department department;
    //省略了 get/set 方法和构造器
}

 

  Department 类
public class Department {
    private Integer id;
    private String deptName;
    private List<Employee> emps;
    //省略了 get/set 方法和构造器
}

 

  当我们想要获取部门信息同时还要获取其部门下的员工信息,这是一个一对多的关系,我们就可以使用下面的几种方式来实现。
 

二、使用 Collection 标签查询

  在 Department 接口中声明方法:

//根据id查询部门及其员工信息
public Department getDeptByIdPlus(Integer id);

 

  在对应的 mapper.xml 中配置:

    <!--
        collection 嵌套结果集的方法,定义关联的集合类型元素的封装规则

        嵌套结果集的方式,使用 collection 标签定义关联的集合类型的属性封装规则
    -->
    <resultMap id="MyDept" type="com.njf.mybatis.bean.Department">
        <id column="did" property="id" />
        <result column="dept_name" property="deptName"/>
        <!--
            collection 定义关联集合类型的属性的封装规则
            ofType:指定集合里面元素的类型
        -->
        <collection property="emps" ofType="com.njf.mybatis.bean.Employee">
            <id column="eid" property="id"/>
            <result column="last_name" property="lastName" />
            <result column="email" property="email" />
            <result column="gender" property="gender" />
        </collection>
    </resultMap>
<!-- public Department getDeptByIdPlus(Integer id); --> <select id="getDeptByIdPlus" resultMap="MyDept"> SELECT d.id did,d.`dept_name` dept_name, e.id eid, e.`last_name` last_name, e.`email` email, e.`gender` gender FROM tbl_department d LEFT JOIN tbl_employee e ON d.`id` = e.`dept_id` WHERE d.`id` = #{id}; </select>

 

  说明:

collection 嵌套结果集的方法,定义关联的集合类型元素的封装规则
property 对应的属性值
ofType:指定集合里面元素的类型

  

三、使用 Collection 实现分步查询

  实际的开发中,对于每个实体类都应该有具体的增删改查方法,也就是DAO层, 因此
  对于查询部门信息并且将对应的所有的员工信息也查询出来的需求,就可以通过分步的方式完成查询。
    ① 先通过部门的 id 查询部门信息
    ② 再通过部门的 id 作为员工的 外键查询对应的部门信息
  在 EmployeeMapper 接口中声明方法:
//根据部门id查询员工
public List<Employee> getEmpsByDeptId(Integer deptId);

 

  在对应的 mapper.xml中进行配置:
    <!--
        public List<Employee> getEmpsByDeptId(Integer id);
     -->
    <select id="getEmpsByDeptId" resultType="Employee">
        select * from tbl_employee where dept_id=#{deptId}
    </select>

 

 
  在 DepartmentMapper 接口中声明方法:
public Department getDeptByIdStep(Integer id);

 

  在对应的mapper.xml 中进行配置:
 
    <!--
        collection 嵌套结果集的方法,定义关联的集合类型元素的封装规则
        collection 分段查询
        延迟加载
    -->
    <resultMap id="MyDeptStep" type="com.njf.mybatis.bean.Department">
        <id column="id" property="id" />
        <result column="dept_name" property="deptName"/>
        <!--
            collection 定义关联集合类型的属性的封装规则
            ofType:指定集合里面元素的类型
        -->
        <collection property="emps"
                    select="com.njf.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
                    column="id">

        </collection>
    </resultMap>
    <!--
        public Department getDeptByIdStep(Integer id);
     -->
    <select id="getDeptByIdStep" resultMap="MyDeptStep">
        select id, dept_name deptName from tbl_department where id = #{id}
    </select>

 

四、使用 Collection 延迟加载

  设置同 association 的延迟加载

 

posted on 2021-09-07 16:32  格物致知_Tony  阅读(131)  评论(0编辑  收藏  举报

目录导航