• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot+redis配置及测试的方法

springboot 搞代码 4年前 (2022-01-05) 20次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了SpringBoot+redis配置及测试的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1.创建项目时选择redis依赖

2.修改配置文件,使用SpringBoot就避免了之前很多的xml文件

2.1学过redis的同学都知道这个东西有集群版也有单机版,无论哪个版本配置起来都很简单

2.1.1首先找到配置文件

   

2.1.2然后配置集群版,直接在配置文件内编辑即可

2.1.3配置单机版

3.测试

找到测试文件夹,自动注入redis模板

4.分别测试操作String和Hash类型的数据

4.1操作String

 @Test public void testString(){ //操作String类型的数据 ValueOperations valueStr = redisTemplate.opsForValue(); //存储一条数据 valueStr.set("goodsProdu","长安"); //获取一条数据并输出 String goodsName = valueStr.get("goodsProdu"); System.out.println(goodsName); //存储多条数据 Map map = new HashMap(); map.put("goodsName","福特汽车"); map.put("goodsPrice","88888"); map.put("goodsId","88"); valueStr.multiSet(map); //获取多条数据 System.out.println("========================================"); Listlist = new ArrayList(); list.add("goodsName"); list.add("goodsPrice"); list.add("goodsId"); list.add("goodsProdu"); List listKeys = valueStr.multiGet(list); for (String key : listKeys) { System.out.println(key); } }

效果

 .   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) ) '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v2.0.3.RELEASE) 2018-06-21 09:45:31.328  INFO 8848 --- [           main] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library 长安 ======================================== 福特汽车 88888 88 长安

4.2测试hash数据

 @Test public void testHash(){ //创建对象 HashOperations opsForHash = redisTemplate.opsForHash(); //存储一条数据 opsForHash.put("orderInfo","orderId","11"); //获取一条数据 String value = opsForHash.get("orderInfo", "orderId"); System.out.println(value); //存储多条数据 Map map = new HashMap(); map.put("createTime","2018-06-21"); map.put("orderSn","888888"); opsForHash.putAll("orderInfo",map); //获取多条数据 List listKey = new ArrayList(); listKey.add("createTime"); listKey.add("orderSn"); List info = opsForHash.multiGet("orderInfo", listKey); for (String s : info) { System.out.println(s); } }

效果

 .   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \<mark style="color:transparent">来源gaodaimacom搞#^代%!码网</mark> \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) ) '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v2.0.3.RELEASE) 2018-06-21 09:48:26.020  INFO 3852 --- [           main] c.b.s.SpringbootRedisApplicationTests    : Starting SpringbootRedisApplicationTests on sixfly with PID 3852 (started by Administrator in D:\work_space\springbootdemo\springboot-redis) 2018-06-21 09:48:26.030  INFO 3852 --- [           main] c.b.s.SpringbootRedisApplicationTests    : No active profile set, falling back to default profiles: default 2018-06-21 09:48:26.174  INFO 3852 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2f953efd: startup date [Thu Jun 21 09:48:26 CST 2018]; root of context hierarchy 2018-06-21 09:48:28.398  INFO 3852 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode! 2018-06-21 09:48:32.182  INFO 3852 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 2018-06-21 09:48:35.054  INFO 3852 --- [           main] c.b.s.SpringbootRedisApplicationTests    : Started SpringbootRedisApplicationTests in 11.637 seconds (JVM running for 19.635) 2018-06-21 09:48:36.390  INFO 3852 --- [           main] io.lettuce.core.EpollProvider            : Starting without optional epoll library 2018-06-21 09:48:36.398  INFO 3852 --- [           main] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library 11 2018-06-21 888888

以上就是SpringBoot+redis配置及测试的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot+redis配置及测试的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址