In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use redis as a secondary cache in mybatis plus. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
1. Mybatis-plus enables secondary cache
Mybatis-plus.configuration.cache-enabled=true
two。 The bean that defines RedisTemplate is managed by spring. In order to access the object directly into redis, some serialization operations @ Bean (value = "redisTemplate") public RedisTemplate redisTemplate (RedisConnectionFactory connectionFactory) {RedisTemplate redisTemplate = new RedisTemplate (); redisTemplate.setConnectionFactory (connectionFactory); / / Use Jackson2JsonRedisSerializer to serialize and deserialize the value of redis (default JDK serialization) Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer (Object.class); ObjectMapper objectMapper = new ObjectMapper () ObjectMapper.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); / / serialize the class name to objectMapper.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL) in the json string; / / set the attribute objectMapper.configure (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) that exists in the JSON string but does not actually exist in the Java object when setting input; jackson2JsonRedisSerializer.setObjectMapper (objectMapper) / / Use StringRedisSerializer to serialize and deserialize the key value of redis RedisSerializer redisSerializer = new StringRedisSerializer (); / / key redisTemplate.setKeySerializer (redisSerializer); redisTemplate.setHashKeySerializer (redisSerializer); / / value redisTemplate.setValueSerializer (jackson2JsonRedisSerializer); redisTemplate.setHashValueSerializer (jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet (); return redisTemplate;} 3. Customize your own cache management package com.qctchina.headsetserver.config;import com.qctchina.headsetserver.util.SpringUtil;import lombok.extern.slf4j.Slf4j;import org.apache.ibatis.cache.Cache;import org.springframework.data.redis.connection.RedisServerCommands;import org.springframework.data.redis.core.RedisCallback;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.util.CollectionUtils;import java.util.Set;import java.util.concurrent.locks.ReadWriteLock;import java.util.concurrent.locks.ReentrantReadWriteLock / * * @ author shuangyueliao * @ create 14:02 on 2019-9-10 * @ Version 0.1 * / @ Slf4jpublic class MybatisRedisCache implements Cache {/ / read-write lock private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock (true); / / redis cache is used here, and private RedisTemplate redisTemplate; private String id; public MybatisRedisCache (final String id) {if (id = = null) {throw new IllegalArgumentException ("Cache instances require an ID") is automatically injected using springboot. } this.id = id;} @ Override public String getId () {return this.id;} @ Override public void putObject (Object key, Object value) {if (redisTemplate = = null) {/ / because injection failed during startup, only injection can be run during startup. This code can delete redisTemplate = (RedisTemplate) SpringUtil.getBean ("redisTemplate"). } if (value! = null) {redisTemplate.opsForValue () .set (key.toString (), value);} @ Override public Object getObject (Object key) {if (redisTemplate = = null) {/ / because injection failed during startup, only injection can be run during startup. This code can delete redisTemplate = (RedisTemplate) SpringUtil.getBean ("redisTemplate"). } try {if (key! = null) {return redisTemplate.opsForValue () .get (key.toString ());} catch (Exception e) {e.printStackTrace (); log.error ("cache error");} return null } @ Override public Object removeObject (Object key) {if (redisTemplate = = null) {/ / due to injection failure during startup, only injection during startup can be run. This code can delete redisTemplate = (RedisTemplate) SpringUtil.getBean ("redisTemplate");} if (key! = null) {redisTemplate.delete (key.toString ());} return null } @ Override public void clear () {log.debug ("clear cache"); if (redisTemplate = = null) {redisTemplate = (RedisTemplate) SpringUtil.getBean ("redisTemplate");} Set keys = redisTemplate.keys ("*:" + this.id + "*"); if (! CollectionUtils.isEmpty (keys)) {redisTemplate.delete (keys) } @ Override public int getSize () {if (redisTemplate = = null) {/ / due to injection failure during startup, only injection during startup can be run. This code can delete redisTemplate = (RedisTemplate) SpringUtil.getBean ("redisTemplate");} Long size = redisTemplate.execute ((RedisCallback) RedisServerCommands::dbSize); return size.intValue () } @ Override public ReadWriteLock getReadWriteLock () {return this.readWriteLock;}}
SpringUtil is a tool class that gets bean manually.
@ Componentpublic class SpringUtil implements ApplicationContextAware {private static ApplicationContext applicationContext; @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {SpringUtil.applicationContext = applicationContext;} public static Object getBean (String name) {return applicationContext.getBean (name);} public static T getBean (String name, Class clazz) {return applicationContext.getBean (name, clazz);} public static T getBean (Class clazz) {return applicationContext.getBean (clazz);}} 4. Annotate @ CacheNamespace@CacheNamespace (implementation= MybatisRedisCache.class,eviction=MybatisRedisCache.class) public interface CommonMapper extends BaseMapper on mapper {if the method under this mapper is called, the redis cache will be used
After reading the above, do you have any further understanding of how to use redis as a secondary cache in mybatis plus? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.