一、Mybatis一二级缓存处理
知识点:
1 级缓存使用场景
2 级缓存使用场景
1. 一级缓存使用场景
订单表与会员表是存在一对多的关系 为了尽可能减少join 查询,进行了分阶段查询,即先查询出订单表,在根据member_id 字段查询出会员表,最后进行数据整合 。如果订单表中存在重复的member_id,就会出现很多没必要的重复查询。 针对这种情况myBatis 通过1缓存来实现,在同一次查询会话中如果出现相同的语句及参数,就会从缓存中取出不在走数据库查询。1级缓存只能作用于查询会话中 所以也叫做会话缓存。
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);
Label label4 = session.getMapper(Label2Mapper.class).getById(23);
System.out.println(label == label2);
System.out.println(label3 == label2);
System.out.println(label4 == label2);
1.2 一级缓存的使用条件
1、 必须是相同的SQL和参数
2、 必须是相同的会话
3、 必须是相同的namespace即同一个mapper
4、 必须是相同的statement即同一个mapper接口中的同一个方法
5、 查询语句中间没有执行session.clearCache()方法
6、 查询语句中间没有执行 insert update delete 方法(无论变动记录是否与 缓存数据有无关系)
1.3 一级缓存源码解析
缓存获取:
>mapper.findById(1) dao方法
>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
缓存存储:
>mapper.findById(1) dao方法
>org.apache.ibatis.session.defaults.DefaultSqlSession#selectList()
>org.apache.ibatis.executor.CachingExecutor#query()
>org.apache.ibatis.executor.BaseExecutor#query() 142L
>org.apache.ibatis.executor.BaseExecutor#queryFromDatabase
>org.apache.ibatis.cache.impl.PerpetualCache#putObject
通过对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.1 二级缓存示例:
@CacheNamespace()
public interface LabelMapper {
@Select("select * from t_label where id =#{id}")
Label getById(Integer id);
}
属性说明:
@CacheNamespace(
implementation = PerpetualCache.class, // 缓存实现 Cache接口 实现类
eviction = LruCache.class,// 缓存算法
flushInterval = 60000, // 刷新间隔时间 毫秒
size = 1024, // 最大缓存引用对象
readWrite = true, // 是否可写
blocking = false // 是否阻塞
)
2.2 二级缓存使用条件
1、 当会话提交或关闭之后才会填充二级缓存
2、 必须是在同一个命名空间之下
3、 必须是相同的statement 即同一个mapper 接口中的同一个方法
4、 必须是相同的SQL语句和参数
5、 如果readWrite=true ,实体对像必须实现Serializable 接口
2.3 二级缓存清除条件
1、 xml中配置的update 不能清空 @CacheNamespace 中的缓存数据
2、 只有修改会话提交之后 才会执行清空操作
3、 任何一种增删改操作 都会清空整个namespace 中的缓存
2.4 二级缓存源码解析
清除缓存
>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
保存二级缓存
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动态化SQL
基本命令使用
if
choose (when, otherwise)
trim (where, set)
foreach
示例说明:
<trim prefix="where" prefixOverrides="and|or">
<if test="id != null">
and id = #{id}
</if>
<if test="name != null">
and name = #{name}
</if>
</trim>
1. trim属性说明:
prefix="where" // 前缀
prefixOverrides="and|or" // 前缀要替换的词
suffix="" // 添加后缀
suffixOverrides="" // 后缀要替换的词
2. 元素说明:
在where 包裹的SQL前会自动添加 where 字符 并去掉首尾多佘的 and|or 字符 相当于下配置:
<trim prefix="where" prefixOverrides="and|or" suffixOverrides="and|or">
3. 元素说明:
在set包裹的SQL前会自动添加 set 字符并去掉首尾多佘的 , 字符。
4. 元素说明:
在同一个mapper 多个statement 存在多个相同的sql 片段时,可以通过元素声明,在通过 元素进行引用。
声明sql 段
<sql id="files">
id ,name ,createTime
</sql>
引用
<include refid="files" />
5. 变量使用
有时需要进行一些额外 逻辑运行,通过 声明元素,并在其value 属性中添加运算脚本,如下示例 自动给likeName 加上了% 分号,然后就可以用#{likeName} 来使用带%分号的like 运算。
<bind name="likeName" value="'%'+ _parameter.getName() +'%'"></bind>
6. 内置变量
_databaseid 数据库标识ID _parameter 当前参数变理
7. 自定义模板解释器
以上的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>
添加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>
添加接口方法
List<User> selectByIds(@Param("ids") List<Integer> ids);