如今咱们有 Author 做者类,其属性中有 List<Book> bookList 用来表示该做者所出版的书籍。咱们配置好映射关系以后,但愿在获取到一个 Author 类时,其 bookList 的值只有当咱们在用到的时候才会去数据库获取,而不是每次都给咱们所有的完整数据,这也就是MyBatis延迟加载要解决的问题:即映射集合内存在级联时,咱们实际须要的数据少于数据库查出来的数据。这就会形成数据库多查出来的数据派不上用场,同时也加大了数据库负担。
咱们在提到 MyBatis 的内联时,必然会涉及到的就是两个元素 association 和 collection,在以前的阐述中咱们也说过,对于内联查询的方式有一种利用到了 select 属性,也便是咱们延迟加载的主人公。咱们说使用 select 其实是将SQL语句分红了两部分单独执行,而不是采用联表查询的SQL,也正是这样,能让咱们实现延迟加载。
在MyBatis中实现延迟加载有两种方式,若是你但愿全局开启延迟加载,那么你须要在mybatis-config.xml全局配置文件的<settings>中加入下面这两个配置:
<!--开启全局延迟加载(懒加载)-->
<setting name="lazyLoadingEnabled" value="true" />
<!--任何方法调用均加载该对象全部属性-->
<setting name="aggressiveLazyLoading" value="false"/>
若是你并不想全局开启,只是针对某个类的某些属性进行延迟加载开启,那么则须要你在 resultMap 中的 association 或 collection 设置属性值 fetchType="lazy",以下示例:
<association fetchType="lazy" property="author" column="author_id" javaType="dulk.learn.mybatis.pojo.Author" select="findAuthorById"/>
刚才咱们提到了做者和书之间的一对多关系,下面咱们就来以此为例看下相关的代码和配置。
Author类:
public class Author {
private long id;
private String name;
private int age;
private List<Book> bookList;
//... getter and setter
}
Dao接口:
public interface AuthorDao {
Author findAuthorById(long id);
Book findBookByAuthorId(long authorId);
}
mapper.xml:
<mapper namespace="dulk.learn.mybatis.dao.AuthorDao">
<resultMap id="authorResultMap" type="dulk.learn.mybatis.pojo.Author">
<id property="id" column="id"/>
<result property="name" column="name" />
<result property="age" column="age" />
<!--采用select形式,设定fetchType延迟加载,column为关联查询的列-->
<collection fetchType="lazy" property="bookList" column="id" select="findBookByAuthorId" />
</resultMap>
<select id="findAuthorById" parameterType="long" resultMap="authorResultMap">
SELECT *
FROM author
WHERE id = #{id}
</select>
<select id="findBookByAuthorId" parameterType="long" resultType="dulk.learn.mybatis.pojo.Book">
SELECT *
FROM book
WHERE author_id = #{authorId}
</select>
</mapper>
- 使用select方式加载内联属性的数据(而非联表查询)
- 设置全局延迟加载,或者局部设置 fetchType="lazy"
- column列为关联查询的列
写个单元测试看看:
public class TestAuthor {
@org.junit.Test
public void testMyBatis() throws IOException {
//读取配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//获取工厂类
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//获取SqlSession数据库会话对象
SqlSession sqlSession = factory.openSession();
//获取Dao
AuthorDao authorDao = sqlSession.getMapper(AuthorDao.class);
Author author = authorDao.findAuthorById(2);
System.out.println("-------------------------------------");
List<Book> bookList = author.getBookList();
}
}
以下输出日志,能够看到bookList在调用时才执行了SQL语句去查询:
Opening JDBC Connection
Created connection 1685232414.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@64729b1e]
==> Preparing: SELECT * FROM author WHERE id = ?
==> Parameters: 2(Long)
<== Columns: id, name, age
<== Row: 2, 李四, 18
<== Total: 1
-------------------------------------
==> Preparing: SELECT * FROM book WHERE author_id = ?
==> Parameters: 2(Long)
<== Columns: id, name, price, author_id
<== Row: 1, 小帽子历险记, 23, 2
<== Row: 2, 红帽子探险记, 12, 2
<== Total: 2