时间:2021-05-19
创建springboot项目
在NoSQL中选择Redis
项目目录
pom.xml中还需要加入下面的jar包
org.springframework.boot spring-boot-starter-json
在application.properties文件中添加Redis服务器信息
spring.redis.host=192.168.5.132spring.redis.port=6379剩下4个test类,我直接以源码的方式粘出来,里面有些代码是非必须的,我保留了测试的验证过程,所以里面会有冗余代码。(这些代码在我GitHub上的练习项目中也有)
package com.myspringboot.redis;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplicationpublic class DemoApplication { public static void main(String[] args) { ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(DemoApplication.class, args); RedisTest redisTest = configurableApplicationContext.getBean(RedisTest.class); redisTest.testRedis(); }}package com.myspringboot.redis;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;@Configurationpublic class MyTemplate { @Bean public StringRedisTemplate getMyTemplate(RedisConnectionFactory redisConnectionFactory){ StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory); stringRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class)); return stringRedisTemplate; }}package com.myspringboot.redis;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.data.redis.connection.Message;import org.springframework.data.redis.connection.MessageListener;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.core.HashOperations;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.hash.Jackson2HashMapper;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.stereotype.Component;import java.util.Map;@Componentpublic class RedisTest { @Autowired RedisTemplate redisTemplate; @Autowired StringRedisTemplate stringRedisTemplate; @Autowired ObjectMapper objectMapper; // 自定义模板 @Autowired @Qualifier("getMyTemplate") StringRedisTemplate myStringRedisTemplate; public void testRedis() { //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")); RedisConnection connection = redisTemplate.getConnectionFactory().getConnection(); connection.set("key3".getBytes(), "hello3".getBytes()); System.out.println(new String(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(new Jackson2JsonRedisSerializer<Object>(Object.class)); Jackson2HashMapper jackson2HashMapper = new Jackson2HashMapper(objectMapper, false);// true 扁平化(将对象中的参数展开) User user = new User(); user.setId(123); user.setName("zhangsan"); stringRedisTemplate.opsForHash().putAll("key5", jackson2HashMapper.toHash(user)); Map map = stringRedisTemplate.opsForHash().entries("key5"); User user1 = objectMapper.convertValue(map, User.class); System.out.println(user1.getId()); System.out.println(user1.getName()); myStringRedisTemplate.opsForHash().putAll("key6", jackson2HashMapper.toHash(user)); Map map1 = myStringRedisTemplate.opsForHash().entries("key6"); User user2 = objectMapper.convertValue(map, User.class); System.out.println(user2.getId()); System.out.println(user2.getName()); //发布订阅 myStringRedisTemplate.convertAndSend("qunliao", "hello"); RedisConnection connection1 = myStringRedisTemplate.getConnectionFactory().getConnection(); connection1.subscribe(new MessageListener() { @Override public void onMessage(Message message, byte[] bytes) { byte[] body = message.getBody(); System.out.println(new String(body)); } }, "qunliao".getBytes()); while (true){ myStringRedisTemplate.convertAndSend("qunliao", "hello"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } }}package com.myspringboot.redis;public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}到此这篇关于springboot连接Redis的教程详解的文章就介绍到这了,更多相关springboot连接Redis内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
简介在springboot使用搭建好的redis集群添加redis和连接池依赖org.apache.commonscommons-pool2org.spring
SpringBoot2.0默认采用Lettuce客户端来连接Redis服务端的默认是不使用连接池的,只有配置redis.lettuce.pool下的属性的时候才
使用了SpringBoot的项目,在远程连接Redis服务器时,会遇倒一些小问题,这里剖析一下。1.首先,要在pom文件中添加redis依赖:org.sprin
在上一节中,我带大家学习了详解SpringBoot集成Redis来实现缓存技术方案,尤其是结合SpringCache的注解的实现方案,接下来在本章节中,我带大家
Redis整合SpringBoot》》RedisService接口?1234567891011121314151617181920212223242526272