Mybatis-7-延迟加载

一、Mybatis配置

要让mybatis开启延迟加载,需要配置:
在这里插入图片描述
配置文件代码:

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

二、一对一的延迟加载

映射文件:

CardMapper.xml:

<?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.seven.mapper.CardMapper">
    <resultMap id="cardMap" type="com.seven.entity.Card">
        <id property="cid" column="cid"/>
        <result property="cnum" column="cnum"/>
    </resultMap>

    <select id="findAll" resultMap="cardMap">
        select * from t_card
    </select>

    <select id="findById" resultMap="cardMap">
        select * from t_card where cid=#{cid}
    </select>
</mapper>

PersonMapper.xml:

<?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.seven.mapper.PersonMapper">
    <resultMap id="personMap" type="com.seven.entity.Person">
        <id property="pid" column="pid"/>
        <result property="name" column="name"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>
        <result property="birthday" column="birthday"/>
        <result property="cid" column="cid"/>
    </resultMap>

    <resultMap id="pcMap" type="com.seven.entity.Person" extends="personMap">
        <association property="card" column="cid"
                     select="com.seven.mapper.CardMapper.findById"/>
    </resultMap>

    <select id="findAll" resultMap="pcMap">
        select * from t_person
    </select>
</mapper>
测试类
	/**
     * 测试一对一延迟加载
     */
    @Test
    public void testFindDetailed() {
        List<Person> persons = mapper.findAll();
        for (Person person : persons) {
            System.out.println("-------------------------------------");
            //使用该card属性
            System.out.println(person.getCard());
        }
    }

使用延迟加载的结果:在使用card属性的时候才会去数据库中查
在这里插入图片描述
不使用延迟加载的结果:会一次性查完
在这里插入图片描述

三、一对多/多对多延迟加载

映射文件

OrderMapper.xml:

<?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.seven.mapper.OrderMapper">
    <resultMap id="orderMap" type="com.seven.entity.Order">
        <id property="oid" column="oid"/>
        <result property="pid" column="pid"/>
        <result property="odate" column="odate"/>
        <result property="price" column="price"/>
    </resultMap>

    <resultMap id="ogMap" type="com.seven.entity.Order" extends="orderMap">
        <collection property="goods" ofType="com.seven.entity.Goods">
            <id property="gid" column="gid"/>
            <result property="gname" column="gname"/>
            <result property="gprice" column="gprice"/>
            <result property="gdesc" column="gdesc"/>
            <result property="gstatus" column="gstatus"/>
        </collection>
    </resultMap>

    <select id="findAll" resultMap="orderMap">
        select * from t_order
    </select>

    <select id="findOrderGoods" resultMap="ogMap">
        SELECT o.*, g.* FROM t_order o
        LEFT JOIN t_order_goods og ON og.oid=o.oid
        LEFT JOIN t_goods g ON og.gid=g.gid;
    </select>

    <select id="findByPid" resultMap="orderMap">
        select * from t_order where pid=#{pid}
    </select>
</mapper>

PersonMapper.xml:

<?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.seven.mapper.PersonMapper">
    <resultMap id="personMap" type="com.seven.entity.Person">
        <id property="pid" column="pid"/>
        <result property="name" column="name"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>
        <result property="birthday" column="birthday"/>
        <result property="cid" column="cid"/>
    </resultMap>

    <!-- 一对多 -->
    <resultMap id="poMap" type="com.seven.entity.Person" extends="personMap">
        <collection property="orders" ofType="com.seven.entity.Order" column="pid"
                    select="com.seven.mapper.OrderMapper.findByPid"/>
    </resultMap>

    <select id="findAll" resultMap="poMap">
        select * from t_person
    </select>
</mapper>
测试类
	/**
     * 测试一对多/多对多延迟加载
     */
    @Test
    public void testFindPersonOrder() {
        List<Person> persons = mapper.findAll();
        for (Person person : persons) {
            System.out.println("--------------------------------------------");
            for (Order order : person.getOrders()) {
                System.out.println(order);
            }
        }
    }
结果

在这里插入图片描述


更新时间:2020-1-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值