springboot 使用jedis 集成 redis 实现 mybatis 二级缓存
第一步:pom.xml引入相关依赖
1 2 3 4 5 6 7 8 9 10 | <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version> 1.3 . 2 </version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version> 1.4 . 6 .RELEASE</version> </dependency> |
注:mybatis 相关使用我在这里就不写了
第二步:编写RedisConfig 配置类
1 @Configuration 2 public class RedisConfig { 3 public JedisPoolConfig getRedisConfig(){ 4 JedisPoolConfig config = new JedisPoolConfig(); 5 config.setMaxIdle(8); 6 config.setMaxTotal(8); 7 config.setMaxWaitMillis(2); 8 config.setMinIdle(0); 9 return config; 10 } 11 @Bean(name = "jedisConnectionFactory") 12 public JedisConnectionFactory getConnectionFactory(){ 13 JedisConnectionFactory factory = new JedisConnectionFactory(getRedisConfig()); 14 factory.setHostName("127.0.0.1"); 15 factory.setPassword("123456"); 16 return factory; 17 } 18 }
第三步:编写RedisCacheTransfer 用以实例化JedisConnectionFactory
1 2 3 4 5 6 7 | @Component public class RedisCacheTransfer { @Autowired public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) { RedisCache.setJedisConnectionFactory(jedisConnectionFactory); } } |
第四步:编写RedisCache 并且实现 Cache接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | import org.apache.ibatis.cache.Cache; //特意把cache包路径写出来防止你们导错包 public class RedisCache implements Cache { private static final Logger logger = LoggerFactory.getLogger(RedisCache. class ); @Autowired private static JedisConnectionFactory jedisConnectionFactory; private final String id; private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); public RedisCache( final String id){ if (id == null ){ throw new IllegalArgumentException( "Cache instances require an Id" ); } logger.debug( "RedisCache id = {}" , id); this .id = id; } @Override public String getId() { return this .id; } @Override public void putObject(Object key, Object value) { logger.debug( "putObject Key = {} , value = {}" ,key,value); JedisConnection connection = null ; try { connection = (JedisConnection) jedisConnectionFactory.getConnection(); RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer(); connection.set(serializer.serialize(key),serializer.serialize(value)); } catch (JedisConnectionException e){ e.printStackTrace(); } finally { if (connection != null ){ connection.close(); } } } @Override public Object getObject(Object key) { logger.debug( "getObject key = {}" ,key); Object result = null ; JedisConnection connection = null ; try { connection = (JedisConnection) jedisConnectionFactory.getConnection(); RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer(); result = serializer.deserialize(connection.getJedis().get(serializer.serialize(key))); } catch (JedisConnectionException e){ e.printStackTrace(); } finally { if (connection!= null ){ connection.close(); } } return result; } @Override public Object removeObject(Object key) { logger.debug( "removeObject key = {}" ,key); JedisConnection connection = null ; Object result = null ; try { connection = (JedisConnection) jedisConnectionFactory.getConnection(); RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer(); result = connection.getJedis().exists(serializer.serialize(key)); } catch (JedisConnectionException e){ e.printStackTrace(); } finally { if (connection != null ){ connection.close(); } } return result; } @Override public void clear() { logger.debug( "clear" ); JedisConnection connection = null ; try { connection = (JedisConnection) jedisConnectionFactory.getConnection(); connection.getJedis().flushAll(); connection.getJedis().flushDB(); } catch (JedisConnectionException e){ e.printStackTrace(); } finally { if (connection != null ){ connection.close(); } } } @Override public int getSize() { logger.debug( "getSize" ); int result = 0 ; JedisConnection connection = null ; try { connection = (JedisConnection) jedisConnectionFactory.getConnection(); result = Integer.valueOf(connection.getJedis().dbSize().toString()); } catch (JedisConnectionException e){ e.printStackTrace(); } finally { connection.close(); } return result; } @Override public ReadWriteLock getReadWriteLock() { logger.debug( "getReadWriteLock" ); return this .readWriteLock; } public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory){ logger.debug( "setJedisConnectionFactory" ); RedisCache.jedisConnectionFactory = jedisConnectionFactory; } } |
第五步:application.yml 内开启 mybatis 二级缓存
1 | mybatis.configuration.cache-enabled: true |
第六步:修改实体类实现 序列号接口
1 2 3 | public class ResourceDO implements Serializable { //此处是成员变量以及get set方法 } |
第七步:在mapping 中添加cache 标签
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?xml version= "1.0" encoding= "UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace= "com.qianxiao.blog.mapper.ResourceDOMapper" > <cache type= "com.qianxiao.blog.cache.RedisCache" eviction= "LRU" flushInterval= "100000" readOnly= "true" size= "1024" ></cache> <sql id= "tableName" > resource </sql> <select id= "findById" resultType= "com.qianxiao.blog.dataobject.ResourceDO" > select * from <include refid= "tableName" /> where id = #{id} order by id desc LIMIT 0 , 1 </select> </mapper> |
注:想了解cache 里面各属性请查看我这一篇文章:https://www.cnblogs.com/qianxiaoPro/p/14201364.html
第八步:查看效果
redis内
分类:
java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)