MyBatis学习笔记之高级映射及延迟加载

文章目录

  • 环境搭建,数据配置
  • 多对一的映射的思路逻辑
    • 级联属性映射
    • association
    • 分布查询
  • 一对多的映射的思路逻辑
    • collection
    • 分布

环境搭建,数据配置

t_class表
在这里插入图片描述
MyBatis学习笔记之高级映射及延迟加载_第1张图片


t_stu表
MyBatis学习笔记之高级映射及延迟加载_第2张图片

多对一的映射的思路逻辑

多对一:多个学生对应一个班级

多的一方是student,
一的一方是class


怎么分主表和副表
谁在前,谁是主表

多对一和一对多其实都是一样的“叫法”,就是主宾之间的顺序,这里的区分是对于设计需求逻辑的区分

多对一:多在前,那么多就是主表
一对多:一在前,那么一就是主表


现在选取多对一,那么多的一方是学生,那么学生就是主表,班级就是副表

所以映射到JVM虚拟机中的主对象就是学生对象

为什么映射过去的是学生对象,因为学生是主表,映射过去的是主表对象


讨论内存结构

在Student对象中通过sid可以查询到学生的属性,再通过学生对象的cid可以查询到他的班级,那么就应该在Student类中加上private Class class,通过在Student类中加上对Class的引用从而完成对Class的关联
MyBatis学习笔记之高级映射及延迟加载_第3张图片


mybatis实现映射
多种方式,常见的包括三种

  • 第一种方式:一条SQL语句,级联属性映射
  • 第二种方式:一条SQL语句,association
  • 第三种方式:两条SQL语句,分布查询。(这种方式常用:优点一是可复用,优点二十支持懒加载。)

级联属性映射

//StudnetMapper.java
Student selectById(Integer id);

    <resultMap id="studentResultMap" type="Student">
        <id property="sid" column="sid"/>
        <result property="sname" column="sname"/>
        <result property="clazz.cid" column="cid"/>
        <result property="clazz.cname" column="cname"/>
    resultMap>
    <select id="selectById" resultMap="studentResultMap">
    select
        s.sid,s.sname,c.cid,c.cname
    from
        t_stu s left join t_class c on s.cid = c.cid
    where
        sid = #{sid}
    select>
//test文件
    @Test
    public void testSelectById(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student1 = mapper.selectById(2);
        System.out.println(student1);
        sqlSession.close();
    }

association

association译为关联,一个Student对象关联一个Class对象

    Student selectByIdByAssociation(Integer id);
    <resultMap id="studentResultMapByAssociation" type="Student">
        <id property="sid" column="sid"/>
        <result property="sname" column="sname"/>
        <association property="clazz" javaType="Clazz">
            <id property="cid" column="cid"/>
            <result property="cname" column="cname"/>
        association>
    resultMap>
    <select id="selectByIdByAssociation" resultMap="studentResultMapByAssociation">
        select
            s.sid,s.sname,c.cid,c.cname
        from
            t_stu s left join t_class c on s.cid = c.cid
        where
            sid = #{sid}
    select>
    @Test
    public void testSelectByIdByAssociation(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectByIdByAssociation(6);
        System.out.println(student);
        sqlSession.close();
    }

SQL语句都是一样的,不同的就是对于class这个“外面来的类”不同的处理方式,如何去访问到其中的数据

分布查询

显而易见的方式,不用动脑子都能想到的方法,先去查student,再利用student获取的cid去查询cname
其中最大的问题就是关于sql语句如何将cid的值赋上
并且如何再从其中拿出来再进行查询


//StudentMapper.java
Student selectByIdStep1(Integer id);
//ClassMapper.java
Clazz selectByIdStep2(Integer cid);

    <resultMap id="studentResultMapByStep" type="Student">
        <id property="sid" column="sid"/>
        <result property="sname" column="sname"/>
        <association property="clazz"
                     select="org.powernode.mapper.ClassMapper.selectByIdStep2"
                     column="cid"/>
    resultMap>


    <select id="selectByIdOnlyStudent" resultMap="studentResultMapByStep">
        select
            sid,sname,cid
        from
            t_stu
        where
            sid = #{sid}
    select>

    <select id="selectByIdStep2" resultType="Clazz">
        select
            cid,cname
        from
            t_class
        where
            cid = #{cid}
    select>
//test
    @Test
    public void TestAll2(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectByIdOnlyStudent(6);
        System.out.println(student);
        sqlSession.close();
    }

about 优点

  • 第一点:复用性增强,可以重复利用。(大步拆成N多个小碎步,每一个小碎步更加可以重复利用)
  • 第二点:采用这种分布查询,可以充分利用他们的延迟加载/懒加载机制。

什么是延迟加载(懒加载),有什么用?
延迟加载的核心原理就是:用的时候再执行查询语句,不用的时候不查询
作用:提供性能。


在mybatis中如何开启延迟加载?

association标签中添加fetchType=“lazy”
注意:默认情况下是没有开启延迟加载的,需要设置:fetchType=“lazy”
这种在Association标签中配置fetchType=“lazy”的是局部设置,只对当前的Association关联的sql语句起作用
在实际开发中,大部分都是需要使用延迟加载的,所以建议开启全部的延迟加载机制
可以在mybatis-config中配置setting

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

如果在开启全局之后有部分地方在这里插入代码片不想要开启懒加载,可以在associationfetchType中设置为eager

一对多的映射的思路逻辑

一对多,一在前,那么一就是主表,t_class就是主表,那就是在class类中加入对student的引用List stus

一对多的实现通常包括两种实现方式:

  • 第一种方式:collection
  • 第二种方式:分布查询(老伙计)

collection

//classMapper.java
    Clazz selectById(Integer id);

//classMapper.xml
    <resultMap id="clazzResultMap" type="Clazz">
        <id property="cid" column="cid"/>
        <result property="cname" column="cname"/>
        <collection property="stus" ofType="Student">
            <id property="sid" column="sid"/>
            <result property="sname" column="sname"/>
        collection>
    resultMap>
    <select id="selectById" resultMap="clazzResultMap">
        select
            c.cid,c.cname,s.sid,s.sname
        from
            t_class c left join t_stu s on c.cid = s.cid
        where
            c.cid = #{cid}
    select>
//test
    @Test
    public void testSelectById(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        ClassMapper mapper = sqlSession.getMapper(ClassMapper.class);
        Clazz clazz = mapper.selectById(1001);
        System.out.println(clazz);
        sqlSession.close();
    }

分布

//ClassMapper.java
Clazz selectByStep(Integer cid);
//StudentMapper.java
List<Student> selectByIdByStep(Integer cid);

    <resultMap id="clazzResultMapStep" type="Clazz">
        <id property="cid" column="cid"/>
        <result property="cname" column="cname"/>
        <collection property="stus"
                    select="org.powernode.mapper.StudentMapper.selectByIdByStep"
                    column="cid"/>
    resultMap>
    <select id="selectByStep" resultMap="clazzResultMapStep">
        select
            cid,cname
        from
            t_class
        where
            cid = #{cid}
    select>

    <select id="selectByIdByStep" resultType="Student">
        select
            *
        from
            t_stu
        where
            cid = #{cid}
    select>
//test
    @Test
    public void testByStep(){
        SqlSession sqlSession = SqlSessionUtil.openSession();
        ClassMapper mapper = sqlSession.getMapper(ClassMapper.class);
        Clazz clazz = mapper.selectByStep(1001);
        System.out.println(clazz);
        sqlSession.close();
    }

你可能感兴趣的:(mybatis学习笔记,mybatis,学习,笔记)