SpringBoot中整合Redis
本次,我们以IDEA + SpringBoot作为 Java中整合Redis的使用 的测试环境,如果对创建SpringBoot项目有不清楚的地方,可以参考这篇文章: 使用Idea创建我的第一个SpringBoot项目
首先,我们需要导入Redis的maven依赖
1 2 3 4 5 |
<!-- Redis的maven依赖包 --> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-data-redis</ artifactId > </ dependency > |
其次,我们需要在配置文件中配置你的Redis配置信息,我使用的是 .yml文件格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# redis配置 spring: redis: # r服务器地址 host: 127.0.0.1 # 服务器端口 port: 6379 # 数据库索引(默认0) database: 0 jedis: pool: # 连接池最大连接数(使用负值表示没有限制) max-active: 50 # 连接池最大阻塞等待时间(使用负值表示没有限制) max-wait: 3000ms # 连接池中的最大空闲连接数 max-idle: 20 # 连接池中的最小空闲连接数 min-idle: 2 # 连接超时时间(毫秒) timeout: 5000ms |
然后,我们需要创建一个RedisUtil来对Redis数据库进行操作
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 121 122 123 124 125 126 |
package com.zyxx.test.utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component;
/** * @ClassName RedisUtil * @Author Lizhou * @Date 2019-08-03 17:29:29 * @Version 1.0 **/ @Component public class RedisUtil {
@Autowired private RedisTemplate<String, String> template;
/** * 读取数据 * * @param key * @return */ public String get( final String key) { return template.opsForValue().get(key); }
/** * 写入数据 */ public boolean set( final String key, String value) { boolean res = false ; try { template.opsForValue().set(key, value); res = true ; } catch (Exception e) { e.printStackTrace(); } return res; }
/** * 根据key更新数据 */ public boolean update( final String key, String value) { boolean res = false ; try { template.opsForValue().getAndSet(key, value); res = true ; } catch (Exception e) { e.printStackTrace(); } return res; }
/** * 根据key删除数据 */ public boolean del( final String key) { boolean res = false ; try { template.delete(key); res = true ; } catch (Exception e) { e.printStackTrace(); } return res; }
/** * 是否存在key */ public boolean hasKey( final String key) { boolean res = false ; try { res = template.hasKey(key); } catch (Exception e) { e.printStackTrace(); } return res; }
/** * 给指定的key设置存活时间 * 默认为-1,表示永久不失效 */ public boolean setExpire( final String key, long seconds) { boolean res = false ; try { if ( 0 < seconds) { res = template.expire(key, seconds, TimeUnit.SECONDS); } } catch (Exception e) { e.printStackTrace(); } return res; }
/** * 获取指定key的剩余存活时间 * 默认为-1,表示永久不失效,-2表示该key不存在 */ public long getExpire( final String key) { long res = 0 ; try { res = template.getExpire(key, TimeUnit.SECONDS); } catch (Exception e) { e.printStackTrace(); } return res; }
/** * 移除指定key的有效时间 * 当key的有效时间为-1即永久不失效和当key不存在时返回false,否则返回true */ public boolean persist( final String key) { boolean res = false ; try { res = template.persist(key); } catch (Exception e) { e.printStackTrace(); } return res; } } |
最后,我们可以使用单元测试来检测我们在RedisUtil中写的操作Redis数据库的方法
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 |
package com.zyxx.test; import com.zyxx.test.utils.RedisUtil; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource;
@RunWith (SpringRunner. class ) @SpringBootTest public class TestApplicationTest {
@Resource private RedisUtil redisUtil;
@Test public void setRedis() { boolean res = redisUtil.set( "jay" , "周杰伦 - 《以父之名》" ); System.out.println(res); }
@Test public void getRedis() { String res = redisUtil.get( "jay" ); System.out.println(res); }
@Test public void updateRedis() { boolean res = redisUtil.update( "jay" , "周杰伦 - 《夜的第七章》" ); System.out.println(res); }
@Test public void delRedis() { boolean res = redisUtil.del( "jay" ); System.out.println(res); }
@Test public void hasKey() { boolean res = redisUtil.hasKey( "jay" ); System.out.println(res); }
@Test public void expire() { boolean res = redisUtil.setExpire( "jay" , 100 ); System.out.println(res); }
@Test public void getExpire() { long res = redisUtil.getExpire( "jay" ); System.out.println(res); }
@Test public void persist() { boolean res = redisUtil.persist( "jay" ); System.out.println(res); } } |
推荐使用Redis客户端(redis-desktop-manager)来查看Redis数据库中的数据
至此,我们在日常项目中整合Redis的基本使用操作就完成了,但在实际项目中,可能会涉及到更复杂的用法,可以根据你的业务需求调整Redis的使用即可。
SpringBoot整合Redis改不了database问题
关于学习redis写的yaml文件,里面的
1 2 3 4 5 6 |
redis: database: 15 host: 127.0.0.1 port: 6379 password: timeout: 3000ms # 连接超时时间(毫秒) |
但是springboot自动装配一直是database为0
最后发现是没有找到我配置的redis,默认是用的自动装配的默认地址(所以我一直以为我的yaml写对了,但是没用写对,因为一直没有取到我的值)
自动装配的就是
database=0,port=6379;无密码,host=localhost,
就是因为这个原因,我写的数据能进入我的本地redis中,所以我一直没找到问题,因为我也使用的本地的redis测试的,一直找不到为什么改不了database。
最后发现是我的yaml文件的redis写错了,把redis写到log下面,并且归属于log。
最后该成归属于spring下面就可以了。
注意: 一定要注意yaml的书写规范和上下级的关系。
深刻的教训,我在网上查了很多,也没找到对应的办法,幸好没有,不然的话,我的代码会被改的乱七八糟的,花了一下午才发现是自己yaml写错了。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
原文链接:https://lizhou.blog.csdn.net/article/details/98358258
查看更多关于使用SpringBoot中整合Redis的详细内容...