Mybatis的延迟加载:
* 在实际开发中,在查询账户时,账户的所属用户信息应该是随着账户时一起查询出来。[立即加载]
* 查询用户时,账户的信息是随着用户的需要才加载出来。 [延迟加载/按需加载/懒加载]
隐患:List集合的元素个数无法预知,可能会因为数据量过大导致内存溢出。
概念:
1.延迟加载:在真正使用数据时才发起查询,不用的时候不查询。按需加载(懒加载)。在查询时,调用对方配置文件中的查询方法进行对方的查询。
2.立即加载:不管用不用,只要一调用方法,马上发起查询。
时机:
在对应的四种表关系中:一对多,多对一,一对一,多对多
* 一对多,多对多,通常情况下是延迟加载。
* 多对一,一对一,通常情况下是立即加载。
好处与坏处
* 好处:先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表速度要快。
* 坏处:因为只有当需要需要用到数据时,才会进行数据库查询,这样在大批量数据查询时,因为查询工作也要消耗时间,所以造成用户等待时间变长,造成用户体验下降。
在SqlMapConfig.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-->
<properties resource="jdbcConfig.properties"></properties>
<!--配置参数-->
<settings>
<!--开启Mybatis支持延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--aggressiveLazyLoading为true则每次调用domain中的get或set或任何方法时,都会自动全部加载,false为按需加载-->
<setting name="aggressiveLazyLoading" value="false"></setting>
</settings>
<!--使用typeAliases配置别名,它只能配置domain中类的别名 -->
<typeAliases>
<package name="com.itheima.domain"></package>
</typeAliases>
<!--配置环境-->
<environments default="mysql">
<!-- 配置mysql的环境-->
<environment id="mysql">
<!-- 配置事务 -->
<transactionManager type="JDBC"></transactionManager>
<!--配置连接池-->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments>
<!-- 配置映射文件的位置 -->
<mappers>
<package name="com.itheima.dao"></package>
</mappers>
</configuration>
配置association
<?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.itheima.dao.IAccountDao">
<!-- 定义封装account和user的resultMap -->
<resultMap id="accountMap" type="account">
<id property="id" column="aid"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!-- 一对一的关系映射:配置封装user的内容-->
<association property="user" column="uid" javaType="user" select="com.itheima.dao.IUserDao.findById"></association>
</resultMap>
<select id="findAll" resultMap="accountMap">
SELECT * FROM account
</select>
<select id="findByUid" resultType="account" parameterType="Integer">
SELECT * FROM account WHERE uid = #{id};
</select>
</mapper>
配置collection
<?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.itheima.dao.IUserDao">
<!-- 定义User的resultMap-->
<resultMap id="userMap" type="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>
<!-- 配置user对象中accounts集合的映射 -->
<collection property="accounts" ofType="account" column="id" select="com.itheima.dao.IAccountDao.findByUid"></collection>
</resultMap>
<select id="findAll" resultMap="userMap">
SELECT * FROM user
</select>
<select id="findById" resultType="user">
SELECT * FROM user where id = #{uid};
</select>
</mapper>
select属性指定的内容:查询用户的唯一标识:
column属性指定的内容:用户根据id查询时,所需要的参数的值,由当前查询的数据提供。给#{id}赋值的
在association和collection中,可以使用以下配置覆盖懒加载的全局开关
fetchType可选的。有效值为lazy和eager。如果使用了,它将取代全局配置参数lazyLoadingEnabled
collection一般用延迟加载fetchType="lazy"
<collection fetchType="lazy" property="accounts" ofType="account" select="com.itheima.dao.IAccountDao.findAccountByUid" column="id"></collection>
Association一般用直接加载fetchType="eager"
<association fetchType="eager" property="user" column="uid" javaType="user" select="com.itheima.dao.IUserDao.findById"></association>
在注解开发中:
直接在注解中设置fetchType = FetchType.LAZY / fetchType= FetchType.EAGER 代表在一对多或多对一时是否开启延迟加载。
@Result(property = "users", column = "id", javaType = List.class, many = @Many(select = "com.itheima.dao.IUserDao.findById", fetchType = FetchType.LAZY))