springboot连接Redis的教程详解
创建springboot项目
在NoSQL中选择Redis
项目目录
pom.xml中还需要加入下面的jar包
org.springframework.bootspring-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
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()))); HashOperationshash=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
packagecom.myspringboot.redis; publicclassUser{ privateintid; privateStringname; publicintgetId(){ returnid; } publicvoidsetId(intid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } }
到此这篇关于springboot连接Redis的教程详解的文章就介绍到这了,更多相关springboot连接Redis内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。