JavaEE——Mybatis(6)--association的分步查询与延迟加载 以及Collection和鉴别器discriminator

使用association进行分步查询:(一般,部门什么的会有自己的Mappper,所以直接利用就好,不用再重新自己写)
1、先按照学生id查询学生信息
2、根据查询学生信息中的grade_id值去班级表查出班级信息
3、班级设置到学生中;

association定义关联对象的封装规则
  select:表明当前属性是调用select指定的方法查出的结果
  column:指定将哪一列的值传给这个方法 注意不光要有所用方法所在的mapper ,还要有该方法的名字  com.dao.GradeMapper.getGradeById


  property:指定的属性
  流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性

<!--public Students getStuAndGradeStep(Integer id);-->
<resultMap id="StuByStep" type="com.person.Students">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="sex" property="sex"/>
<result column="age" property="age"/>

<!-- association定义关联对象的封装规则
select:表明当前属性是调用select指定的方法查出的结果
column:指定将哪一列的值传给这个方法

流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
-->
<association property="grade" select="com.dao.GradeMapper.getGradeById" column="grade_id">

</association>
</resultMap>
<select id="getStuAndGradeStep" resultMap="StuByStep">
SELECT * FROM student WHERE id=#{id}
</select>

   其中GradeMapper.xml中的方法

<!--public Grade getGradeById(Integer id);-->
    <select id="getGradeById" resultType="com.person.Grade">
        SELECT * FROM grade WHERE id=#{id}
    </select>

  GradeMapper接口中

public Grade getGradeById(Integer id);

  

association使用延迟加载(懒加载);(按需加载)
Students==>Grade:
我们每次查询Students对象的时候,都将一起查询出来。
班级信息在我们使用的时候再去查询;
分段查询的基础之上加上两个配置:

加在mybatis-config.xml里面

<settings>
		<!--显示的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题  -->
		<setting name="lazyLoadingEnabled" value="true"/>
		<setting name="aggressiveLazyLoading" value="false"/>
	</settings>

  

场景二:
  查询班级的时候将班级对应的所有学生信息也查询出来:

SELECT g.id id, g.grade_name gradeName, s.id stuId, s.name name, s.sex sex, s.age age 
FROM grade g LEFT JOIN student s ON g.id=s.grade_id WHERE g.id=2

  

注释在GradeMapper.xml中

<!-- public List<Employee> getEmpsByDeptId(Integer deptId); -->
<select id="getEmpsByDeptId" resultType="com.atguigu.mybatis.bean.Employee">
	select * from tbl_employee where d_id=#{deptId}
</select>

  DepartmentMapper.xml

collection标签:定义关联集合类型的属性的封装规则
         ofType:指定集合里面元素的类型

<?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.GradeMapper">

public class Grade {
    private Integer id;
    private String gradeName;
    private List<Students> students;

    <!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则  -->
    <!--public Grade getGradeAndStuById(Integer id);-->
    <resultMap id="GradeAndStu" type="com.person.Grade">
        <id column="id" property="id"/>
        <result column="gradeName" property="gradeName"/>
        <!--
			collection定义关联集合类型的属性的封装规则
			ofType:指定集合里面元素的类型
		-->
        <collection property="students" ofType="com.person.Students">
            <!-- 定义这个集合中元素的封装规则 -->
            <id column="stuId" property="id"/>
            <result column="name" property="name"/>
            <result column="sex" property="sex"/>
            <result column="age" property="age"/>
        </collection>

    </resultMap>
    <select id="getGradeAndStuById" resultMap="GradeAndStu">
        SELECT g.id id, g.grade_name gradeName,
         s.id stuId, s.name name,
         s.sex sex, s.age age
         FROM grade g
         LEFT JOIN student s ON g.id=s.grade_id
         WHERE g.id=#{id}
    </select>
</mapper>

  collection的分步查询

<!--public Grade getGradeAndStuByIdStep(Integer id);-->
    <resultMap id="GradeAndStuByStep" type="com.person.Grade">
        <id column="id" property="id"/>
        <result column="gradeName" property="gradeName"/>
        <collection property="students"
                    select="com.dao.StudentMapperPlus.getStuByGradeId"
                    column="id"></collection>
    </resultMap>
    <select id="getGradeAndStuByIdStep" resultMap="GradeAndStuByStep">
        SELECT id, grade_name gradeName FROM grade WHERE id=#{id}
    </select>

  在StudentMapper中有getStuByGradeId方法来根据班级从学生表中查询学生信息

 

<!--public List<Students> getStuByGradeId(Integer grade_id);-->
    <select id="getStuByGradeId" resultType="com.person.Students">
        SELECT * FROM student WHERE grade_id=#{grade_id}
    </select>

 

<!-- 扩展:多列的值传递过去:
将多列的值封装map传递;
column="{key1=column1,key2=column2}"
fetchType="lazy":表示使用延迟加载;
- lazy:延迟
- eager:立即
-->

 

 鉴别器

<!-- <discriminator javaType=""></discriminator>
  鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为
    封装Students:
    如果查出的是女生:就把班级信息查询出来,否则不查询;
    如果是男生,把id这一列的值赋值给age;
-->

  

<!--public Students getStuByIdDiscriminator(Integer id);-->
    <resultMap id="Discriminator" type="com.person.Students">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="sex" property="sex"/>
        <result column="age" property="age"/>
        <!--
	 		column:指定判定的列名
	 		javaType:列值对应的java类型  -->
        <discriminator javaType="String" column="sex">
            <case value="girl" resultType="com.person.Students">
                <association property="grade" select="com.dao.GradeMapper.getGradeById" column="gid"></association>
            </case>
            <case value="boy" resultType="com.person.Students">
                <result column="id" property="age"/>
            </case>
        </discriminator>

    </resultMap>
    <select id="getStuByIdDiscriminator" resultMap="Discriminator">
        SELECT s.id id, s.name name, s.sex sex, s.age age, g.id gid, g.grade_name gradename FROM student s, grade g
        WHERE s.grade_id=g.id AND s.id=#{id}
    </select>

  

原文地址:https://www.cnblogs.com/SkyeAngel/p/8258195.html