数据库、持久化缓存学习
Mybatis的延迟加载策略
1.Mybatis配置为延迟加载
这里实现一对多的情况
<!--核心配置文件-->
<settings>
<!--开启驼峰命名自动映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--延迟加载全局开关-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--开启时,任一方法的调用都会加载该对象的所有延迟加载属性-->
<setting name="aggressiveLazyLoading" value="true"/>
</settings>
UserDao.xml中用到一对多查询的情况下,配置
<resultMap id="userAccountMap" type="com.example.bean.User">
<!-- 主键字段-->
<id property="id" column="id"></id>
<!-- 非主键字段-->
<result property="name" column="name"></result>
<result property="age" column="age"></result>
<collection property="accounts" ofType="account" select="com.example.dao.AccountDao.findByUid" column="id">
</collection>
</resultMap>
<select id="findAll" resultMap="userAccountMap">
select * from usera
</select>
<select id="findByUid" parameterType="int" resultType="account">
select * from account where uid=#{uid}
</select>
相应的要在主表里增加外表的属性,一对多情况
public class User implements Serializable {
private Integer id;
private String name;
private int age;
// 增加一对多的属性
private List<Account> accounts;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
查询的结果
DEBUG [main] - ==> Preparing: select * from usera
DEBUG [main] - ==> Parameters:
TRACE [main] - <== Columns: id, name, age
TRACE [main] - <== Row: 1, 盖伦, 33
TRACE [main] - <== Row: 2, 嘉文四世, 28
TRACE [main] - <== Row: 3, 拉克丝, 25
TRACE [main] - <== Row: 4, 凯瑟琳, 27
TRACE [main] - <== Row: 5, 劫, 32
TRACE [main] - <== Row: 7, 泰伦, 31
TRACE [main] - <== Row: 8, 亚索, 30
TRACE [main] - <== Row: 9, 永恩, 30
DEBUG [main] - <== Total: 8
DEBUG [main] - ==> Preparing: select * from account where uid=?
DEBUG [main] - ==> Parameters: 1(Integer)
TRACE [main] - <== Columns: id, uid, money
TRACE [main] - <== Row: 1, 1, 2345.0
TRACE [main] - <== Row: 2, 1, 2325.0
DEBUG [main] - <== Total: 2
User{id=1, name='盖伦', age=33}
[Account{id=1, uid=1, money=2345.0}, Account{id=2, uid=1, money=2325.0}]
DEBUG [main] - ==> Preparing: select * from account where uid=?
DEBUG [main] - ==> Parameters: 2(Integer)
TRACE [main] - <== Columns: id, uid, money
TRACE [main] - <== Row: 3, 2, 3325.0
DEBUG [main] - <== Total: 1
User{id=2, name='嘉文四世', age=28}
[Account{id=3, uid=2, money=3325.0}]
如果不打印,就只会查询主表,不会查询用户的相关账户
DEBUG [main] - ==> Preparing: select * from usera
DEBUG [main] - ==> Parameters:
TRACE [main] - <== Columns: id, name, age
TRACE [main] - <== Row: 1, 盖伦, 33
TRACE [main] - <== Row: 2, 嘉文四世, 28
TRACE [main] - <== Row: 3, 拉克丝, 25
TRACE [main] - <== Row: 4, 凯瑟琳, 27
TRACE [main] - <== Row: 5, 劫, 32
TRACE [main] - <== Row: 7, 泰伦, 31
TRACE [main] - <== Row: 8, 亚索, 30
TRACE [main] - <== Row: 9, 永恩, 30
DEBUG [main] - <== Total: 8
Process finished with exit code 0
Mybatis的缓存
就是临时存在于内存的临时数据,为了减少与数据库的交互次数,提高执行空间。
一级缓存:指存在SqlSession对象的缓存,当SqlSession对象被回收时,一级缓存也消失了。
当调用SqlSession的update,insert,delete,commit,close等方法时,就会清空一级缓存
二级缓存:指mybatis中SqlSessionfactory对象的缓存,由同一个SqlSessionfactory创建的SqlSession共享同一个二级缓存内容。
二级缓存的使用步骤:
1.让Mybatis框架支持二级缓存,在SqlMapconfig.xml中配置
<setting name="cacheEnabled" value="true"/>
2.让当前的映射文件支持二级缓存,在UserDao.xml中配置
<cache/>
3.让当前的操作支持二级缓存,在select标签中配置
<select id="findById" parameterType="java.lang.Integer" resultType="com.example.bean.User" useCache="true">
select * from usera where id=#{id}
</select>
二级缓存中存放的是数据而不是对象。
测试结果:
DEBUG [main] - Cache Hit Ratio [com.example.dao.UserDao]: 0.0
DEBUG [main] - ==> Preparing: select * from usera where id=?
DEBUG [main] - ==> Parameters: 1(Integer)
TRACE [main] - <== Columns: id, name, age
TRACE [main] - <== Row: 1, 盖伦, 33
DEBUG [main] - <== Total: 1
User{id=1, name='盖伦', age=33}
DEBUG [main] - Cache Hit Ratio [com.example.dao.UserDao]: 0.0
User{id=1, name='盖伦', age=33}
Process finished with exit code 0
可以看到有非常明显的效果。
Hibernate框架缓存
一级缓存的概念和Mybatis的一级缓存的概念类似,也是存在于Session中的临时缓存。
二级缓存也是存在于SessionFactory中的。
配置二级缓存
具体配置请看:
https://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide.html#caching-config