您现在的位置是:首页 > 个人日记个人日记

mybatis-08-延迟加载

2019-02-03 18:17:16【个人日记】72人已围观

简介说简单就是需要的时候查,不需要就不查

延迟加载

collection便签中的fecthType属性

eager表示积极加载

lazy表示延迟加载(需要时加载,不需要时不加载)

举例:User和Orders关系,当使用toString()方法时,将会打印Orders信息,如果只是单独调用User的信息,则Orders不会被查询调用

当使用积极加载时

控制台输出

  1. D:\Java\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:D:\IntelliJ\lib\idea_rt.jar=6538:D:\IntelliJ\bin -Dfile.encoding=UTF-8 -classpath D:\IntelliJ\lib\idea_rt.jar;D:\IntelliJ\plugins\junit\lib\junit-rt.jar;D:\IntelliJ\plugins\junit\lib\junit5-rt.jar;D:\mybatis05\target\test-classes;D:\mybatis05\target\classes;C:\Users\Administrator\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar;C:\Users\Administrator\.m2\repository\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.huangxin.order.mapper.OrderMapperTest,getById
  2. 2019-01-03 10:38:47 [DEBUG] Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
  3. WARNING: An illegal reflective access operation has occurred
  4. WARNING: Illegal reflective access by org.apache.ibatis.reflection.Reflector (file:/C:/Users/Administrator/.m2/repository/org/mybatis/mybatis/3.4.6/mybatis-3.4.6.jar) to method java.lang.Class.checkPackageAccess(java.lang.SecurityManager,java.lang.ClassLoader,boolean)
  5. WARNING: Please consider reporting this to the maintainers of org.apache.ibatis.reflection.Reflector
  6. WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
  7. WARNING: All illegal access operations will be denied in a future release
  8. 2019-01-03 10:38:47 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  9. 2019-01-03 10:38:47 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  10. 2019-01-03 10:38:47 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  11. 2019-01-03 10:38:47 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  12. 2019-01-03 10:38:47 [DEBUG] Opening JDBC Connection
  13. 2019-01-03 10:38:47 [DEBUG] Created connection 2037764568.
  14. 2019-01-03 10:38:47 [DEBUG] Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@7975d1d8]
  15. 2019-01-03 10:38:47 [DEBUG] > Preparing: SELECT * FROM user ;
  16. 2019-01-03 10:38:47 [DEBUG] > Parameters:
  17. 2019-01-03 10:38:47 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
  18. 2019-01-03 10:38:47 [DEBUG] > Parameters: 1(Integer)
  19. 2019-01-03 10:38:47 [DEBUG] < Total: 2
  20. 2019-01-03 10:38:47 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
  21. 2019-01-03 10:38:47 [DEBUG] > Parameters: 2(Integer)
  22. 2019-01-03 10:38:47 [DEBUG] < Total: 1
  23. 2019-01-03 10:38:47 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
  24. 2019-01-03 10:38:47 [DEBUG] > Parameters: 3(Integer)
  25. 2019-01-03 10:38:47 [DEBUG] < Total: 1
  26. 2019-01-03 10:38:47 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
  27. 2019-01-03 10:38:47 [DEBUG] > Parameters: 4(Integer)
  28. 2019-01-03 10:38:47 [DEBUG] < Total: 2
  29. 2019-01-03 10:38:47 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
  30. 2019-01-03 10:38:47 [DEBUG] > Parameters: 5(Integer)
  31. 2019-01-03 10:38:47 [DEBUG] < Total: 3
  32. 2019-01-03 10:38:47 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
  33. 2019-01-03 10:38:47 [DEBUG] > Parameters: 6(Integer)
  34. 2019-01-03 10:38:47 [DEBUG] < Total: 1
  35. 2019-01-03 10:38:47 [DEBUG] < Total: 6
  36. [User{id=1, userName='黄鑫', userTelephone='1234567890', userSex='男', orders=[Orders{id=1, userId='1', createTime=Tue Jan 01 19:49:58 CST 2019}, Orders{id=2, userId='1', createTime=Tue Jan 01 19:50:46 CST 2019}]}, User{id=2, userName='张三', userTelephone='1234567890', userSex='男', orders=[Orders{id=3, userId='2', createTime=Tue Jan 01 19:50:52 CST 2019}]}, User{id=3, userName='李四', userTelephone='1234567890', userSex='男', orders=[Orders{id=4, userId='3', createTime=Tue Jan 01 19:50:55 CST 2019}]}, User{id=4, userName='王麻子', userTelephone='1234567890', userSex='男', orders=[Orders{id=5, userId='4', createTime=Tue Jan 01 19:50:57 CST 2019}, Orders{id=6, userId='4', createTime=Tue Jan 01 19:51:00 CST 2019}]}, User{id=5, userName='小红', userTelephone='1234567890', userSex='女', orders=[Orders{id=7, userId='5', createTime=Tue Jan 01 19:51:03 CST 2019}, Orders{id=8, userId='5', createTime=Tue Jan 01 19:51:05 CST 2019}, Orders{id=9, userId='5', createTime=Tue Jan 01 19:51:07 CST 2019}]}, User{id=6, userName='小明', userTelephone='1234567890', userSex='男', orders=[Orders{id=10, userId='6', createTime=Tue Jan 01 19:51:09 CST 2019}]}]
  37. 2019-01-03 10:38:47 [DEBUG] Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@7975d1d8]
  38. 2019-01-03 10:38:47 [DEBUG] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@7975d1d8]
  39. 2019-01-03 10:38:47 [DEBUG] Returned connection 2037764568 to pool.
  40. Process finished with exit code 0

