springboot连接Redis的教程详解

时间:2022-04-27 17:49:38

创建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服务器信息

?
1
2
spring.redis.host=192.168.5.132
spring.redis.port=6379

剩下4个test类,我直接以源码的方式粘出来,里面有些代码是非必须的,我保留了测试的验证过程,所以里面会有冗余代码。(这些代码在我github上的练习项目中也有)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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();
  }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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();
      }
    }
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/jt781861965/article/details/114199289