• Mybatis3.1-[tp_34-35]-_映射文件_select_resultMap关联查询_collection定义关联集合封装规则_collection分步查询_延迟加载


    笔记要点
    出错分析与总结
    工程组织


    1.定义接口

    interface DepartmentMapper
    package com.dao;
    
    import com.bean.Department;
    
    public interface DepartmentMapper {
     
    
        public Department getDeptByIdStep(Integer id);  //使用Collection,执行分步查询
    
    
    }
    interface EmployeeMapperPlus
    package com.dao;
    import com.bean.*;
    
    import java.util.List;
    
    public interface EmployeeMapperPlus {
    
    
        public List<Employee> getEmpsByDeptId(Integer deptId);  //按照部门的id,返回employee一个列表
    }


    2.定义XML映射文件

    DepartmentMapper.xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.dao.DepartmentMapper">
        <!--public Department getDeptById(Integer id);-->
        <select id="getDeptById" resultType="com.bean.Department">
            select id,dept_name departmentName from tbl_dept
            where id=#{id}
        </select>
    <!--==========================================================================================-->
        <!--
        public class Department {
        private Integer id;
        private String departmentName;
        private List<Employee> emps;
        }
        JavaBean中:   did  dept_name   ||   eid  last_name   email          gender
     -->
        <!--public Department getDeptByIdPlus(Integer id);-->
        <!--进行collection的联合查询/ 分步查询和延迟查询 -->
        <resultMap id="MyDept" type="com.bean.Department">
            <id column="did" property="id"/>
            <result column="dept_name" property="departmentName"/>
    
            <!--collection 用于定义关联集合类型的属性的封装规则!
                ofType用于指定集合中的类型;-->
            <collection property="emps" ofType="com.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>
        <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,gender
            FROM tbl_dept d LEFT JOIN tbl_employee e ON d.id=e.d_id
            WHERE d.id=#{id};
    
        </select>
        <!--========================================-->
    
        <!--Department中有属性: private List<Employee> emps;
        public Department getDeptByIdStep(Integer id);  //执行Collection 的分步查询-->
    
        <resultMap id="MyDeptStep" type="com.bean.Department">
            <id column="id" property="id"/>
            <result column="detp_name" property="departmentName"/>
            <collection property="emps" select="com.dao.EmployeeMapperPlus.getEmpsByDeptId"
                column="id">
            </collection>
        </resultMap>
    
        <select id="getDeptByIdStep" resultMap="MyDeptStep">
           select id,dept_name departmentName from tbl_dept
            where id=#{id}
        </select>
    
    
    </mapper>
    View Code

      EmployeeMapperPlus.xml c新增内容

        <select id="getEmpsByDeptId" resultType="com.bean.Employee">
            select * from tbl_employee where d_id = #{DeptId}
        </select>


    3.编写测试代码

        @Test
        public void test07() throws Exception{
            SqlSession openSession = getSqlSessionFactory().openSession();
            try{
                DepartmentMapper mapper = openSession.getMapper(DepartmentMapper.class);
    //            System.out.println("---tp_34---多表关联查询,使用collection 定义关联集合封装规则-----");
    //            Department department = mapper.getDeptByIdPlus(1);
    //            List<Employee> emps = department.getEmps();
    //            for(Employee e:emps)
    //                System.out.println(e);
    
                System.out.println("---tp_35---多表关联查询,使用collection分步查询&延迟加载--");
                Department dept = mapper.getDeptByIdStep(1);
                System.out.println(dept.getDepartmentName());   //只有一行时,进行按需加载
    //          System.out.println(dept);       //当要加载全部的时候,就不会延迟加载了
    
                openSession.commit();//默认是不自动提交数据的,需要我们自己手动提交
            }finally {
                openSession.close();
            }
        }

    测试结果   (全部调用时的结果)

    ---tp_35---多表关联查询,使用collection分步查询&延迟加载--
    DEBUG 12-04 12:01:58,977 ==>  Preparing: select id,dept_name departmentName from tbl_dept where id=?   (BaseJdbcLogger.java:145) 
    DEBUG 12-04 12:01:58,997 ==> Parameters: 1(Integer)  (BaseJdbcLogger.java:145) 
    DEBUG 12-04 12:01:59,069 <==      Total: 1  (BaseJdbcLogger.java:145) 
    开发部
    DEBUG 12-04 12:01:59,069 ==>  Preparing: select * from tbl_employee where d_id = ?   (BaseJdbcLogger.java:145) 
    DEBUG 12-04 12:01:59,070 ==> Parameters: 1(Integer)  (BaseJdbcLogger.java:145) 
    DEBUG 12-04 12:01:59,072 <==      Total: 2  (BaseJdbcLogger.java:145) 
    Department{id=1, departmentName='开发部'}
  • 相关阅读:
    phpcms页面替换
    phpcms笔记
    php头像上传预览
    phpcms后台管理
    php写流程管理
    php写留言板
    php人员权限管理(RBAC)
    单例模式
    Effective C++笔记——day01
    C++Primer笔记-----day08
  • 原文地址:https://www.cnblogs.com/zhazhaacmer/p/10063398.html
Copyright © 2020-2023  润新知