mybatis二级缓存对细粒度的数据级别的缓存实现不好,对同时缓存较多条数据的缓存,比如如下需求:
对商品信息进行缓存,由于商品信息查询访问量大,且要求用户每次都能查询最新的商品信息,此时如果使用mybatis的二级缓存就无法实现---当一个商品变化时只刷新该商品的缓存信息而不刷新其他商品的信息。
因为mybatis的二级缓存区域以mapper为单位划分,当一个商品信息变化会将所有商品信息的缓存数据全部清空。
解决此类问题需要在业务层根据需求对数据进行针对性的缓存,即需要使用三级缓存。
<dependency>
<groupId>org.mybatis.cachesgroupId>
<artifactId>mybatis-ehcacheartifactId>
<version>1.1.0version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.3version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
要开启mybatis.configuration.cache-enabled=true
mybatis:
type-aliases-package: com.example.demo.orm.po
mapper-locations: classpath:mappers/*Mapper.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
# 开启全局缓存(mybatis二级缓存),默认不会开启,需要程序员配置开启
cache-enabled: true
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir/Tmp_EhCache"/>
<defaultCache
eternal="false"
maxElementsInMemory="10000"
overflowToDisk="true"
diskPersistent="true"
timeToIdleSeconds="1800"
timeToLiveSeconds="259200"
memoryStoreEvictionPolicy="LRU"/>
ehcache>
如:在AccountMapper.xml里配置
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.orm.dao.AccountMapper">
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>
<resultMap id="BaseResultMap"
type="com.example.demo.orm.po.Account">
<id column="id" jdbcType="BIGINT" property="id"/>
<result column="username" jdbcType="VARCHAR" property="username"/>
<result column="password_ciper" jdbcType="VARCHAR" property="passwordCiper"/>
resultMap>
<select id="findOneById" resultMap="BaseResultMap">
select * from t_account where id = #{id}
select>
<select id="selectUserList"
parameterType="com.example.demo.orm.po.Account"
resultMap="BaseResultMap">
select * from t_account
<where>
<if test="id != null">
and id = #{id}
if>
<if test="username != null and username != '' ">
and username = #{username}
if>
<if test="passwordCiper != null and passwordCiper != '' ">
and password_ciper = #{passwordCiper}
if>
where>
select>
<insert id="insertOne"
useGeneratedKeys="true"
keyProperty="id"
parameterType="com.example.demo.orm.po.Account">
insert into t_account(username,password_ciper)
values (#{username},#{passwordCiper})
insert>
mapper>
@Test
void test81902(){
//连续执行2次相同的select操作
System.out.println(accountMapper.findOneById(17));
System.out.println(accountMapper.findOneById(17));
}
2023-06-19 19:51:41.924 INFO 6356 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-06-19 19:51:42.186 INFO 6356 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@1502971166 wrapping com.mysql.cj.jdbc.ConnectionImpl@48a2db72] will not be managed by Spring
==> Preparing: select * from t_account where id = ?
==> Parameters: 17(Integer)
<== Columns: id, username, password, password_ciper
<== Row: 17, ert, sgvf23t, 1a9777393e7fda54
<== Total: 1
2023-06-19 19:51:42.452 INFO 6356 --- [ main] c.e.demo.plugin.EncryptDecryptUtil : passwordCiper字段需要解密,1a9777393e7fda54解密后的值是sgvf23t
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@518ddd3b]
Account(id=17, username=ert, password=sgvf23t, passwordCiper=sgvf23t)
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@72443081] was not registered for synchronization because synchronization is not active
Cache Hit Ratio [com.example.demo.orm.dao.AccountMapper]: 0.5
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@72443081]
Account(id=17, username=ert, password=sgvf23t, passwordCiper=sgvf23t)
Cache Hit Ratio [com.example.demo.orm.dao.AccountMapper]: 0.5
意思是缓存命中了AccountMapper,命中率0.5