一、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