Mybatis-18-Cache in action Mybatis 缓存实战
2016年7月27日大约 8 分钟
Mybatis 缓存实战
数据准备
建表脚本
- db.sql
CREATE DATABASE mybatis
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci;
USE mybatis;
- init.sql
DROP TABLE IF EXISTS `user`;
CREATE TABLE user (
id BIGINT(20) PRIMARY KEY AUTO_INCREMENT NOT NULL
COMMENT '主键, 自增',
username VARCHAR(64) NOT NULL
COMMENT '用户名',
password VARCHAR(128) NOT NULL
COMMENT '密码',
`created_time` timestamp NULL,
`updated_time` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE INDEX `username_UNIQUE` (`username`)
)
COMMENT '用户表';
INSERT INTO `user` (username, password, created_time, updated_time) VALUES (
'ryo', '123456', NOW(), NOW()
);
DROP TABLE IF EXISTS `role`;
CREATE TABLE role (
id BIGINT(20) PRIMARY KEY AUTO_INCREMENT NOT NULL
COMMENT '主键,自增',
name VARCHAR(64) NOT NULL
COMMENT '角色名称',
code VARCHAR(64) NOT NULL
COMMENT '角色代码',
description VARCHAR(128) NULL DEFAULT ''
COMMENT '角色说明',
`created_time` timestamp NULL,
`updated_time` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX `name`(`name`),
UNIQUE INDEX `code_UNIQUE`(`code`)
)
COMMENT '角色表';
INSERT INTO `role` (name, code, description, created_time, updated_time) VALUES (
'管理员', 'admin', '这个系统里天下第一', NOW(), NOW()
);
开启测试
mybatis 配置
- mybatis-config.xml
其他配置省略。
测试一
- 条件
一级缓存开启 + localCacheScope=SESSION
- 测试代码
在同一个 SqlSession 执行两次相同条件的查询。
@Test
public void timesQueryTest() {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setId(1L);
System.out.println("初次 " + userMapper.selectOne(user));
System.out.println("再次 " + userMapper.selectOne(user));
}
- 测试结果
实际上只执行了一次。第二次直接使用缓存的结果。
2018-09-08 13:04:04.109 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.selectOne:145 - ==> Preparing: SELECT ID,USERNAME,PASSWORD,created_time,updated_time FROM uSER WHERE ID = ?
2018-09-08 13:04:04.133 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.selectOne:145 - ==> Parameters: 1(Long)
2018-09-08 13:04:04.150 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.selectOne:145 - Preparing: SELECT ID,USERNAME,PASSWORD,created_time,updated_time FROM uSER WHERE ID = ?
2018-09-08 13:14:29.127 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.selectOne:145 - ==> Parameters: 1(Long)
2018-09-08 13:14:29.143 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.selectOne:145 - Preparing: INSERT INTO uSER ( ID,USERNAME,PASSWORD,created_time,updated_time ) VALUES( ?,?,?,?,? )
2018-09-08 13:14:29.151 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.insert:145 - ==> Parameters: null, new(String), 123456(String), null, null
2018-09-08 13:14:29.157 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.insert:145 - Executing: SELECT LAST_INSERT_ID()
2018-09-08 13:14:29.161 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.insert!selectKey:145 - Preparing: SELECT ID,USERNAME,PASSWORD,created_time,updated_time FROM uSER WHERE ID = ?
2018-09-08 13:14:29.162 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.selectOne:145 - ==> Parameters: 1(Long)
2018-09-08 13:14:29.164 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.selectOne:145 - Preparing: SELECT ID,USERNAME,PASSWORD,created_time,updated_time FROM uSER WHERE ID = ?
2018-09-08 14:12:36.485 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.selectByPrimaryKey:145 - ==> Parameters: 1(Long)
2018-09-08 14:12:36.503 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.selectByPrimaryKey:145 - Preparing: UPDATE uSER SET PASSWORD = ? WHERE ID = ?
2018-09-08 14:12:36.544 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.updateByPrimaryKeySelective:145 - ==> Parameters: new 99507(String), 1(Long)
2018-09-08 14:12:36.548 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.updateByPrimaryKeySelective:145 - Preparing: SELECT ID,USERNAME,PASSWORD,created_time,updated_time FROM uSER WHERE ID = ?
2018-09-08 14:12:36.549 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.selectByPrimaryKey:145 - ==> Parameters: 1(Long)
2018-09-08 14:12:36.550 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.UserMapper.selectByPrimaryKey:145 - 一级缓存 -> 数据库。
## 二级缓存配置
- 全局开启配置
```xml
- 在 xml 文件中配置
在 MyBatis 的映射 XML 中配置 cach e或者 cache-ref 。
or
cache-ref代表引用别的命名空间的Cache配置,两个命名空间的操作使用的是同一个Cache。
实验一
- 场景
两个 session,第一个不提交
- 代码
@Test
public void queryTest() {
final Long roleKey = 1L;
SqlSession firstSqlSession = sqlSessionFactory.openSession(true);
SqlSession secondSqlSession = sqlSessionFactory.openSession(true);
RoleMapper firstMapper = firstSqlSession.getMapper(RoleMapper.class);
RoleMapper secondMapper = secondSqlSession.getMapper(RoleMapper.class);
System.out.println("firstUserMapper 查询:" + firstMapper.selectByPrimaryKey(roleKey));
System.out.println("secondUserMapper 查询:" + secondMapper.selectByPrimaryKey(roleKey));
}
- 日志
查询了两次。第二次查询并没有命中缓存。
2018-09-08 14:29:55.104 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.RoleMapper.selectByPrimaryKey:145 - ==> Preparing: SELECT ID,NAME,CODE,DESCRIPTION,created_time,updated_time FROM rOLE WHERE ID = ?
2018-09-08 14:29:55.125 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.RoleMapper.selectByPrimaryKey:145 - ==> Parameters: 1(Long)
2018-09-08 14:29:55.142 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.RoleMapper.selectByPrimaryKey:145 - Preparing: SELECT ID,NAME,CODE,DESCRIPTION,created_time,updated_time FROM rOLE WHERE ID = ?
2018-09-08 14:29:55.161 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.RoleMapper.selectByPrimaryKey:145 - ==> Parameters: 1(Long)
2018-09-08 14:29:55.163 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.RoleMapper.selectByPrimaryKey:145 - Preparing: SELECT ID,NAME,CODE,DESCRIPTION,created_time,updated_time FROM rOLE WHERE ID = ?
2018-09-08 14:33:27.380 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.RoleMapper.selectByPrimaryKey:145 - ==> Parameters: 1(Long)
2018-09-08 14:33:27.398 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.RoleMapper.selectByPrimaryKey:145 - Preparing: SELECT ID,NAME,CODE,DESCRIPTION,created_time,updated_time FROM rOLE WHERE ID = ?
2018-09-08 14:40:48.734 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.RoleMapper.selectByPrimaryKey:145 - ==> Parameters: 1(Long)
2018-09-08 14:40:48.750 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.RoleMapper.selectByPrimaryKey:145 - Preparing: UPDATE rOLE SET DESCRIPTION = ? WHERE ID = ?
2018-09-08 14:40:48.796 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.RoleMapper.updateByPrimaryKeySelective:145 - ==> Parameters: 新的描述哈哈哈(String), 1(Long)
2018-09-08 14:40:48.797 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.RoleMapper.updateByPrimaryKeySelective:145 - Preparing: SELECT ID,NAME,CODE,DESCRIPTION,created_time,updated_time FROM rOLE WHERE ID = ?
2018-09-08 14:40:48.807 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.RoleMapper.selectByPrimaryKey:145 - ==> Parameters: 1(Long)
2018-09-08 14:40:48.808 DEBUG [main] com.github.houbb.mybatis.learn.cache.mapper.RoleMapper.selectByPrimaryKey:145 - <== Total: 1
secondUserMapper 查询:Role{id=1, name='管理员', code='admin', description='新的描述哈哈哈', createdTime=Sat Sep 08 04:37:10 CST 2018, updatedTime=Sat Sep 08 06:39:51 CST 2018}
备注
mybatis 缓存不适应于多表关联的场景。
可以再一个表的缓存中,引用另一个关联表的 cache 即可。
参考资料
- mybatis
贡献者
binbin.hou