一共查询六次,如果有 多条 ,效率则会大大降低

当使用延迟加载时需要修改配置 系统默认使用积极加载

Mapper.xml配置

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!--接口的全限定名-->
  6. <mapper namespace="com.huangxin.order.mapper.UserMapper">
  7. <resultMap id="user" type="com.huangxin.order.model.User">
  8. <id column="id" property="id"></id>
  9. <result column="user_name" property="userName"></result>
  10. <result column="user_telephone" property="userTelephone"></result>
  11. <result column="user_sex" property="userSex"></result>
  12. <collection property="orders" fetchType="lazy" select="com.huangxin.order.mapper.OrderMapper.orders"
  13. column="id">
  14. </collection>
  15. </resultMap>
  16. <select id="getAll" resultMap="user">
  17. SELECT * FROM user ;
  18. </select>
  19. </mapper>

fetchType="lazy" 这个加在collection标签里

查询多条,接口应使用List来封装多条数据

测试中这样写

  1. package com.huangxin.order.mapper;
  2. import com.huangxin.order.model.Orders;
  3. import com.huangxin.order.model.User;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8. import org.junit.Before;
  9. import org.junit.Test;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.util.List;
  13. public class OrderMapperTest {
  14. private SqlSessionFactory factory;
  15. /**
  16. * 初始化SqlSession工厂
  17. */
  18. @Before
  19. public void init() throws IOException {
  20. //将全局配置文件读取
  21. InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
  22. factory = new SqlSessionFactoryBuilder().build(in);
  23. }
  24. @Test
  25. public void getAll() {
  26. SqlSession sqlSession = factory.openSession();
  27. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  28. List<User> All = mapper.getAll();
  29. for (User user : All) {
  30. System.out.println(user.getUserName());
  31. }
  32. sqlSession.close();
  33. }
  34. }

