springboot连接Redis的教程详解

 更新时间:2021年03月04日 14:08:15   作者:触初  
这篇文章主要介绍了springboot连接Redis的教程详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

创建springboot项目

在这里插入图片描述
在这里插入图片描述

在NoSQL中选择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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java 取模与取余的区别说明

    java 取模与取余的区别说明

    这篇文章主要介绍了java 取模与取余的区别说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • SkyWalking 自定义插件(Spring RabbitMQ)具体分析过程

    SkyWalking 自定义插件(Spring RabbitMQ)具体分析过程

    这篇文章主要介绍了SkyWalking 自定义插件(Spring RabbitMQ)具体分析过程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-02-02
  • springboot vue前后端接口测试树结点添加功能

    springboot vue前后端接口测试树结点添加功能

    这篇文章主要为大家介绍了springboot vue前后端接口测试树结点添加功能,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Java实现堆算法的使用示例

    Java实现堆算法的使用示例

    本文主要介绍了Java实现堆算法的使用示例,Java中提供了一个Heap类,可以用来实现堆的操作,可以实现如插入、删除、获取最大最小值等,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • jdk8使用stream实现两个list集合合并成一个(对象属性的合并)

    jdk8使用stream实现两个list集合合并成一个(对象属性的合并)

    本文主要介绍了jdk8使用stream实现两个list集合合并成一个(对象属性的合并),文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • 使用idea自动生成序列化ID全过程

    使用idea自动生成序列化ID全过程

    这篇文章主要介绍了使用idea自动生成序列化ID全过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • Java多线程之synchronized同步代码块详解

    Java多线程之synchronized同步代码块详解

    这篇文章主要为大家详细介绍了Java多线程之synchronized同步代码块,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • Java介绍多线程计算阶乘实现方法

    Java介绍多线程计算阶乘实现方法

    这篇文章主要为大家详细介绍了Java多线程计算阶乘的实现,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • VsCode配置java环境的详细图文教程

    VsCode配置java环境的详细图文教程

    vscode是一个免费的代码编辑器,支持多种主题,应用起来简单方便,下面这篇文章主要给大家介绍了关于VsCode配置java环境的详细图文教程,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-02-02
  • 使用maven整合Spring+SpringMVC+Mybatis框架详细步骤(图文)

    使用maven整合Spring+SpringMVC+Mybatis框架详细步骤(图文)

    这篇文章主要介绍了使用maven整合Spring+SpringMVC+Mybatis框架详细步骤(图文),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-05-05

最新评论