最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot連接Redis的教程詳解

 更新時(shí)間:2021年03月04日 14:08:15   作者:觸初  
這篇文章主要介紹了springboot連接Redis的教程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

創(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類,我直接以源碼的方式粘出來,里面有些代碼是非必須的,我保留了測試的驗(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 扁平化(將對象中的參數(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java 取模與取余的區(qū)別說明

    java 取模與取余的區(qū)別說明

    這篇文章主要介紹了java 取模與取余的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • SkyWalking?自定義插件(Spring?RabbitMQ)具體分析過程

    SkyWalking?自定義插件(Spring?RabbitMQ)具體分析過程

    這篇文章主要介紹了SkyWalking?自定義插件(Spring?RabbitMQ)具體分析過程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • springboot?vue前后端接口測試樹結(jié)點(diǎn)添加功能

    springboot?vue前后端接口測試樹結(jié)點(diǎn)添加功能

    這篇文章主要為大家介紹了springboot?vue前后端接口測試樹結(jié)點(diǎn)添加功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Java實(shí)現(xiàn)堆算法的使用示例

    Java實(shí)現(xiàn)堆算法的使用示例

    本文主要介紹了Java實(shí)現(xiàn)堆算法的使用示例,Java中提供了一個(gè)Heap類,可以用來實(shí)現(xiàn)堆的操作,可以實(shí)現(xiàn)如插入、刪除、獲取最大最小值等,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • jdk8使用stream實(shí)現(xiàn)兩個(gè)list集合合并成一個(gè)(對象屬性的合并)

    jdk8使用stream實(shí)現(xiàn)兩個(gè)list集合合并成一個(gè)(對象屬性的合并)

    本文主要介紹了jdk8使用stream實(shí)現(xiàn)兩個(gè)list集合合并成一個(gè)(對象屬性的合并),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 使用idea自動(dòng)生成序列化ID全過程

    使用idea自動(dòng)生成序列化ID全過程

    這篇文章主要介紹了使用idea自動(dòng)生成序列化ID全過程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java多線程之synchronized同步代碼塊詳解

    Java多線程之synchronized同步代碼塊詳解

    這篇文章主要為大家詳細(xì)介紹了Java多線程之synchronized同步代碼塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Java介紹多線程計(jì)算階乘實(shí)現(xiàn)方法

    Java介紹多線程計(jì)算階乘實(shí)現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了Java多線程計(jì)算階乘的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • VsCode配置java環(huán)境的詳細(xì)圖文教程

    VsCode配置java環(huán)境的詳細(xì)圖文教程

    vscode是一個(gè)免費(fèi)的代碼編輯器,支持多種主題,應(yīng)用起來簡單方便,下面這篇文章主要給大家介紹了關(guān)于VsCode配置java環(huán)境的詳細(xì)圖文教程,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • 使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文)

    使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文)

    這篇文章主要介紹了使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-05-05

最新評論

南漳县| 壤塘县| 来安县| 绥化市| 左云县| 科尔| 临高县| 巫溪县| 武陟县| 钟祥市| 体育| 台东市| 灵武市| 江北区| 普格县| 赞皇县| 丽水市| 西畴县| 和硕县| 新宾| 隆化县| 镇坪县| 潜山县| 尼玛县| 灵台县| 综艺| 黑山县| 博客| 武功县| 望都县| 呼图壁县| 铜山县| 河间市| 银川市| 谢通门县| 徐汇区| 尉犁县| 高州市| 庆云县| 石狮市| 靖州|