Mybatis之延迟加载机制
1. 延迟加载的含义: 用到的时候才会去进行相关操作
2. 延迟加载的例子:
2.1 spring的BeanFactory,在getBean()的时候才创建Bean
2.2 物理分页查询,只有点击某一页时采取数据库查询该页的数据
3. Mybatis启用延迟加载机制
<setting name="lazyLoadingEnabled" value="true"/>
4. Mybatis延迟加载的策略:
侵入式延迟加载机制【如果只查询主表数据而不进行使用,级联表的数据不会被查询;如果使用了主表数据,即使级联表的数据没有使用,也会查询】
4.1 实例一:只查询主表数据而不进行使用
package com.blueStarWei.test; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.blueStarWei.entity.TPersonInfo;
import com.blueStarWei.mappers.PersonAddressMapper;
import com.blueStarWei.utils.SqlSessionFactoryUtil; public class TestPersonAddress { public static void main(String[] args) {
SqlSession session = SqlSessionFactoryUtil.openSession();
PersonAddressMapper mapper = session.getMapper(PersonAddressMapper.class); List<TPersonInfo> info = mapper.findAllWithAddress(); }
}
4.1.1 日志
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Opening JDBC Connection
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Created connection 836514715.
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@31dc339b]
[com.blueStarWei.mappers.PersonAddressMapper.findAllWithAddress]-==> Preparing: select * from t_person_info
[com.blueStarWei.mappers.PersonAddressMapper.findAllWithAddress]-==> Parameters:
[com.blueStarWei.mappers.PersonAddressMapper.findAllWithAddress]-<== Total: 1
4.2 实例二:查询并使用主表数据
package com.blueStarWei.test; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.blueStarWei.entity.TPersonInfo;
import com.blueStarWei.mappers.PersonAddressMapper;
import com.blueStarWei.utils.SqlSessionFactoryUtil; public class TestPersonAddress { public static void main(String[] args) {
SqlSession session = SqlSessionFactoryUtil.openSession();
PersonAddressMapper mapper = session.getMapper(PersonAddressMapper.class); List<TPersonInfo> info = mapper.findAllWithAddress(); for (TPersonInfo tPersonInfo : info) {
System.out.println(tPersonInfo.getName());
}
}
}
4.2.2 日志
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Opening JDBC Connection
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Created connection 836514715.
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@31dc339b]
[com.blueStarWei.mappers.PersonAddressMapper.findAllWithAddress]-==> Preparing: select * from t_person_info
[com.blueStarWei.mappers.PersonAddressMapper.findAllWithAddress]-==> Parameters:
[com.blueStarWei.mappers.PersonAddressMapper.findAllWithAddress]-<== Total: 1
[com.blueStarWei.mappers.AddressMapper.findById]-==> Preparing: SELECT * FROM t_address t where t.id = ?
[com.blueStarWei.mappers.AddressMapper.findById]-==> Parameters: 1(Integer)
[com.blueStarWei.mappers.AddressMapper.findById]-<== Total: 1
5. Mybatis禁用侵入式延迟加载【使用到数据才会去查找相关表】
<setting name="aggressiveLazyLoading" value="false"/>
5.1 实例一:只使用主表数据
package com.blueStarWei.test; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.blueStarWei.entity.TPersonInfo;
import com.blueStarWei.mappers.PersonAddressMapper;
import com.blueStarWei.utils.SqlSessionFactoryUtil; public class TestPersonAddress { public static void main(String[] args) {
SqlSession session = SqlSessionFactoryUtil.openSession();
PersonAddressMapper mapper = session.getMapper(PersonAddressMapper.class); List<TPersonInfo> info = mapper.findAllWithAddress(); for (TPersonInfo tPersonInfo : info) {
System.out.println(tPersonInfo.getName());
}
}
}
5.1.1 日志
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Opening JDBC Connection
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Created connection 836514715.
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@31dc339b]
[com.blueStarWei.mappers.PersonAddressMapper.findAllWithAddress]-==> Preparing: select * from t_person_info
[com.blueStarWei.mappers.PersonAddressMapper.findAllWithAddress]-==> Parameters:
[com.blueStarWei.mappers.PersonAddressMapper.findAllWithAddress]-<== Total: 1
5.2 实例二:使用级联表数据
package com.blueStarWei.test; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.blueStarWei.entity.TPersonInfo;
import com.blueStarWei.mappers.PersonAddressMapper;
import com.blueStarWei.utils.SqlSessionFactoryUtil; public class TestPersonAddress { public static void main(String[] args) {
SqlSession session = SqlSessionFactoryUtil.openSession();
PersonAddressMapper mapper = session.getMapper(PersonAddressMapper.class); List<TPersonInfo> info = mapper.findAllWithAddress(); for (TPersonInfo tPersonInfo : info) {
System.out.println(tPersonInfo.getAddress());
}
}
}
5.2.1 日志
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Opening JDBC Connection
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Created connection 836514715.
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@31dc339b]
[com.blueStarWei.mappers.PersonAddressMapper.findAllWithAddress]-==> Preparing: select * from t_person_info
[com.blueStarWei.mappers.PersonAddressMapper.findAllWithAddress]-==> Parameters:
[com.blueStarWei.mappers.PersonAddressMapper.findAllWithAddress]-<== Total: 1
[com.blueStarWei.mappers.AddressMapper.findById]-==> Preparing: SELECT * FROM t_address t where t.id = ?
[com.blueStarWei.mappers.AddressMapper.findById]-==> Parameters: 1(Integer)
[com.blueStarWei.mappers.AddressMapper.findById]-<== Total: 1
6. 补充
6.1 启用log4j打印日志
导入log4j.jar, 将log4j.properties文件与ybatis-config.xml放到一起【会自动加载,无需配置】
7.附录:
7.1 建表语句【Mysql】
CREATE TABLE `t_person_info` (
`id` int(2) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`addressId` int(3) DEFAULT NULL,
PRIMARY KEY (`id`)
) CREATE TABLE `t_address` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country` varchar(10) DEFAULT NULL,
`city` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`)
)
7.2 mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"/> <settings>
<!-- 延迟加载的全局开关。当开启时,所有的关联对象会延迟加载 。默认false-->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 禁用侵入式延迟加载 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings> <!-- 类型别名:给Java类起一个简单的名字 -->
<typeAliases>
<package name="com.blueStarWei.entity"/>
</typeAliases> <!-- 环境信息 -->
<environments default="development">
<environment id="development">
<!-- myBatis支持两种事务管理:JDBC和MANAGED(托管)
MANAGED一般商业服务器才有此功能,例如JBOSS-->
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments> <mappers>
<package name="com.blueStarWei.mappers"/>
</mappers>
</configuration>
7.3 TpersonInfo.java
package com.blueStarWei.entity; public class TPersonInfo { private Integer id;
private String name;
private Integer age;
private Address address; public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
} public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
} }
7.4 Addreess.java
package com.blueStarWei.entity; public class Address { private int id;
private String country;
private String city; public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Address [country=" + country + ", city=" + city + "]";
} }
7.5 log4j.properties
log4j.rootLogger=debug,console log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
7.6 mybatis相关架包:
https://pan.baidu.com/s/1248sNRHReiK8S3pENKfmlA 密码:xpe7
Mybatis之延迟加载机制的更多相关文章
- mybatis源代码分析:mybatis延迟加载机制改进
在上一篇博客<mybatis源代码分析:深入了解mybatis延迟加载机制>讲诉了mybatis延迟加载的具体机制及实现原理. 可以看出,如果查询结果对象中有一个属性是需要延迟加载的,那整 ...
- MyBatis的延迟加载和缓存机制
延迟加载: 什么是延迟加载: MyBatis中的延迟加载,也称为懒加载,是指在进行关联查询时,按照设置延迟规则推迟对关联对象的select查询.延迟加载可以有效的减少数据库压力. MyBatis根据对 ...
- Mybatis的延迟加载和缓存
1. MyBatis中的延迟加载,也称为懒加载,是指在进行关联查询时,按照设置延迟加载规则推迟对关联对象的select查询.延迟加载可以有效的减少数据库压力. 注意:MyBatis的延迟加 ...
- Mybatis 测试延迟加载
在学习mybatis的延迟加载时,对 lazyLoadingEnabled 和 aggressiveLazyLoading 的区别并不理解,特别是对查询的条件不同时,执行的查询语句也不一样,所以还是测 ...
- mybatis的缓存机制及用例介绍
在实际的项目开发中,通常对数据库的查询性能要求很高,而mybatis提供了查询缓存来缓存数据,从而达到提高查询性能的要求. mybatis的查询缓存分为一级缓存和二级缓存,一级缓存是SqlSessio ...
- Hibernate延迟加载机制
延迟加载: 延迟加载机制是为了避免一些无谓的性能开销而提出来的,所谓延迟加载就是当在真正需要数据的时候,才真正执行数据加载操作.在Hibernate中提供了对实体对象的延迟加载以及对集合的延迟加载,另 ...
- Hibernate延迟加载机制详解
摘自 http://blog.chinaunix.net/uid-20577907-id-3129234.html 1 延迟加载: 延迟加载机制是为了避免一些无谓的性能开销而提出来的,所谓延迟加载就是 ...
- [转]NHibernate之旅(12):初探延迟加载机制
本节内容 引入 延迟加载 实例分析 1.一对多关系实例 2.多对多关系实例 结语 引入 通过前面文章的分析,我们知道了如何使用NHibernate,比如CRUD操作.事务.一对多.多对多映射等问题,这 ...
- 《深入理解mybatis原理》 MyBatis事务管理机制
MyBatis作为Java语言的数据库框架,对数据库的事务管理是其很重要的一个方面.本文将讲述MyBatis的事务管理的实现机制. 首先介绍MyBatis的事务Transaction的接口设计以及其不 ...
随机推荐
- python自动化-unittest批量执行用例(discover)
前言 我们在写用例的时候,单个脚本的用例好执行,那么多个脚本的时候,如何批量执行呢?这时候就需要用到unittet里面的discover方法来加载用例了. 加载用例后,用unittest里面的Text ...
- 【linux】ssh无法root免密解决
在设置了root的私钥和公钥,添加authorized_keys,修改文件权限为600后,用root账号 ssh localhost结果失败了,还是要密码. 解决: vim /etc/ssh/sshd ...
- 【sqli-labs】Less1~Less4
学习sql注入啦,一下都是我做sqli-labs时的笔记.可能有错误,如果有人发现了欢迎指正~~ 常用知识点: 1.mysql注释有三种:① #: 注释从#到行尾 ② --空格: 注释到行尾,注意-- ...
- python网络爬虫笔记(八)
一.pthon 序列化json格式 1.将python内置对象转换成json 模块,dumps()方法返回的是一个str,内容是标准的JSON,dump()方法可以直接吧JSON写入一个file-li ...
- Duplicate 复制数据库 搭建Dataguard
1 操作系统环境 此处隐藏具体信息 System IP-address db_name db_version Comment Target DB Auxiliary D ...
- 外部引入的js 判断js脚本加载是否完成,完成后执行 相应的动作(以引入百度地图js为例)
可以使用JQuery的 $.getScript(url,function(){});方法 $.getScript("http://api.map.baidu.com/getscript?v= ...
- 开启或停止website
1.添加:Microsoft.Web.Administration 2.代码: static void Main(string[] args) { var server = new ServerMan ...
- django linux环境部署
一.操作环境: 1操作系统:Ctrip-CentOS-7.1-x86_64-R3 Python版本:2.7.5 Django版本: Django 1.8.19 (LTS) 二.部署流程 pip ins ...
- 最新版谷歌浏览器的Flash设置已经不能保存了?
解决方法:先去chrome实验室界面chrome://flags/#enable-ephemeral-flash-permission选择取消Disabled.取消该实验室选项. 然后去chrome: ...
- Choosing web framework: ASP.NET MVC vs Django Python vs Ruby on Rails(转载)
来源:http://podlipensky.com/2012/06/choosing-web-framework-asp-net-mvc-vs-django-python-vs-ruby-on-rai ...