背景:springboot项目集成了mybatis-plus,采用redis缓存
出现问题:在方法上接入缓存注解,但是redis库中无缓存
@Cacheable(cacheNames= CacheConstant.SYS_USERS_CACHE, key="#dlzh")
public S_YHRYForLoginVo getUserByDlzh(String dlzh){
//mybatis-plus查询
}
执行sql报如下提示:
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@48cd64a3] was not registered for synchronization because synchronization is not active
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@13190fbc] will not be managed by Spring
解决方法:加上@Transactional注解,将方法声明为事务
@Transactional
@Cacheable(cacheNames= CacheConstant.SYS_USERS_CACHE, key="#dlzh")
原理:redis缓存基于springboot AOP