欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot連接Redis的教程詳解

 更新時(shí)間:2021年03月04日 14:08:15   作者:觸初  
這篇文章主要介紹了springboot連接Redis的教程詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(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類,我直接以源碼的方式粘出來,里面有些代碼是非必須的,我保留了測(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java HtmlParse提取標(biāo)簽中的值操作

    Java HtmlParse提取標(biāo)簽中的值操作

    這篇文章主要介紹了Java HtmlParse提取標(biāo)簽中的值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 基于Java語言的遞歸運(yùn)算例題詳解

    基于Java語言的遞歸運(yùn)算例題詳解

    一個(gè)方法在執(zhí)行過程中調(diào)用自身, 就稱為 "遞歸"。本文將通過幾個(gè)例題帶大家深入了解一下Java語言中的遞歸運(yùn)算,感興趣的可以了解一下
    2022-08-08
  • Java封裝好的mail包發(fā)送電子郵件的類

    Java封裝好的mail包發(fā)送電子郵件的類

    本文給大家分享了2個(gè)java封裝好的mail包發(fā)送電子郵件的類,并附上使用方法,小伙伴們可以根據(jù)自己的需求自由選擇。
    2016-01-01
  • SpringBoot自定義對(duì)象參數(shù)實(shí)現(xiàn)自動(dòng)類型轉(zhuǎ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-09
  • Java ThreadLocal有什么作用你知道嗎

    Java ThreadLocal有什么作用你知道嗎

    這篇文章主要為大家詳細(xì)介紹了java ThreadLocal的作用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-09-09
  • JSP安全開發(fā)之XSS漏洞詳解

    JSP安全開發(fā)之XSS漏洞詳解

    XSS又叫CSS (Cross Site Script) ,跨站腳本攻擊。它指的是惡意攻擊者往Web頁面里插入惡意腳本代碼,而程序?qū)τ谟脩糨斎雰?nèi)容未過濾,當(dāng)用戶瀏覽該頁之時(shí),嵌入其中Web里面的腳本代碼會(huì)被執(zhí)行,從而達(dá)到惡意攻擊用戶的特殊目的。
    2016-09-09
  • MyBatis-Plus詳解(環(huán)境搭建、關(guān)聯(lián)操作)

    MyBatis-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
  • Java中URL的處理方法詳解

    Java中URL的處理方法詳解

    URL(Uniform?Resource?Locator)中文名為統(tǒng)一資源定位符,有時(shí)也被俗稱為網(wǎng)頁地址,表示為互聯(lián)網(wǎng)上的資源,本文主要為大家介紹了Java是如何處理URL的,感興趣的可以了解一下
    2023-05-05
  • SpringBoot框架搭建教程分享

    SpringBoot框架搭建教程分享

    這篇文章主要為大家詳細(xì)介紹了SpringBoot框架搭建教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Java實(shí)現(xiàn)聊天機(jī)器人完善版

    Java實(shí)現(xiàn)聊天機(jī)器人完善版

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)聊天機(jī)器人完善版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07

最新評(píng)論