一、一级缓存和二级缓存
作用域 | 默认开启 | 增删改对缓存的影响 | |
---|---|---|---|
一级缓存 | SqlSession | 开启 | 会清空缓存中所有的内容 |
二级缓存 | mapper级别 | 不开启 | 只会清空value的值,不会清空hashmap中的key值 |
二、一级缓存
2.1、判断两次及以上的查询相同,必须满足以上条件:
1)传入的statementId相同
2)查询时要求的结果集中的结果范围相同
3)要执行的sql语句字符串内容相同
4)传递给java.sql.Statement要设置的参数值相同
2.2、验证一级缓存的存在(以User实例为例)
1、测试方法
User selectOneUser(int id);
2、对应的mapper.xml
<select id="selectOneUser" parameterType="int" resultMap="user">
select * from user where id = #{id}
</select>
3、执行selectOneUser方法两次
User user = userMapper.selectOneUser(3);
System.out.println(user);
User user1 = userMapper.selectOneUser(3);
System.out.println(user1);
4、debug日志
17:22:21,720 DEBUG - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
17:22:21,746 DEBUG - PooledDataSource forcefully closed/removed all connections.
17:22:21,746 DEBUG - PooledDataSource forcefully closed/removed all connections.
17:22:21,746 DEBUG - PooledDataSource forcefully closed/removed all connections.
17:22:21,746 DEBUG - PooledDataSource forcefully closed/removed all connections.
17:22:21,955 DEBUG - Opening JDBC Connection
17:22:22,153 DEBUG - Created connection 1861781750.
17:22:22,155 DEBUG - ==> Preparing: select * from user where id = ?
17:22:22,192 DEBUG - ==> Parameters: 3(Integer)
17:22:22,223 DEBUG - <== Total: 1
User{id=3, username='阿帕奇', iMaxEmail='3214215@160.com', telephone='45436547', sex='男', age=34}
User{id=3, username='阿帕奇', iMaxEmail='3214215@160.com', telephone='45436547', sex='男', age=34}
Process finished with exit code 0
注意:我们可以看到sql语句只执行了一次
2.3、增删改对以及缓存的影响
User user = userMapper.selectOneUser(3);
System.out.println(user);
User user2 = new User();
user2.setiMaxEmail("3214215@qq.com");
user2.setSex("女");
user2.setAge(34);
user2.setTelephone("542353");
user2.setUsername("张达");
userMapper.insertUser(user2);
User user1 = userMapper.selectOneUser(3);
System.out.println(user1);
debug日志
17:24:55,559 DEBUG - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
17:24:55,582 DEBUG - PooledDataSource forcefully closed/removed all connections.
17:24:55,583 DEBUG - PooledDataSource forcefully closed/removed all connections.
17:24:55,583 DEBUG - PooledDataSource forcefully closed/removed all connections.
17:24:55,583 DEBUG - PooledDataSource forcefully closed/removed all connections.
17:24:55,735 DEBUG - Opening JDBC Connection
17:24:55,956 DEBUG - Created connection 1861781750.
17:24:55,957 DEBUG - ==> Preparing: select * from user where id = ?
17:24:55,993 DEBUG - ==> Parameters: 3(Integer)
17:24:56,024 DEBUG - <== Total: 1
User{id=3, username='阿帕奇', iMaxEmail='3214215@160.com', telephone='45436547', sex='男', age=34}
17:24:56,024 DEBUG - ==> Preparing: insert into user( id, user_name, sex, imax_email, telephone, age )values ( null, ?, ?, ?, ?, ? );
17:24:56,026 DEBUG - ==> Parameters: 张达(String), 女(String), 3214215@qq.com(String), 542353(String), 34(Integer)
17:24:56,033 DEBUG - <== Updates: 1
17:24:56,036 DEBUG - ==> Preparing: select * from user where id = ?
17:24:56,036 DEBUG - ==> Parameters: 3(Integer)
17:24:56,041 DEBUG - <== Total: 1
User{id=3, username='阿帕奇', iMaxEmail='3214215@160.com', telephone='45436547', sex='男', age=34}
我们可以看到该select语句执行了两次,证明缓存已经被清除了
三、二级缓存
3.1、开启指定mapper的二级缓存
1)将要开启二级缓存的实体类进行序列化
2)在要开启二级缓存的mapper.xml中配置cache标签,如下:
<cache eviction="LRU" flushInterval="100000" readOnly="true" size="1024"/>
3.2、开启全局的二级缓存
在mybatis-config.xml全局配置文件中配置:
<settings>
<!--这个配置使全局的映射器(二级缓存)启用或禁用缓存-->
<setting name="cacheEnabled" value="true" />
.....
</settings>