MyBatis懒加载问题

在我们查数据时,mybatis会默认把关联的数据查出来,而关联查询比单表查询慢,并且我们有时候不需要那么多的数据。所以我们需要开启懒加载,关联的数据等需要的时候再查出来

myBatis开启懒加载,需要在mybatis主配置文件的settings标签中配置lazyLoadingEnabled为true,aggressiveLazyLoading为false。lazyLoadingEnabled=true即开启延迟加载,aggressiveLazyLoading=false即按需加载。

参数 描述 有效值 默认值
lazyLoadingEnabled 延迟加载的全局开关,当他开启时,所有关联的对象都会延迟加载,特定的关联关系可以通过设置fetchType属性来覆盖选项的开关状态 true,false false
aggressiveLazyLoading 当启用时,当调用对象的任意属性时,如果对象具有懒加载的属性,会把对象完整加载,即把懒加载的属性全部查出来 true,false true

由于我们对lazyLoadingEnable属性的含义比较熟悉,下面我们先单独看这个:
在mybatis主文件中配置:

<settings>
        <setting name="lazyLoadingEnabled" value="true"/>
</settings>

pojo对象层级关系如下:

这里写图片描述

测试发现,确实属性懒加载了。但是它还是会把相同层级的对象数据也会查出来。这是因为默认aggressiveLazyLoading=true,表示按层级延迟加载。

例如当查询出学生的时候,因为健康情况和关联对象属于同一级别,因此会把健康情况也查出来。
这里写图片描述
代码:

public void test() throws IOException{
        SqlSession sqlSession=null;
        try{
            sqlSession = SqlSessionFactoryUtil.openSqlSession();
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            MaleStudentBean student = (MaleStudentBean) studentMapper.getStudent(1);
            sqlSession.commit();
        }finally{
            if(sqlSession!=null){
                sqlSession.close();
            }
        }

    }

当访问对象的一些字段信息时,还会发生一些“异常情况”:

1.如当访问学生信息的时候,会把学生证和课程成绩查出来
这里写图片描述

@Test
    public void test() throws IOException{
        SqlSession sqlSession=null;
        try{
            sqlSession = SqlSessionFactoryUtil.openSqlSession();
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            MaleStudentBean student = (MaleStudentBean) studentMapper.getStudent(1);
            student.getSname();
            sqlSession.commit();
        }finally{
            if(sqlSession!=null){
                sqlSession.close();
            }
        }

    }

还有当访问课程成绩的成绩字段时,对应课程id的课程信息也会查出来

这里写图片描述

public void test() throws IOException{
        SqlSession sqlSession=null;
        try{
            sqlSession = SqlSessionFactoryUtil.openSqlSession();
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            MaleStudentBean student = (MaleStudentBean) studentMapper.getStudent(1);
            student.getStudentLectureList().get(0).getGrade();
            sqlSession.commit();
        }finally{
            if(sqlSession!=null){
                sqlSession.close();
            }
        }

    }

这正是因为aggressiveLazyLoading=true的缘故。它会造成下列影响:

  • 当查询对象时,会把相同级别的关联对象查出来(鉴别器discirminator标识的对象)
  • 当访问对象任何的属性时,如果对象具有懒加载的属性,那么对象会做完整加载,把其他没加载的懒加载属性全部查出来

下面把aggressiveLazyLoading配置成false,false表示按需加载,懒加载的字段只有我们需要时,他才会去查询。即会出现我们想要的情况

<settings>
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
</settings>

查询student对象时,只会查询student表
这里写图片描述

映射器配置如下:
studentMapper.xml

<mapper namespace="com.chenny.mybatisTest.mapper.StudentMapper">
    <resultMap id="studentMap" type="com.chenny.mybatisTest.pojo.Student">
        <id column="id" property="id"></id>
        <result column="sname" property="sname" typeHandler="com.chenny.mybatisTest.typeHandler.MyStringTypeHandler"></result>
        <result column="sex" property="sex" javaType="com.chenny.mybatisTest.enums.Sex" jdbcType="INTEGER" typeHandler="com.chenny.mybatisTest.typeHandler.SexEnumTypeHandler"/>
        <result column="note" property="note"/>
        <association property="studentSelfCard" column="id" select="com.chenny.mybatisTest.mapper.StudentSelfcardMapper.findStudentSelfcardByStudentId"></association>
        <collection property="studentLectureList" column="id" select="com.chenny.mybatisTest.mapper.StudentLectureBeanMapper.findStudentLectureBeanByStudentId"></collection>
        <discriminator javaType="int" column="sex">
            <case value="1" resultMap="maleStudentBeanMap"></case>
            <case value="2" resultMap="femaleStudentBeanMap"></case>
        </discriminator>
    </resultMap>

    <resultMap id="maleStudentBeanMap" type="maleStudentBean" extends="studentMap">
        <collection property="studentHealthMaleList"  ofType="studentHealthMaleBean" column="id" select="findMaleStudentHealthById"></collection>
    </resultMap>

    <resultMap id="femaleStudentBeanMap" type="com.chenny.mybatisTest.pojo.FemaleStudentBean" extends="studentMap">
        <collection property="studentHealthMaleList"  ofType="studentHealthFemaleBean" column="id" select="findFemaleStudentHealthById"></collection>
    </resultMap>

    <resultMap type="com.chenny.mybatisTest.pojo.StudentHealthFemaleBean" id="studentHealthFemale" extends="studentHealthBeanMap"></resultMap>
    <resultMap type="com.chenny.mybatisTest.pojo.StudentHealthMaleBean" id="studentHealthMale" extends="studentHealthBeanMap"></resultMap>


    <resultMap type="com.chenny.mybatisTest.pojo.StudentHealthBean" id="studentHealthBeanMap">
        <id property="id" column="id"/>
        <result property="studentId" column="student_id"/>
        <result property="feature" column="feature"/>
    </resultMap>

studentLectureBeanMapper.xml

 <resultMap id="studentLectureBeanMap" type="com.chenny.mybatisTest.pojo.StudentLectureBean">
        <id column="id" property="id"></id>
        <result property="studentId" column="student_id"/>
        <result property="grade" column="grade"></result>
        <result property="note" column="note"></result>
        <association property="lectureBean" column="lecture_id" select="com.chenny.mybatisTest.mapper.LectureBeanMapper.findLectureBeanByLectureId" javaType="com.chenny.mybatisTest.pojo.LectureBean" ></association>
    </resultMap>

LectureBeanMapper.xml

    <resultMap id="studentLectureBeanMap" type="com.chenny.mybatisTest.pojo.StudentLectureBean">
        <id column="id" property="id"></id>
        <result property="studentId" column="student_id"/>
        <result property="grade" column="grade"></result>
        <result property="note" column="note"></result>
        <association property="lectureBean" column="lecture_id" select="com.chenny.mybatisTest.mapper.LectureBeanMapper.findLectureBeanByLectureId" javaType="com.chenny.mybatisTest.pojo.LectureBean" ></association>
    </resultMap>
原文地址:https://www.cnblogs.com/chenny3/p/10226158.html