1.延迟加载
1.Mybatis中的延迟加载
问题:在一对多中,当我们有一个用户 ,它有100个账户。
在查询用户的时候,要不要把关联的账户查出来?
在查询账户的时候,要不要把关联的用户查出来?
答案:在查询用户时,用户下的账户信息是,什么时候使用,什么时候查询的。
在查询账户时,账户的所属用户信息应该是随着账户查询时一起查询出来的。
什么是延迟加载
在真正使用数据时才发起查询,不用的时候不查询。按需加载(懒加载)
什么是立即加载
不管用不用,只要一调用方法,马上发起查询
在对应的四种表关系中:一对一,一对多,多对一,多对多。
一对多,多对多:通常情况下我们都是采用延迟加载。
多对一,一对一:通常情况下我们都是采用立即加载。
注意:其实就是看后面,后面大就延迟加载,否则立即加载
2.一对一延迟加载
1.在SqlMapConfig.xml中配置setting标签
详情看中文官网(http://www.mybatis.org/mybatis-3/zh/configuration.html#settings)
<settings>
<!-- 配置全局缓存-->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="true"/>
</settings>
2.在IAccoutDao.xml中配置association标签
<!--定义封装account和user的resultMap-->
<resultMap id="accountUserMap" type="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="User" select="com.daniel.dao.IUserDao.findById">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="sex" column="sex"></result>
<result property="address" column="address"></result>
<result property="birthday" column="birthday"></result>
</association>
</resultMap>
<select id="findAll" resultMap="accountUserMap">
select * from account
</select>
3.测试类
@Test
public void findAll(){
List<Account> accounts = accoutDao.findAll();
for (Account account:
accounts) {
System.out.println("每一个account的信息");
System.out.println(account);
System.out.println(account.getUser());
}
}
3.一对多延迟加载
和一对一没有太多区别
在IUserDao.xml中配置collection标签
<!--定义封装account和user的resultMap-->
<resultMap id="accountUserMap" type="Account">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="sex" column="sex"></result>
<result property="address" column="address"></result>
<result property="birthday" column="birthday"></result>
<!--一对一的关系映射,配置封装user的内容
select属性的内容,查询用户的唯一标识符
column属性的内容:用户根据id查询时,所需要参数的值-->
<collection property="accounts" ofType="Account" select="top.zoick.dao.IAccoutDao.findAccountByUid" column="id">
<id property="id" column="aid"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
</collection>
</resultMap>
2.缓存
1.什么是缓存
存在于内存中的临时数据。
2.为什么使用缓存
减少和数据库的交互次数,提高执行效率。
3.什么样的数据能使用缓存,什么样的数据不能使用
适用于缓存:
经常查询并且不经常改变的。
数据的正确与否对最终结果影响不大的。
不适用于缓存:
经常改变的数据
数据的正确与否对最终结果影响很大的。
例如:商品的库存,银行的汇率,股市的牌价。
Mybatis中的一级缓存和二级缓存
一级缓存:
它指的是Mybatis中SqlSession对象的缓存。
当我们执行查询之后,查询的结果会同时存入到SqlSession为我们提供一块区域中。
该区域的结构是一个Map。当我们再次查询同样的数据,mybatis会先去sqlsession中
查询是否有,有的话直接拿出来用。
当SqlSession对象消失时,mybatis的一级缓存也就消失了。
一级缓存是SqlSession范围的缓存,当调用SqlSession的修改,添加,删除,commit(),close()等方法时,就会清空一级缓存
二级缓存:
它指的是Mybatis中SqlSessionFactory对象的缓存。由同一个SqlSessionFactory对象创建的SqlSession共享其缓存。
二级缓存的使用步骤:
第一步:让Mybatis框架支持二级缓存(在SqlMapConfig.xml中配置)
<settings>
<setting name="cacheEnabled" value="true"/>
</settings>
第二步:让当前的映射文件支持二级缓存(在IUserDao.xml中配置)
第三步:让当前的操作支持二级缓存(在select标签中配置)
<!--开启user支持二级缓存-->
<cache/>
<!-- 根据id查询用户 注意属性useCache -->
<select id="findById" parameterType="INT" resultType="user" useCache="true">
select * from user where id = #{uid}
</select>
—— 暂无评论 ——