延迟加载测试观察延迟加载的方式
1.延迟加载
输出语句为:
System.out.println(account.getAid());
测试结果
可以看出,只需要获取id,所以就不用再获取account的user属性
通过日志可以看出只会有一个查询语句
select * from account
2.非延迟加载(积极加载)
会有两条查询语句
Mybatis的延迟加载,两种配置方式:
注意:
如果配置了全局为延迟加载,在映射文件中配置了积极加载,那么改映射文件配置的那个方法,将会积极加载。
1.在核心配置文件中配置(全局配置)
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
2.在映射文件中配置(局部配置)
<association fetchType="lazy" property="user" javaType="user"
column="userId" select="com.william.dao.UserDao.findId"></association>
一、目录结构
二、maven
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.william</groupId>
<artifactId>Mybatis_Day04_OTO_lazy</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
三、domain实体类
1、 Account
package com.william.domain;
/**
* @author :lijunxuan
* @date :Created in 2020/2/2 20:35
* @description :
* @version: 1.0
*/
public class Account {
private Integer aid;
private String accountname;
private Float money;
private Integer userId;
private User user;
@Override
public String toString() {
return "Account{" +
"aid=" + aid +
", accountname='" + accountname + '\'' +
", money=" + money +
", userId=" + userId +
", user=" + user +
'}';
}
public Integer getAid() {
return aid;
}
public void setAid(Integer aid) {
this.aid = aid;
}
public String getAccountname() {
return accountname;
}
public void setAccountname(String accountname) {
this.accountname = accountname;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
2、User
package com.william.domain;
/**
* @author :lijunxuan
* @date :Created in 2020/2/2 20:37
* @description :
* @version: 1.0
*/
public class User {
private Integer id;
private String username;
private String PASSWORD;
private String sex;
private String birthday;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", PASSWORD='" + PASSWORD + '\'' +
", sex='" + sex + '\'' +
", birthday='" + birthday + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPASSWORD() {
return PASSWORD;
}
public void setPASSWORD(String PASSWORD) {
this.PASSWORD = PASSWORD;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
四、Dao 接口
1、AccountDao
package com.william.dao;
import com.william.domain.Account;
import java.util.List;
/**
* @author :lijunxuan
* @date :Created in 2020/2/3 11:58
* @description :
* @version: 1.0
*/
public interface AccountDao {
List<Account> findAll();
}
2、UserDao
package com.william.dao;
public interface UserDao {
void findId(Integer id);
}
五、映射文件
延迟加载
1、AccountDao.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.william.dao.AccountDao">
<resultMap id="resultMap" type="account">
<id column="aid" property="aid"></id>
<result column="accountname" property="accountname"></result>
<result column="money" property="money"></result>
<result column="userId" property="userId"></result>
<!--association ; 关联 联想 配置一对一映射-->
<!--
property :属性名
javaType : 属性的类型
-->
<association fetchType="lazy" property="user" javaType="user" column="userId" select="com.william.dao.UserDao.findId"></association>
</resultMap>
<select id="findAll" resultMap="resultMap">
select * from account
</select>
</mapper>
2、UserDao.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.william.dao.UserDao">
<select id="findId" parameterType="int" resultType="user">
select * from user where id=#{id}
</select>
</mapper>
六、核心配置文件
1、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="db.properties"></properties>
<typeAliases>
<package name="com.william.domain"></package>
</typeAliases>
<environments default="develoment">
<environment id="develoment">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments>
<mappers>
<!--<dao resource="com/william/Mapper.xml"></dao>-->
<package name="com.william.dao"></package>
</mappers>
</configuration>
也可以进行全局配置延迟加载
<?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="db.properties"></properties>
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
</settings>
<typeAliases>
<package name="com.william.domain"></package>
</typeAliases>
<environments default="develoment">
<environment id="develoment">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments>
<mappers>
<!--<dao resource="com/william/Mapper.xml"></dao>-->
<package name="com.william.dao"></package>
</mappers>
</configuration>
七、数库配置文件
1、db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/web02
jdbc.user=root
jdbc.password=root
2、log4j.properties
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE, info
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
八、测试类
1、MybatisTest.java
package com.william;
import com.william.dao.AccountDao;
import com.william.domain.Account;
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.Test;
import java.io.IOException;
import java.util.List;
/**
* @author :lijunxuan
* @date :Created in 2020/2/3 12:18
* @description :
* @version: 1.0
*/
public class MybatisTest {
/**
* 一对一映射
*/
@Test
public void testOTO(){
try {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("Mybatis-config.xml"));
SqlSession sqlSession = sqlSessionFactory.openSession(true);
AccountDao mapper = sqlSession.getMapper(AccountDao.class);
List<Account> list = mapper.findAll();
for (Account account : list) {
System.out.println(account.getAid());
}
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
九、懒加载测试结果
来源:CSDN
作者:L-李俊漩
链接:https://blog.csdn.net/sinat_39179993/article/details/104158768