我是靠谱客的博主 优秀红牛,最近开发中收集的这篇文章主要介绍mybaits延迟加载原理_Mybatis之延迟加载机制,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.  延迟加载的含义: 用到的时候才会去进行相关操作

2.延迟加载的例子:

2.1 spring的BeanFactory,在getBean()的时候才创建Bean

2.2 物理分页查询,只有点击某一页时采取数据库查询该页的数据

3. Mybatis启用延迟加载机制

4. Mybatis延迟加载的策略:

侵入式延迟加载机制【如果只查询主表数据而不进行使用,级联表的数据不会被查询;如果使用了主表数据,即使级联表的数据没有使用,也会查询】

4.1 实例一:只查询主表数据而不进行使用

packagecom.blueStarWei.test;importjava.util.List;importorg.apache.ibatis.session.SqlSession;importcom.blueStarWei.entity.TPersonInfo;importcom.blueStarWei.mappers.PersonAddressMapper;importcom.blueStarWei.utils.SqlSessionFactoryUtil;public classTestPersonAddress {public static voidmain(String[] args) {

SqlSession session=SqlSessionFactoryUtil.openSession();

PersonAddressMapper mapper= session.getMapper(PersonAddressMapper.class);

List 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 falseon 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 实例二:查询并使用主表数据

packagecom.blueStarWei.test;importjava.util.List;importorg.apache.ibatis.session.SqlSession;importcom.blueStarWei.entity.TPersonInfo;importcom.blueStarWei.mappers.PersonAddressMapper;importcom.blueStarWei.utils.SqlSessionFactoryUtil;public classTestPersonAddress {public static voidmain(String[] args) {

SqlSession session=SqlSessionFactoryUtil.openSession();

PersonAddressMapper mapper= session.getMapper(PersonAddressMapper.class);

List 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 falseon 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禁用侵入式延迟加载【使用到数据才会去查找相关表】

5.1 实例一:只使用主表数据

packagecom.blueStarWei.test;importjava.util.List;importorg.apache.ibatis.session.SqlSession;importcom.blueStarWei.entity.TPersonInfo;importcom.blueStarWei.mappers.PersonAddressMapper;importcom.blueStarWei.utils.SqlSessionFactoryUtil;public classTestPersonAddress {public static voidmain(String[] args) {

SqlSession session=SqlSessionFactoryUtil.openSession();

PersonAddressMapper mapper= session.getMapper(PersonAddressMapper.class);

List 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 falseon 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 实例二:使用级联表数据

packagecom.blueStarWei.test;importjava.util.List;importorg.apache.ibatis.session.SqlSession;importcom.blueStarWei.entity.TPersonInfo;importcom.blueStarWei.mappers.PersonAddressMapper;importcom.blueStarWei.utils.SqlSessionFactoryUtil;public classTestPersonAddress {public static voidmain(String[] args) {

SqlSession session=SqlSessionFactoryUtil.openSession();

PersonAddressMapper mapper= session.getMapper(PersonAddressMapper.class);

List 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 falseon 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 NULLAUTO_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 NULLAUTO_INCREMENT,

`country`varchar(10) DEFAULT NULL,

`city`varchar(10) DEFAULT NULL,PRIMARY KEY(`id`)

)

7.2 mybatis-config.xml

/p>

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

7.3 TpersonInfo.java

packagecom.blueStarWei.entity;public classTPersonInfo {privateInteger id;privateString name;privateInteger age;privateAddress address;publicInteger getId() {returnid;

}public voidsetId(Integer id) {this.id =id;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicInteger getAge() {returnage;

}public voidsetAge(Integer age) {this.age =age;

}publicAddress getAddress() {returnaddress;

}public voidsetAddress(Address address) {this.address =address;

}

}

7.4 Addreess.java

packagecom.blueStarWei.entity;public classAddress {private intid;privateString country;privateString city;publicString getCountry() {returncountry;

}public voidsetCountry(String country) {this.country =country;

}publicString getCity() {returncity;

}public voidsetCity(String city) {this.city =city;

}public intgetId() {returnid;

}public void setId(intid) {this.id =id;

}

@OverridepublicString 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

最后

以上就是优秀红牛为你收集整理的mybaits延迟加载原理_Mybatis之延迟加载机制的全部内容,希望文章能够帮你解决mybaits延迟加载原理_Mybatis之延迟加载机制所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部