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