1. 延遲加載的含義: 用到的時候纔會去進行相關操做java
2. 延遲加載的例子:mysql
2.1 spring的BeanFactory,在getBean()的時候才建立Beanspring
2.2 物理分頁查詢,只有點擊某一頁時採起數據庫查詢該頁的數據sql
3. Mybatis啓用延遲加載機制數據庫
<setting name="lazyLoadingEnabled" value="true"/>
4. Mybatis延遲加載的策略:apache
侵入式延遲加載機制【若是隻查詢主表數據而不進行使用,級聯表的數據不會被查詢;若是使用了主表數據,即便級聯表的數據沒有使用,也會查詢】服務器
4.1 實例一:只查詢主表數據而不進行使用session
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 日誌mybatis
[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 實例二:查詢並使用主表數據app
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