控制台输出

  1. D:\Java\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:D:\IntelliJ\lib\idea_rt.jar=6743:D:\IntelliJ\bin -Dfile.encoding=UTF-8 -classpath D:\IntelliJ\lib\idea_rt.jar;D:\IntelliJ\plugins\junit\lib\junit-rt.jar;D:\IntelliJ\plugins\junit\lib\junit5-rt.jar;D:\mybatis05\target\test-classes;D:\mybatis05\target\classes;C:\Users\Administrator\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar;C:\Users\Administrator\.m2\repository\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.huangxin.order.mapper.OrderMapperTest,getById
  2. 2019-01-03 10:46:18 [DEBUG] Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
  3. WARNING: An illegal reflective access operation has occurred
  4. WARNING: Illegal reflective access by org.apache.ibatis.reflection.Reflector (file:/C:/Users/Administrator/.m2/repository/org/mybatis/mybatis/3.4.6/mybatis-3.4.6.jar) to method java.lang.Class.checkPackageAccess(java.lang.SecurityManager,java.lang.ClassLoader,boolean)
  5. WARNING: Please consider reporting this to the maintainers of org.apache.ibatis.reflection.Reflector
  6. WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
  7. WARNING: All illegal access operations will be denied in a future release
  8. 2019-01-03 10:46:18 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  9. 2019-01-03 10:46:18 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  10. 2019-01-03 10:46:18 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  11. 2019-01-03 10:46:18 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  12. 2019-01-03 10:46:18 [DEBUG] Opening JDBC Connection
  13. 2019-01-03 10:46:18 [DEBUG] Created connection 37981645.
  14. 2019-01-03 10:46:18 [DEBUG] Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@2438dcd]
  15. 2019-01-03 10:46:18 [DEBUG] > Preparing: SELECT * FROM user ;
  16. 2019-01-03 10:46:18 [DEBUG] > Parameters:
  17. 2019-01-03 10:46:19 [DEBUG] < Total: 6
  18. 黄鑫
  19. 张三
  20. 李四
  21. 王麻子
  22. 小红
  23. 小明
  24. 2019-01-03 10:46:19 [DEBUG] Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@2438dcd]
  25. 2019-01-03 10:46:19 [DEBUG] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@2438dcd]
  26. 2019-01-03 10:46:19 [DEBUG] Returned connection 37981645 to pool.
  27. Process finished with exit code 0

这样只会查询需要的数据

如果测试这样写

  1. package com.huangxin.order.mapper;
  2. import com.huangxin.order.model.Orders;
  3. import com.huangxin.order.model.User;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8. import org.junit.Before;
  9. import org.junit.Test;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.util.List;
  13. public class OrderMapperTest {
  14. private SqlSessionFactory factory;
  15. /**
  16. * 初始化SqlSession工厂
  17. */
  18. @Before
  19. public void init() throws IOException {
  20. //将全局配置文件读取
  21. InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
  22. factory = new SqlSessionFactoryBuilder().build(in);
  23. }
  24. @Test
  25. public void getAll() {
  26. SqlSession sqlSession = factory.openSession();
  27. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
  28. List<User> All = mapper.getAll();
  29. for (User user : All) {
  30. System.out.println(user.getUserName());
  31. List<Orders> orders = user.getOrders();
  32. System.out.println(orders);
  33. }
  34. sqlSession.close();
  35. }
  36. }

