Mybatis核心应用配置与原理解析
# 一、1,2级缓存处理
知识点:
1级缓存使用场景
2级缓存使用场景
# 1级缓存使用场景
订单表与会员表是存在一对多的关系 为了尽可能减少join 查询,进行了分阶段查询,即先查询出订单表,在根据member_id 字段查询出会员表,最后进行数据整合 。如果订单表中存在重复的member_id,就会出现很多没必要的重复查询。
针对这种情况myBatis 通过1缓存来实现,在同一次查询会话中如果出现相同的语句及参数,就会从缓存中取出不在走数据库查询。1级缓存只能作用于查询会话中 所以也叫做会话缓存
示例:
LabelMapper mapper = session.getMapper(LabelMapper.class);
Label label = mapper.getById(23);
Label label2 = mapper.getById(23);
Label label3 = sqlSessionFactory.openSession().getMapper(LabelMapper.class).getById(23); // session不同,不会触发一级缓存
Label label4 = session.getMapper(Label2Mapper.class).getById(23); // mapper不同,不会触发一级缓存
session.clearCache(); //
mapper.updateById(new Label(1,"jim")); // 有增删改操作,不会触发一级缓存
System.out.println(label == label2);
System.out.println(label3 == label2);
System.out.println(label4 == label2);
2
3
4
5
6
7
8
9
10
# 一级缓存的使用条件:
1、必须是相同的SQL和参数
2、 必须是相同的session会话
3、必须是相同的namespace 即同一个mapper
4、 必须是相同的statement 即同一个mapper 接口中的同一个方法
5、 查询语句中间没有执行session.clearCache() 方法
6、查询语句中间没有执行 insert update delete 方法(无论变动记录是否与缓存数据有无关系)
一级缓存源码解析:
# 缓存获取 :
断点打在mapper.selectById(23), 通过F5进入方法内,不断追踪到
MapperProxy
,其调用mapperMethod.execute(sqlSession, args)
, 类MapperMethod
调用sqlSession.selectOne(command.getName(), param)
, 追踪到类DefaultSqlSession
,调用selectList()
, 再到类CachingExecutor
的query(),其调用父类``BaseExecutor的query(),其调用类
PerpetualCache的
cache.getObject(key)`,发现cache就是一个hashMap
mapper.mapper.selectById(23)
> org.apache.ibatis.session.defaults.DefaultSqlSession#selectOne(java.lang.String, java.lang.Object)
>org.apache.ibatis.session.defaults.DefaultSqlSession#selectList()
>org.apache.ibatis.executor.CachingExecutor#query()
>org.apache.ibatis.executor.BaseExecutor#query() 142L
>org.apache.ibatis.cache.impl.PerpetualCache#getObject 55L
2
3
4
5
6
# 缓存的存储:
mapper.mapper.selectById(23)
> org.apache.ibatis.session.defaults.DefaultSqlSession#selectOne(java.lang.String, java.lang.Object)
>org.apache.ibatis.session.defaults.DefaultSqlSession#selectList()
>org.apache.ibatis.executor.CachingExecutor#query()
>org.apache.ibatis.executor.BaseExecutor#queryFromDatabase() 156L
>org.apache.ibatis.cache.impl.PerpetualCache#putObject
2
3
4
5
6
通过对clearCache 作为入口我们可能追踪到 一级缓存的实现 PerpetualCache
>org.apache.ibatis.session.defaults.DefaultSqlSession#clearCache
>org.apache.ibatis.executor.CachingExecutor#clearLocalCache
>org.apache.ibatis.executor.BaseExecutor#clearLocalCache
>org.apache.ibatis.cache.impl.PerpetualCache#clear
提问:
在查询时另一个会话并发去修改查询的数据,一级缓存是否会生效?如果生效是否就会导致数据不正确?
- 因为一级缓存的结果,对象地址一样,因此如果修改查询数据时,原数据也会跟着改变。
# 2级缓存使用场景:
业务系统中存在很多的静态数据如,字典表、菜单表、权限表等,这些数据的特性是不会轻易修改但又是查询的热点数据。一级缓存针对的是同一个会话当中相同SQL,并不适合这情热点数据的缓存场景。为了解决这个问题引入了二级缓存,它脱离于会话之外。
2级缓存示例:
// 二级缓存: 默认readWrite = true,即为对象拷贝,对象model需要可序列化, 改为readWrite = false,则缓存为引用,会有安全问题
@CacheNamespace()
public interface LabelMapper {
@Select("select * from t_label where id =#{id}")
Label getById(Integer id);
}
2
3
4
5
6
7
8
属性说明:
@CacheNamespace(
implementation = PerpetualCache.class, // 缓存实现 Cache接口 实现类
eviction = LruCache.class,// 缓存算法
flushInterval = 60000, // 刷新间隔时间 毫秒
size = 1024, // 最大缓存引用对象
readWrite = true, // 是否可写,默认true, model对象必须可序列化
blocking = false // 是否阻塞
)
2
3
4
5
6
7
8
# 2级缓存使用条件:
1、当会话提交或关闭之后才会填充二级缓存
2、必须是在同一个命名空间之下
3、必须是相同的statement 即同一个mapper 接口中的同一个方法
4、 必须是相同的SQL语句和参数
5、如果readWrite=true ,实体对像必须实现Serializable 接口, 这里采用的是对象拷贝; 为false的情况下,是对象引用,会有缓存安全问题
# 2级缓存清除条件:
1、 xml中配置的update 不能清空 @CacheNamespace 中的缓存数据,即xml里面的namespace和注解的缓存空间是不一样的。
2、只有修改会话提交之后 才会执行清空操作
3、 任何一种增删改操作 都会清空整个namespace 中的缓存
# 2级缓存源码解析:
清除缓存!
>org.apache.ibatis.session.defaults.DefaultSqlSession#selectList() 147L
>org.apache.ibatis.executor.CachingExecutor#query()81L
>org.apache.ibatis.executor.CachingExecutor#query()95L
>org.apache.ibatis.executor.CachingExecutor#flushCacheIfRequired() 164L //清除缓存
# 获取缓存关键源码
>org.apache.ibatis.cache.TransactionalCacheManager#getObject
>org.apache.ibatis.cache.decorators.TransactionalCache#getObject
>org.apache.ibatis.cache.decorators.SynchronizedCache#getObject
>org.apache.ibatis.cache.decorators.LoggingCache#getObject
>org.apache.ibatis.cache.decorators.SerializedCache#getObject
>org.apache.ibatis.cache.decorators.ScheduledCache#getObject
>org.apache.ibatis.cache.decorators.LruCache#getObject
>org.apache.ibatis.cache.impl.PerpetualCache#getObject
# 保存2级缓存
org.apache.ibatis.executor.CachingExecutor#close
>org.apache.ibatis.cache.TransactionalCacheManager#commit
>org.apache.ibatis.cache.decorators.TransactionalCache#flushPendingEntries
>org.apache.ibatis.cache.decorators.SynchronizedCache#putObject
>org.apache.ibatis.cache.decorators.LoggingCache#putObject
>org.apache.ibatis.cache.decorators.SerializedCache#putObject
>org.apache.ibatis.cache.decorators.ScheduledCache#putObject
>org.apache.ibatis.cache.decorators.LruCache#putObject
>org.apache.ibatis.cache.impl.PerpetualCache#putObject
# MyBatis的缓存机制整体设计以及二级缓存的工作模式
MyBatis 一级缓存最大的共享范围就是一个SqlSession内部,那么如果多个 SqlSession 需要共享缓存,则需要开启二级缓存,开启二级缓存后,会使用 CachingExecutor 装饰 Executor,进入一级缓存的查询流程前,先在CachingExecutor 进行二级缓存的查询
当二级缓存开启后,同一个命名空间(namespace) 所有的操作语句,都影响着一个共同的 cache,也就是二级缓存被多个 SqlSession 共享,是一个全局的变量。当开启缓存后,数据的查询执行的流程就是
二级缓存 -> 一级缓存 -> 数据库
一次会话关闭时,会将数据缓存到二级缓存当中。
参考 :聊聊MyBatis缓存机制 - 美团技术团队 (meituan.com) (opens new window)
# 二、动态化SQL
基本命令使用
if
choose (when, otherwise)
trim (where, set)
foreach
# trim示例说明:
<trim prefix="where" prefixOverrides="and|or">
<if test="id != null">
and id = #{id}
</if>
<if test="name != null">
and name = #{name}
</if>
</trim>
2
3
4
5
6
7
8
trim属性说明:
prefix="where" // 前缀
prefixOverrides="and|or" // 前缀要替换的词
suffix="" // 添加后缀
suffixOverrides="" // 后缀要替换的词
# where元素说明:
在where 包裹的SQL前会自动添加 where 字符 并去掉首尾多佘的 and|or 字符 相当于下配置:
<trim prefix="where" prefixOverrides="and|or" suffixOverrides="and|or">
# set元素说明:
在set包裹的SQL前会自动添加 set 字符并去掉首尾多佘的 , 字符。
# sql 元素说明:
在同一个mapper 多个statement 存在多个相同的sql 片段时,可以通过
声明sql 段
<sql id="files">
id ,name ,createTime
</sql>
2
3
引用
<include refid="files" />
# bind 变量使用
有时需要进行一些额外 逻辑运行,通过 声明
<bind name="likeName" value="'%'+ _parameter.getName() +'%'"></bind>
内置变量
_databaseid 数据库标识ID
_parameter 当前参数变理
# 自定义模板解释器
以上的if trim where 等逻辑符都是 myBatis 自带的XMLLanguageDriver 所提供的解释语言,除非此之外 我们还可以使用 MyBatis-Velocity 或 mybatis-freemarker 等外部 解释器来编写动态脚本。
# mybatis-freemarker 使用
引入mybatis 包:
<dependency>
<groupId>org.mybatis.scripting</groupId>
<artifactId>mybatis-freemarker</artifactId>
<version>1.1.2</version>
</dependency>
2
3
4
5
添加sql 语句
<select id="selectByIds"
resultType="com.tuling.mybatis.dao.User"
lang="org.mybatis.scripting.freemarker.FreeMarkerLanguageDriver">
select * from user
where id in(${ids?join(',')})
</select>
2
3
4
5
6
添加接口方法
List<User> selectByIds(@Param("ids") List<Integer> ids);