springboot連接Redis的教程詳解
創(chuàng)建springboot項(xiàng)目
在NoSQL中選擇Redis
項(xiàng)目目錄
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個(gè)test類,我直接以源碼的方式粘出來,里面有些代碼是非必須的,我保留了測(cè)試的驗(yàn)證過程,所以里面會(huì)有冗余代碼。(這些代碼在我GitHub上的練習(xí)項(xiàng)目中也有)
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中直接查看時(shí),亂碼 redisTemplate.opsForValue().set("key1", "hello1"); System.out.println(redisTemplate.opsForValue().get("key1")); //redis中直接查看時(shí),正常 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 扁平化(將對(duì)象中的參數(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot實(shí)現(xiàn)自定義Redis的連接的流程步驟
- SpringBoot無法連接redis的解決方案
- springBoot連接遠(yuǎn)程Redis連接失敗的問題解決
- 關(guān)于SpringBoot集成Lettuce連接Redis的方法和案例
- springboot連接不上redis的三種解決辦法
- springboot連接redis并動(dòng)態(tài)切換database的實(shí)現(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)文章
SpringBoot自定義對(duì)象參數(shù)實(shí)現(xiàn)自動(dòng)類型轉(zhuǎn)換與格式化
SpringBoot 通過自定義對(duì)象參數(shù),可以實(shí)現(xiàn)自動(dòng)類型轉(zhuǎn)換與格式化,并可以級(jí)聯(lián)封裝,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09MyBatis-Plus詳解(環(huán)境搭建、關(guān)聯(lián)操作)
MyBatis-Plus 是一個(gè) MyBatis 的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生,今天通過本文給大家介紹MyBatis-Plus環(huán)境搭建及關(guān)聯(lián)操作,需要的朋友參考下吧2022-09-09