Springboot连接Redis的详细教程

本篇内容主要讲解“Springboot连接Redis的详细教程”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Springboot连接Redis的详细教程”吧!

创建springboot项目

Springboot连接Redis的详细教程 Springboot连接Redis的详细教程

在NoSQL中选择Redis

Springboot连接Redis的详细教程 Springboot连接Redis的详细教程

项目目录

Springboot连接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的详细教程”有了更深的了解,不妨来实际操作一番吧!这里是恰卡编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

发布于 2021-07-09 21:19:15
收藏
分享
海报
0 条评论
170
上一篇:thinkphp中怎么实现左右值无限分类 下一篇:swift的类属性作用是什么
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码