这样他就输出另一张表的数据

  1. D:\Java\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:D:\IntelliJ\lib\idea_rt.jar=6891:D:\IntelliJ\bin -Dfile.encoding=UTF-8 -classpath D:\IntelliJ\lib\idea_rt.jar;D:\IntelliJ\plugins\junit\lib\junit-rt.jar;D:\IntelliJ\plugins\junit\lib\junit5-rt.jar;D:\mybatis05\target\test-classes;D:\mybatis05\target\classes;C:\Users\Administrator\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar;C:\Users\Administrator\.m2\repository\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.huangxin.order.mapper.OrderMapperTest,getById
  2. 2019-01-03 10:52:03 [DEBUG] Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
  3. WARNING: An illegal reflective access operation has occurred
  4. WARNING: Illegal reflective access by org.apache.ibatis.reflection.Reflector (file:/C:/Users/Administrator/.m2/repository/org/mybatis/mybatis/3.4.6/mybatis-3.4.6.jar) to method java.lang.Class.checkPackageAccess(java.lang.SecurityManager,java.lang.ClassLoader,boolean)
  5. WARNING: Please consider reporting this to the maintainers of org.apache.ibatis.reflection.Reflector
  6. WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
  7. WARNING: All illegal access operations will be denied in a future release
  8. 2019-01-03 10:52:03 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  9. 2019-01-03 10:52:03 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  10. 2019-01-03 10:52:03 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  11. 2019-01-03 10:52:03 [DEBUG] PooledDataSource forcefully closed/removed all connections.
  12. 2019-01-03 10:52:03 [DEBUG] Opening JDBC Connection
  13. 2019-01-03 10:52:03 [DEBUG] Created connection 1048098469.
  14. 2019-01-03 10:52:03 [DEBUG] Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@3e78b6a5]
  15. 2019-01-03 10:52:03 [DEBUG] > Preparing: SELECT * FROM user ;
  16. 2019-01-03 10:52:03 [DEBUG] > Parameters:
  17. 2019-01-03 10:52:03 [DEBUG] < Total: 6
  18. 黄鑫
  19. 2019-01-03 10:52:03 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
  20. 2019-01-03 10:52:03 [DEBUG] > Parameters: 1(Integer)
  21. 2019-01-03 10:52:03 [DEBUG] < Total: 2
  22. [Orders{id=1, userId='1', createTime=Tue Jan 01 19:49:58 CST 2019}, Orders{id=2, userId='1', createTime=Tue Jan 01 19:50:46 CST 2019}]
  23. [Orders{id=1, userId='1', createTime=Tue Jan 01 19:49:58 CST 2019}, Orders{id=2, userId='1', createTime=Tue Jan 01 19:50:46 CST 2019}]
  24. 张三
  25. 2019-01-03 10:52:03 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
  26. 2019-01-03 10:52:03 [DEBUG] > Parameters: 2(Integer)
  27. 2019-01-03 10:52:03 [DEBUG] < Total: 1
  28. [Orders{id=3, userId='2', createTime=Tue Jan 01 19:50:52 CST 2019}]
  29. 李四
  30. 2019-01-03 10:52:03 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
  31. 2019-01-03 10:52:03 [DEBUG] > Parameters: 3(Integer)
  32. 2019-01-03 10:52:03 [DEBUG] < Total: 1
  33. [Orders{id=4, userId='3', createTime=Tue Jan 01 19:50:55 CST 2019}]
  34. 王麻子
  35. 2019-01-03 10:52:03 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
  36. 2019-01-03 10:52:03 [DEBUG] > Parameters: 4(Integer)
  37. 2019-01-03 10:52:03 [DEBUG] < Total: 2
  38. [Orders{id=5, userId='4', createTime=Tue Jan 01 19:50:57 CST 2019}, Orders{id=6, userId='4', createTime=Tue Jan 01 19:51:00 CST 2019}]
  39. [Orders{id=5, userId='4', createTime=Tue Jan 01 19:50:57 CST 2019}, Orders{id=6, userId='4', createTime=Tue Jan 01 19:51:00 CST 2019}]
  40. 小红
  41. 2019-01-03 10:52:03 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
  42. 2019-01-03 10:52:03 [DEBUG] > Parameters: 5(Integer)
  43. 2019-01-03 10:52:03 [DEBUG] < Total: 3
  44. [Orders{id=7, userId='5', createTime=Tue Jan 01 19:51:03 CST 2019}, Orders{id=8, userId='5', createTime=Tue Jan 01 19:51:05 CST 2019}, Orders{id=9, userId='5', createTime=Tue Jan 01 19:51:07 CST 2019}]
  45. [Orders{id=7, userId='5', createTime=Tue Jan 01 19:51:03 CST 2019}, Orders{id=8, userId='5', createTime=Tue Jan 01 19:51:05 CST 2019}, Orders{id=9, userId='5', createTime=Tue Jan 01 19:51:07 CST 2019}]
  46. [Orders{id=7, userId='5', createTime=Tue Jan 01 19:51:03 CST 2019}, Orders{id=8, userId='5', createTime=Tue Jan 01 19:51:05 CST 2019}, Orders{id=9, userId='5', createTime=Tue Jan 01 19:51:07 CST 2019}]
  47. 小明
  48. 2019-01-03 10:52:03 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
  49. 2019-01-03 10:52:03 [DEBUG] > Parameters: 6(Integer)
  50. 2019-01-03 10:52:03 [DEBUG] < Total: 1
  51. [Orders{id=10, userId='6', createTime=Tue Jan 01 19:51:09 CST 2019}]
  52. 2019-01-03 10:52:03 [DEBUG] Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@3e78b6a5]
  53. 2019-01-03 10:52:03 [DEBUG] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@3e78b6a5]
  54. 2019-01-03 10:52:03 [DEBUG] Returned connection 1048098469 to pool.
  55. Process finished with exit code 0

按需使用延迟加载

Tags: JavaWeb  

上一篇: mybatis-05-动态SQL

下一篇: mybatis-06-映射关系

评论区

    2019-10-22 19:14:51

    站长

    欢迎各位评论!


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我