您现在的位置是:首页 > 个人日记个人日记
mybatis-08-延迟加载
2019-02-03 18:17:16【个人日记】385人已围观
简介说简单就是需要的时候查,不需要就不查
延迟加载
collection便签中的fecthType属性
eager表示积极加载
lazy表示延迟加载(需要时加载,不需要时不加载)
举例:User和Orders关系,当使用toString()方法时,将会打印Orders信息,如果只是单独调用User的信息,则Orders不会被查询调用
当使用积极加载时
控制台输出
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
2019-01-03 10:38:47 [DEBUG] Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
WARNING: An illegal reflective access operation has occurred
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)
WARNING: Please consider reporting this to the maintainers of org.apache.ibatis.reflection.Reflector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2019-01-03 10:38:47 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-03 10:38:47 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-03 10:38:47 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-03 10:38:47 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-03 10:38:47 [DEBUG] Opening JDBC Connection
2019-01-03 10:38:47 [DEBUG] Created connection 2037764568.
2019-01-03 10:38:47 [DEBUG] Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@7975d1d8]
2019-01-03 10:38:47 [DEBUG] > Preparing: SELECT * FROM user ;
2019-01-03 10:38:47 [DEBUG] > Parameters:
2019-01-03 10:38:47 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
2019-01-03 10:38:47 [DEBUG] > Parameters: 1(Integer)
2019-01-03 10:38:47 [DEBUG] < Total: 2
2019-01-03 10:38:47 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
2019-01-03 10:38:47 [DEBUG] > Parameters: 2(Integer)
2019-01-03 10:38:47 [DEBUG] < Total: 1
2019-01-03 10:38:47 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
2019-01-03 10:38:47 [DEBUG] > Parameters: 3(Integer)
2019-01-03 10:38:47 [DEBUG] < Total: 1
2019-01-03 10:38:47 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
2019-01-03 10:38:47 [DEBUG] > Parameters: 4(Integer)
2019-01-03 10:38:47 [DEBUG] < Total: 2
2019-01-03 10:38:47 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
2019-01-03 10:38:47 [DEBUG] > Parameters: 5(Integer)
2019-01-03 10:38:47 [DEBUG] < Total: 3
2019-01-03 10:38:47 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
2019-01-03 10:38:47 [DEBUG] > Parameters: 6(Integer)
2019-01-03 10:38:47 [DEBUG] < Total: 1
2019-01-03 10:38:47 [DEBUG] < Total: 6
[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}]}]
2019-01-03 10:38:47 [DEBUG] Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@7975d1d8]
2019-01-03 10:38:47 [DEBUG] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@7975d1d8]
2019-01-03 10:38:47 [DEBUG] Returned connection 2037764568 to pool.
Process finished with exit code 0
一共查询六次,如果有 多条 ,效率则会大大降低
当使用延迟加载时需要修改配置 系统默认使用积极加载
Mapper.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.huangxin.order.mapper.UserMapper">
<resultMap id="user" type="com.huangxin.order.model.User">
<id column="id" property="id"></id>
<result column="user_name" property="userName"></result>
<result column="user_telephone" property="userTelephone"></result>
<result column="user_sex" property="userSex"></result>
<collection property="orders" fetchType="lazy" select="com.huangxin.order.mapper.OrderMapper.orders"
column="id">
</collection>
</resultMap>
<select id="getAll" resultMap="user">
SELECT * FROM user ;
</select>
</mapper>
fetchType="lazy" 这个加在collection标签里
查询多条,接口应使用List来封装多条数据
测试中这样写
package com.huangxin.order.mapper;
import com.huangxin.order.model.Orders;
import com.huangxin.order.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class OrderMapperTest {
private SqlSessionFactory factory;
/**
* 初始化SqlSession工厂
*/
@Before
public void init() throws IOException {
//将全局配置文件读取
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
factory = new SqlSessionFactoryBuilder().build(in);
}
@Test
public void getAll() {
SqlSession sqlSession = factory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> All = mapper.getAll();
for (User user : All) {
System.out.println(user.getUserName());
}
sqlSession.close();
}
}
控制台输出
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
2019-01-03 10:46:18 [DEBUG] Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
WARNING: An illegal reflective access operation has occurred
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)
WARNING: Please consider reporting this to the maintainers of org.apache.ibatis.reflection.Reflector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2019-01-03 10:46:18 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-03 10:46:18 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-03 10:46:18 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-03 10:46:18 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-03 10:46:18 [DEBUG] Opening JDBC Connection
2019-01-03 10:46:18 [DEBUG] Created connection 37981645.
2019-01-03 10:46:18 [DEBUG] Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@2438dcd]
2019-01-03 10:46:18 [DEBUG] > Preparing: SELECT * FROM user ;
2019-01-03 10:46:18 [DEBUG] > Parameters:
2019-01-03 10:46:19 [DEBUG] < Total: 6
黄鑫
张三
李四
王麻子
小红
小明
2019-01-03 10:46:19 [DEBUG] Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@2438dcd]
2019-01-03 10:46:19 [DEBUG] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@2438dcd]
2019-01-03 10:46:19 [DEBUG] Returned connection 37981645 to pool.
Process finished with exit code 0
这样只会查询需要的数据
如果测试这样写
package com.huangxin.order.mapper;
import com.huangxin.order.model.Orders;
import com.huangxin.order.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class OrderMapperTest {
private SqlSessionFactory factory;
/**
* 初始化SqlSession工厂
*/
@Before
public void init() throws IOException {
//将全局配置文件读取
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
factory = new SqlSessionFactoryBuilder().build(in);
}
@Test
public void getAll() {
SqlSession sqlSession = factory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> All = mapper.getAll();
for (User user : All) {
System.out.println(user.getUserName());
List<Orders> orders = user.getOrders();
System.out.println(orders);
}
sqlSession.close();
}
}
这样他就输出另一张表的数据
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
2019-01-03 10:52:03 [DEBUG] Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
WARNING: An illegal reflective access operation has occurred
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)
WARNING: Please consider reporting this to the maintainers of org.apache.ibatis.reflection.Reflector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2019-01-03 10:52:03 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-03 10:52:03 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-03 10:52:03 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-03 10:52:03 [DEBUG] PooledDataSource forcefully closed/removed all connections.
2019-01-03 10:52:03 [DEBUG] Opening JDBC Connection
2019-01-03 10:52:03 [DEBUG] Created connection 1048098469.
2019-01-03 10:52:03 [DEBUG] Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@3e78b6a5]
2019-01-03 10:52:03 [DEBUG] > Preparing: SELECT * FROM user ;
2019-01-03 10:52:03 [DEBUG] > Parameters:
2019-01-03 10:52:03 [DEBUG] < Total: 6
黄鑫
2019-01-03 10:52:03 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
2019-01-03 10:52:03 [DEBUG] > Parameters: 1(Integer)
2019-01-03 10:52:03 [DEBUG] < Total: 2
[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}]
[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}]
张三
2019-01-03 10:52:03 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
2019-01-03 10:52:03 [DEBUG] > Parameters: 2(Integer)
2019-01-03 10:52:03 [DEBUG] < Total: 1
[Orders{id=3, userId='2', createTime=Tue Jan 01 19:50:52 CST 2019}]
李四
2019-01-03 10:52:03 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
2019-01-03 10:52:03 [DEBUG] > Parameters: 3(Integer)
2019-01-03 10:52:03 [DEBUG] < Total: 1
[Orders{id=4, userId='3', createTime=Tue Jan 01 19:50:55 CST 2019}]
王麻子
2019-01-03 10:52:03 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
2019-01-03 10:52:03 [DEBUG] > Parameters: 4(Integer)
2019-01-03 10:52:03 [DEBUG] < Total: 2
[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}]
[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}]
小红
2019-01-03 10:52:03 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
2019-01-03 10:52:03 [DEBUG] > Parameters: 5(Integer)
2019-01-03 10:52:03 [DEBUG] < Total: 3
[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}]
[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}]
[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}]
小明
2019-01-03 10:52:03 [DEBUG] > Preparing: SELECT * FROM orders WHERE user_id=?;
2019-01-03 10:52:03 [DEBUG] > Parameters: 6(Integer)
2019-01-03 10:52:03 [DEBUG] < Total: 1
[Orders{id=10, userId='6', createTime=Tue Jan 01 19:51:09 CST 2019}]
2019-01-03 10:52:03 [DEBUG] Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@3e78b6a5]
2019-01-03 10:52:03 [DEBUG] Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@3e78b6a5]
2019-01-03 10:52:03 [DEBUG] Returned connection 1048098469 to pool.
Process finished with exit code 0
按需使用延迟加载
Tags: JavaWeb
上一篇: 网页抓取工具
下一篇: mybatis-03-Mapper代理
相关文章
随机图文
评论区
2024-10-08 03:30:02
站长
没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信