1、问题提出
在之前的文章一对多中,当我们有一个用户,他又一百个账户。
(1)在查询用户时,要不要把关联的账户信息查询出来?
(2)在查询账户时,要不要把关联的用户信息查询出来?
- 对于第一个问题,我们应该是什么时候需要查询账户信息,才查询。没必要每次查询用户信息都把账户信息也查询出来。因为如果每次都查询出账户信息,对我们的内存开销是很大的,而且每次查询也都不一定用到账户信息。
- 对于第二个问题,我们就可以在查询账户信息的时候查询出关联的用户信息,因为我们如果只是单纯的账户信息没有说明用户是谁,这对于我们来说是不友好的。也没什么意义,所以在每次查询账户信息的时候都要显示出关联的用户信息。
2、延迟加载和立即加载
第一个问题其实就是延迟加载。
第二个问题就是立即加载。
延迟加载
在真正的使用数据时才发起查询,不用的时候不查。按需加载(懒加载)。
立即加载
不管用不用,只要一调用方法,马上发起查询。
3、四种对应的表关系中的加载
- 四种表 关系:一对一,一对多,多对一,多对多。
- 一对多、多对多:通常情况下,我们都是采用延迟加载。
- 多对一、一对一:通常情况下,我们都是采用立即加载。
4、实现一对一的延迟加载
这里的项目结构和https://blog.csdn.net/weixin_44588495/article/details/91556447里面的大部分内容是一样的,我们只需要更改一些配置信息。
- 更改IAccountDao.xml的配置信息
这里将association标签中的内容全部删除,添加了一个select属性,select属性指向的是IUserDao接口的findById方法,也就是说执行这个sql语句后,就会执行findById这个方法,其中入口参数就是column中的uid这个值。
<?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.mybatis.dao.IAccountDao">
<!-- 定义封装account和user的resultMap-->
<resultMap id="accountUserMap" type="com.mybatis.domain.Account">
<id property="id" column="id"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!-- 一对一关系映射,配置封装user的内容
select 属性指定的内容,查询用户的唯一标识
column 属性指定的内容,用户根据id查询时,所需要的参数的值
-->
<association property="user" column="uid" javaType="com.mybatis.domain.User" select="com.mybatis.dao.IUserDao.findById"></association>
</resultMap>
<!-- 查询所有-->
<select id="findAll" resultMap="accountUserMap">
select * from account
</select>
</mapper>
- 更改SqlMapConfig.xml中的信息
在这里添加两条setting标签配置信息。根据mybatis的文档
http://www.mybatis.org/mybatis-3/zh/configuration.html#settings
需要设置这两个属性。才能开启延迟加载。
<?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">
<!--mybatis主配置文件-->
<configuration>
<!-- 配置参数-->
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<!--配置环境-->
<environments default="mysql">
<!-- 配置mysql环境-->
<environment id="mysql">
<!-- 配置事务类型-->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据源(连接池)-->
<dataSource type="POOLED">
<!-- 配置数据库的基本信息-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="111111"/>
</dataSource>
</environment>
</environments>
<!-- 指定映射配置文件的位置,映射配置文件指的是每一个dao独立的配置文件-->
<mappers>
<package name="com.mybatis.dao"/>
</mappers>
</configuration>
运行结果
未调用获取账户信息的方法。
- 在MybatisAccountTest中调用
public void testFindAll() throws Exception{
List<Account> accounts = accountDao.findAll();
for (Account a:accounts){
System.out.println("-----------------------");
System.out.println(a );
}
}
调用获取信息的方法
- 在MybatisAccountTest中调用
public void testFindAll() throws Exception{
List<Account> accounts = accountDao.findAll();
for (Account a:accounts){
System.out.println("-----------------------");
System.out.println(a );
System.out.println(a.getUser());
}
}
5、实现一对多的延迟加载
这里的项目结构和https://blog.csdn.net/weixin_44588495/article/details/91556447里面的大部分内容是一样的,我们只需要更改一些配置信息。这里面的道理和一对一是一样的。只不过改的是collection标签内的属性。
- 更改IUserDao.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.mybatis.dao.IUserDao">
<!-- 定义User的resultMap-->
<resultMap id="userAccountMap" type="com.mybatis.domain.User">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<collection property="accounts" ofType="com.mybatis.domain.Account" select="com.mybatis.dao.IAccountDao.findAccountByUid" column="id"></collection>
</resultMap>
<!-- 查询所有-->
<select id="findAll" resultMap="userAccountMap">
select * from users
</select>
<select id="findById" parameterType="INT" resultType="com.mybatis.domain.User">
select * from users where id = #{uid}
</select>
</mapper>
IAccountDao接口:这里多增加了一个findAccountByUid方法
package com.mybatis.dao;
import com.mybatis.domain.Account;
import java.util.List;
public interface IAccountDao {
/**
* 查询所有的Account
* @return
*/
List<Account> findAll();
List<Account> findAccountByUid(Integer uid);
}
IAccountDao.xml中需要增加一条方法配置信息
<select id="findAccountByUid" resultType="com.mybatis.domain.Account" parameterType="Integer">
select * from account where uid = #{uid}
</select>
SqMapConfig.xml文件和一对一是一样的。
运行结果:
未调用获取账户信息的方法。
- 在MybatisTest中调用
public void testFindAll() throws Exception{
List<User> users = userDao.findAll();
for (User user:users){
System.out.println("--------------------------------------");
System.out.println(user);
}
}
调用获取信息的方法
- 在MybatisTest中调用
public void testFindAll() throws Exception{
List<User> users = userDao.findAll();
for (User user:users){
System.out.println("--------------------------------------");
System.out.println(user);
System.out.println(user.getAccounts());
}
}
这样就起到了该调用的时候调用,不用浪费多余的内存空间。