zoukankan      html  css  js  c++  java
  • MyBatis --- 映射关系【一对一、一对多、多对多】,懒加载机制

    映射(多、一)对一的关联关系

    1)若只想得到关联对象的id属性,不用关联数据表

    2)若希望得到关联对象的其他属性,要关联其数据表

    举例:

    员工与部门的映射关系为:多对一

    1.创建表

    员工表

    确定其外键是部门表的 id

    DROP TABLE IF EXISTS emp;
    
    CREATE TABLE emp(
    id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
    emp_name VARCHAR(255) DEFAULT NULL,
    gender CHAR(1) DEFAULT NULL,
    email VARCHAR(255) DEFAULT NULL,
    dept_id INT(22) DEFAULT NULL,
    FOREIGN KEY (dept_id) REFERENCES dept(id)
    )
    

     部门表

    CREATE TABLE dept (
    id INT(11) PRIMARY KEY AUTO_INCREMENT,
    dept_name VARCHAR(255)
    )
    

     2.创建相应的实体类和Mapper接口

    查询的方法有三!

    方法一:

    - 写关联的 sql 语句

    将两个表通过部门表的id关联,查找员工表id为1的员工信息

    SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id,d.id,d.dept_name
    FROM emp e,dept d
    WHERE e.dept_id = d.id AND e.id = 1
    

     - 在sql映射文件中写映射sql语句【联合查询:级联属性封装结果集】

    使用resultMap映射,对于【对一】关系可以不用 association

    <resultMap type="com.neuedu.mybatis.entity.TblEmployee" id="getEmployeeByIdMap">
          <!-- Employee -->
          <id column="id" property="id"/>
           <result column="emp_name" property="empName"/>
           <result column="gender" property="gender"/>
           <result column="email" property="email"/>
           <!-- Department -->
           <result column="id" property="dept.id"/>
           <result column="dept_name" property="dept.deptName"/>
    </resultMap>
    <select id="getEmployeeById" resultMap="getEmployeeByIdMap">
          SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id,d.id,d.dept_name
          FROM emp e,dept d
          WHERE e.dept_id = d.id AND e.id = #{id}
    </select>
    

     - 编写测试用例

    private ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
    @Test
    public void test() {
          TblEmployeeMapper bean = ioc.getBean(TblEmployeeMapper.class);
          TblEmployee employee = bean.getEmployeeById(1);
          System.out.println(employee);
    }
    

     方法二:使用association来定义关联对象的规则,【比较正规的,推荐的方式】

    <resultMap type="com.neuedu.mybatis.entity.TblEmployee" id="getEmployeeByIdMap">
          <!-- Employee -->
          <id column="id" property="id"/>
           <result column="emp_name" property="empName"/>
           <result column="gender" property="gender"/>
           <result column="email" property="email"/>
           <!--
                   association可以指定联合的javaBean对象
                   property="dept":指定哪个属性是联合的对象
                   javaType:指定这个属性对象的类型【不能省略】
                -->
           <association property="dept" javaType="com.neuedu.mybatis.entity.TblDepartment">
                      <id column="id" property="id"/>
                      <result column="dept_name" property="deptName"/>
           </association>
    </resultMap>
    
    <select id="getEmployeeById" resultMap="getEmployeeByIdMap">
          SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id,d.id,d.dept_name
          FROM emp e,dept d
          WHERE e.dept_id = d.id AND e.id = #{id}
    </select>
    

     方法三:上述结果相当于使用嵌套结果集的形式,还可以使用 association 进行分步查询:

    使用association进行分步查询:
       1.先按照员工id查询员工信息
       2.根据查询员工信息中d_id值取部门表查出部门信息
       3.部门设置到员工中:
    <!-- 通过id与resultMap相连 -->
    <select id="selectDept" resultType="com.neuedu.mybatis.entity.TblDepartment">
          select d.id,d.dept_name
          from dept d
          where d.id = #{id}
    </select>
    <!-- 通过id被查找employee的select找到 -->
    <resultMap type="com.neuedu.mybatis.entity.TblEmployee" id="getEmployeeByIdMap">
          <id column="id" property="id"/>
        <result column="emp_name" property="empName"/>
        <result column="gender" property="gender"/>
        <result column="email" property="email"/>
        <!-- property:是employee中的属性,
           select:dept的select语句的id
           column:查找到的部门的id,传到dept的select中
         -->
        <association property="dept" select="selectDept" column="dept_id"/>
    </resultMap>
    
    <select id="getEmployeeById" resultMap="getEmployeeByIdMap">
          SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id
          from emp e
          where e.id = #{id}
    </select>
    
    修改:
    由于上个方法中有两个select
    一个针对employee,另一个针对department
    所以另写一个DepartmentMapper,存放针对 department 的 select
    在 DepartmentMapper 中:
    <mapper namespace="com.neuedu.mybatis.mapper.TblDepartmentMapper">
    
          <select id="selectDept" resultType="com.neuedu.mybatis.entity.TblDepartment">
                select d.id,d.dept_name
                from dept d
                where d.id = #{id}
          </select>
    
    </mapper>
    

    之前的 association 中的select是写 id,现在写 DepartmentMapper 的全类名+ id

    <resultMap type="com.neuedu.mybatis.entity.TblEmployee" id="getEmployeeByIdMap">
          <id column="id" property="id"/>
        <result column="emp_name" property="empName"/>
        <result column="gender" property="gender"/>
        <result column="email" property="email"/>
        <!-- property:是employee中的属性,
           select:select语句的id
           column:查找到的部门的id,传到查找dept的select中
         -->
        <association property="dept" select="com.neuedu.mybatis.mapper.TblDepartmentMapper.selectDept" column="dept_id"/>
    </resultMap>
    
    <select id="getEmployeeById" resultMap="getEmployeeByIdMap">
          SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id
          from emp e
          where e.id = #{id}
    </select>
    

    懒加载机制【按需加载】:

    我们只需要在分步查询的基础之上,在mybatis的全局配置文件中加入两个属性:

    <!-- 开启懒加载机制 ,默认值为false-->
    <setting name="lazyLoadingEnabled" value="true"/>
    <!-- 开启的话,每个属性都会直接全部加载出来;禁用的话,只会按需加载出来 -->
    <setting name="aggressiveLazyLoading" value="false"/>
    
    private ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
    @Test
    public void test() {
          TblEmployeeMapper bean = ioc.getBean(TblEmployeeMapper.class);
          TblEmployee employee = bean.getEmployeeById(1);
          System.out.println(employee.getEmpName());
    }
    
    此时:可以看到这里只发送了一条SQL语句。
     
    懒加载机制,是针对第三种映射的,因为只有第三种映射有两个select语句
     
    注意:如果关联的是一个对象,可以使用association标签
     
    映射对多的关联关系
    查询部门的时候,将部门对应的所有员工信息也查询出来,注释在DepartmentMapper.xml中
     
    第一种:
    1.修改Department实体类【添加Employee集合,并为该集合提供getter/setter方法】
    private List<TblEmployee> list;
    public List<TblEmployee> getList() {
          return list;
    }
    public void setList(List<TblEmployee> list) {
          this.list = list;
    }
    

     建立DepartmentMapper接口文件,并添加如下方法:

    public TblDepartment selectDeptAndEmp(Integer id);
    

     2.sql映射文件中的内容为:【collection:嵌套结果集的方式:使用collection标签定义关联的集合类型元素的封装规则】

    <resultMap type="com.neuedu.mybatis.entity.TblDepartment" id="selectDeptAndEmpMap">
          <id column="id" property="id"/>
          <result column="dept_name" property="deptName"/>
          <!-- property:Department中的属性名,ofType集合中存放的类型 -->
          <collection property="list" ofType="com.neuedu.mybatis.entity.TblEmployee">
                <id column="eid" property="id"/>
                <result column="emp_name" property="empName"/>
                <result column="gender" property="gender"/>
                <result column="email" property="email"/>
          </collection>
    </resultMap>
    <select id="selectDeptAndEmp" resultMap="selectDeptAndEmpMap">
          select d.id,d.dept_name,e.id eid,e.emp_name,e.gender,e.email,e.dept_id
          from dept d
          LEFT JOIN emp e
          ON d.id = e.dept_id
          WHERE d.id = #{id}
    </select>
    

     3.测试方法为:

    @Test
    public void TestselectDeptAndEmp(){
          TblDepartmentMapper bean = ioc.getBean(TblDepartmentMapper.class);
          TblDepartment dept = bean.selectDeptAndEmp(1);
          List<TblEmployee> list = dept.getList();
          for (TblEmployee tblEmployee : list) {
                System.out.println(tblEmployee);
          }
    }
    

    第二种:使用分步查询结果集

    1.在DepartmentMapper接口文件中添加方法,如下所示:

    public TblDepartment selectDeptAndEmp(Integer id);
    

     2.再从EmployeeMapper接口中添加一个方法,如下所示:

    public List<TblEmployee> getEmps(Integer deptId);
    

     3.并在响应的sql映射文件中添加相应的sql语句

    <select id="getEmps" resultType="com.neuedu.mybatis.entity.TblEmployee">
          SELECT e.id,e.emp_name,e.gender,e.email,e.dept_id
          FROM emp e
          WHERE e.dept_id = #{id}
    </select>
    

     4.在DepartmentMapper映射文件中:

    <resultMap type="com.neuedu.mybatis.entity.TblDepartment" id="selectDeptAndEmpMap">
          <id column="id" property="id"/>
          <result column="dept_name" property="deptName"/>
          <!-- property:Department中的属性名,ofType集合中存放的类型 -->
          <collection property="list" select="com.neuedu.mybatis.mapper.TblEmployeeMapper.getEmps" column="id"/>
    </resultMap>
    
    <select id="selectDeptAndEmp" resultMap="selectDeptAndEmpMap">
          SELECT d.id,d.dept_name
          FROM dept d
          WHERE d.id = #{id}
    </select>
    

     5.测试类:

    @Test
    public void TestselectDeptAndEmp(){
          TblDepartmentMapper bean = ioc.getBean(TblDepartmentMapper.class);
          TblDepartment dept = bean.selectDeptAndEmp(1);
          List<TblEmployee> list = dept.getList();
          for (TblEmployee tblEmployee : list) {
                System.out.println(tblEmployee);
          }
    }
    
    映射(一)对多、(多)对多的关联关系=======》【映射"对多"的关联关系】
    1.必须使用collection节点进行映射
    2.基本示例:
    注意:
    1). ofType指定集合中的元素类型
    2). collection标签:映射多的一端的关联关系,使用ofType指定集合中的元素类型
         column:传要查找的项,如按 id 查找就传 id
         prefix:指定列的前缀
         使用情境:若关联的数据表和之前的数据表有相同的列名,此时就需要给关联的列其"别名".
         若有多个列需要起别名,可以为所有关联的数据表的列都加上相同的前缀,然后再映射时指定前缀。
     
  • 相关阅读:
    重载函规则
    lambd
    内联函数
    c和c++中的枚举和 区别
    关于于c++中的类型转换
    作用域解析运算符
    day01
    二级指针输入特性
    二级指针的 数出特性
    influence maximization 第二弹
  • 原文地址:https://www.cnblogs.com/lwj-0923/p/7482800.html
Copyright © 2011-2022 走看看