springboot連接Redis的教程詳解
創(chuàng)建springboot項目


在NoSQL中選擇Redis


項目目錄

pom.xml中還需要加入下面的jar包
org.springframework.boot spring-boot-starter-json
在application.properties文件中添加Redis服務(wù)器信息
spring.redis.host=192.168.5.132 spring.redis.port=6379
剩下4個test類,我直接以源碼的方式粘出來,里面有些代碼是非必須的,我保留了測試的驗證過程,所以里面會有冗余代碼。(這些代碼在我GitHub上的練習(xí)項目中也有)
package com.myspringboot.redis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public 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;
@Configuration
public 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;
@Component
public 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 扁平化(將對象中的參數(shù)展開)
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());
//發(fā)布訂閱
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;
}
}
到此這篇關(guān)于springboot連接Redis的教程詳解的文章就介紹到這了,更多相關(guān)springboot連接Redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot實現(xiàn)自定義Redis的連接的流程步驟
- SpringBoot無法連接redis的解決方案
- springBoot連接遠(yuǎn)程Redis連接失敗的問題解決
- 關(guān)于SpringBoot集成Lettuce連接Redis的方法和案例
- springboot連接不上redis的三種解決辦法
- springboot連接redis并動態(tài)切換database的實現(xiàn)方法
- springboot 如何使用jedis連接Redis數(shù)據(jù)庫
- springboot2整合redis使用lettuce連接池的方法(解決lettuce連接池?zé)o效問題)
- 基于SpringBoot2.0默認(rèn)使用Redis連接池的配置操作
- Springboot2.X集成redis集群(Lettuce)連接的方法
- Spring?Boot2?整合連接?Redis的操作方法
相關(guān)文章
SkyWalking?自定義插件(Spring?RabbitMQ)具體分析過程
這篇文章主要介紹了SkyWalking?自定義插件(Spring?RabbitMQ)具體分析過程,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02
springboot?vue前后端接口測試樹結(jié)點添加功能
這篇文章主要為大家介紹了springboot?vue前后端接口測試樹結(jié)點添加功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
jdk8使用stream實現(xiàn)兩個list集合合并成一個(對象屬性的合并)
本文主要介紹了jdk8使用stream實現(xiàn)兩個list集合合并成一個(對象屬性的合并),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
VsCode配置java環(huán)境的詳細(xì)圖文教程
vscode是一個免費的代碼編輯器,支持多種主題,應(yīng)用起來簡單方便,下面這篇文章主要給大家介紹了關(guān)于VsCode配置java環(huán)境的詳細(xì)圖文教程,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文)
這篇文章主要介紹了使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05

