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

环境搭建,数据配置

t_class表
在这里插入图片描述
在这里插入图片描述


t_stu表
在这里插入图片描述

多对一的映射的思路逻辑

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

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


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

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

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


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

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

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


讨论内存结构

在Student对象中通过sid可以查询到学生的属性,再通过学生对象的cid可以查询到他的班级,那么就应该在Student类中加上private Class class,通过在Student类中加上对Class的引用从而完成对Class的关联
在这里插入图片描述


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

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

级联属性映射

//StudnetMapper.java
Student selectById(Integer id);
<!-- StudentMapper.xml -->
    <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);
<!--StudentMapper.xml -->
    <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>
<!--ClassMapper.xml -->
    <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>	
	<!-- 延迟加载的全局开关,默认值false不开启 -->
	<!-- 什么意思:所有只要但凡带有分布的,都采用延迟加载 -->
	<setting name="lazyLoadingEnabled" value="true"/>
</settings>

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

一对多的映射的思路逻辑

一对多,一在前,那么一就是主表,t_class就是主表,那就是在class类中加入对student的引用List<Student> 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);
<!-- ClassMapper.xml -->
    <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>
<!-- StudentMapper.xml -->
    <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();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

优降宁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值