欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot连接Redis的教程详解

程序员文章站 2022-06-23 08:10:41
创建springboot项目在nosql中选择redis项目目录pom.xml中还需要加入下面的jar包org.springframework.boot spring-boot-starter-jso...

创建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上的练习项目中也有)

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 扁平化(将对象中的参数展开)
    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());


    //发布订阅
    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;
  }
}

到此这篇关于springboot连接redis的教程详解的文章就介绍到这了,更多相关springboot连接redis内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!