1. 在 Application中添加@EnableCaching注解
@EnableCaching
public class TsrApplication {
public static void main(String[] args) {
SpringApplication.run(TsrApplication.class, args);
System.setProperty("tomcat.util.http.parser.HttpParser.requestTargetAllow","|{}");
}
}
2. 然后我们一般在serviceImpl 实现的方法上加上
@Override
@Cacheable(value="scene", key="#title")
public Scene select(String title) {
return sceneMapper.select(title);
}但是当传入的参数为null的时候报错
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Null key returned for cache operation (maybe you are using named params on classes without debug info?) Builder[public java.util.List com.knowology.service.impl.SearchContentServiceImpl.selectByKey(java.lang.String)] caches=[searchcontents] | key='#keyword' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'] with root cause
java.lang.IllegalArgumentException: Null key returned for cache operation (maybe you are using named params on classes without debug info?) Builder[public java.util.List com.knowology.service.impl.SearchContentServiceImpl.selectByKey(java.lang.String)] caches=[searchcontents] | key='#keyword' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
当有空参传入 redis就会找不到key
@Override @Cacheable(value="scene", key="'scene_'.concat(#title)") 最好的方法是'scene_'+(#title)当title为null的时候回报控制针public Scene select(String title) { return sceneMapper.select(title); }
所以在传key的是后添加前缀方式空参
注意实体类要序列化 这就是二级mybatis的二级缓存 ,mybatis默认是开启一级二级缓存的,但是一级缓存只有在添加事务的情况下才会触发 ,二级缓存需要添加相关内容配置才会触发