Springboot连接Redis的详细教程
本篇内容主要讲解“Springboot连接Redis的详细教程”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Springboot连接Redis的详细教程”吧!
创建springboot项目
在NoSQL中选择Redis
项目目录
pom.xml中还需要加入下面的jar包
org.springframework.boot spring-boot-starter-json
在application.properties文件中添加Redis服务器信息
spring.redis.host=192.168.5.132 spring.redis.port=6379
剩下4个test类,我直接以源码的方式粘出来,里面有些代码是非必须的,我保留了测试的验证过程,所以里面会有冗余代码。(这些代码在我GitHub上的练习项目中也有)
packagecom.myspringboot.redis; importorg.springframework.boot.SpringApplication; importorg.springframework.boot.autoconfigure.SpringBootApplication; importorg.springframework.context.ConfigurableApplicationContext; @SpringBootApplication publicclassDemoApplication{ publicstaticvoidmain(String[]args){ ConfigurableApplicationContextconfigurableApplicationContext=SpringApplication.run(DemoApplication.class,args); RedisTestredisTest=configurableApplicationContext.getBean(RedisTest.class); redisTest.testRedis(); } }
packagecom.myspringboot.redis; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importorg.springframework.data.redis.connection.RedisConnectionFactory; importorg.springframework.data.redis.core.StringRedisTemplate; importorg.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; @Configuration publicclassMyTemplate{ @Bean publicStringRedisTemplategetMyTemplate(RedisConnectionFactoryredisConnectionFactory){ StringRedisTemplatestringRedisTemplate=newStringRedisTemplate(redisConnectionFactory); stringRedisTemplate.setHashValueSerializer(newJackson2JsonRedisSerializer<Object>(Object.class)); returnstringRedisTemplate; } }
packagecom.myspringboot.redis; importcom.fasterxml.jackson.databind.ObjectMapper; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.beans.factory.annotation.Qualifier; importorg.springframework.data.redis.connection.Message; importorg.springframework.data.redis.connection.MessageListener; importorg.springframework.data.redis.connection.RedisConnection; importorg.springframework.data.redis.core.HashOperations; importorg.springframework.data.redis.core.RedisTemplate; importorg.springframework.data.redis.core.StringRedisTemplate; importorg.springframework.data.redis.hash.Jackson2HashMapper; importorg.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; importorg.springframework.stereotype.Component; importjava.util.Map; @Component publicclassRedisTest{ @Autowired RedisTemplateredisTemplate; @Autowired StringRedisTemplatestringRedisTemplate; @Autowired ObjectMapperobjectMapper; //自定义模板 @Autowired @Qualifier("getMyTemplate") StringRedisTemplatemyStringRedisTemplate; publicvoidtestRedis(){ //redis中直接查看时,乱码 redisTemplate.opsForValue().set("key1","hello1"); System.out.println(redisTemplate.opsForValue().get("key1")); //redis中直接查看时,正常 stringRedisTemplate.opsForValue().set("key2","hello2"); System.out.println(stringRedisTemplate.opsForValue().get("key2")); RedisConnectionconnection=redisTemplate.getConnectionFactory().getConnection(); connection.set("key3".getBytes(),"hello3".getBytes()); System.out.println(newString(connection.get("key3".getBytes()))); HashOperations<String,Object,Object>hash=stringRedisTemplate.opsForHash(); hash.put("key4","name","张三"); hash.put("key4","age","18"); System.out.println(hash.get("key4","name")); System.out.println(hash.entries("key4")); stringRedisTemplate.setHashValueSerializer(newJackson2JsonRedisSerializer<Object>(Object.class)); Jackson2HashMapperjackson2HashMapper=newJackson2HashMapper(objectMapper,false);//true扁平化(将对象中的参数展开) Useruser=newUser(); user.setId(123); user.setName("zhangsan"); stringRedisTemplate.opsForHash().putAll("key5",jackson2HashMapper.toHash(user)); Mapmap=stringRedisTemplate.opsForHash().entries("key5"); Useruser1=objectMapper.convertValue(map,User.class); System.out.println(user1.getId()); System.out.println(user1.getName()); myStringRedisTemplate.opsForHash().putAll("key6",jackson2HashMapper.toHash(user)); Mapmap1=myStringRedisTemplate.opsForHash().entries("key6"); Useruser2=objectMapper.convertValue(map,User.class); System.out.println(user2.getId()); System.out.println(user2.getName()); //发布订阅 myStringRedisTemplate.convertAndSend("qunliao","hello"); RedisConnectionconnection1=myStringRedisTemplate.getConnectionFactory().getConnection(); connection1.subscribe(newMessageListener(){ @Override publicvoidonMessage(Messagemessage,byte[]bytes){ byte[]body=message.getBody(); System.out.println(newString(body)); } },"qunliao".getBytes()); while(true){ myStringRedisTemplate.convertAndSend("qunliao","hello"); try{ Thread.sleep(3000); }catch(InterruptedExceptione){ e.printStackTrace(); } } } }
packagecom.myspringboot.redis; publicclassUser{ privateintid; privateStringname; publicintgetId(){ returnid; } publicvoidsetId(intid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } }
到此,相信大家对“Springboot连接Redis的详细教程”有了更深的了解,不妨来实际操作一番吧!这里是恰卡编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
推荐阅读
-
springboot实现基于aop的切面日志
本文实例为大家分享了springboot实现基于aop的切面日志的具体代码,供大家参考,具体内容如下通过aop的切面方式实现日志...
-
SpringBoot定时任务功能怎么实现
-
SpringBoot中的@Import注解怎么使用
-
SpringBoot整合Lombok及常见问题怎么解决
-
springboot图片验证码功能模块怎么实现
-
Springboot+SpringSecurity怎么实现图片验证码登录
-
SpringBoot注解的知识点有哪些
SpringBoot注解的知识点有哪些这篇“SpringBoot注...
-
SpringBoot2.x中management.security.enabled=false无效怎么解决
-
springboot怎么禁用某项健康检查
springboot怎么禁用某项健康检查今天小编给大家分享一下sp...
-
SpringBoot2怎么自定义端点