SpringBoot整合Redis(通俗易懂版)

文章目录

  • SpringBoot整合Redis
    • 一、快速上手
        • 1、导入依赖
        • 2、配置连接
        • 3、测试
        • 4、运行效果
    • 二、遇到问题并解决
        • 1、实体类
        • 2、Redis序列化配置
        • 3、再次测试
        • 4、运行结果

SpringBoot整合Redis

一、快速上手

1、导入依赖

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-redisartifactId>
dependency>

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
    <groupId>org.projectlombokgroupId>
    <artifactId>lombokartifactId>
dependency>

2、配置连接

application.yaml

spring:
  redis:
    #如果是本地连接的话,使用127.0.0.1
    host: 110.41.20.61
    port: 6379
    #我这边配置了密码,一般是没有密码的(可忽略)
    password: 123456

3、测试

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
public class MyTest {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    void contextLoads(){
        redisTemplate.opsForValue().set("mykey","张三");
        System.out.println(redisTemplate.opsForValue().get("mykey"));
    }

}

4、运行效果

SpringBoot整合Redis(通俗易懂版)_第1张图片

二、遇到问题并解决

查看Redis数据库,发现乱码

SpringBoot整合Redis(通俗易懂版)_第2张图片

怎么解决?
加上序列化配置即可

1、实体类

User

注意:实体类记得要继承Serializable序列化 【常用方法】

@Component
@Data
@NoArgsConstructor
@AllArgsConstructor

//在企业中,我们的所有pojo都会序列化
public class User implements Serializable{
    private String name;
    private Integer age;

}

2、Redis序列化配置

常用
RedisConfig


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.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.net.UnknownHostException;

//固定模板,拿去直接使用

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        // 将template 泛型设置为 
        RedisTemplate<String, Object> template = new RedisTemplate();
        // 连接工厂,不必修改
        template.setConnectionFactory(redisConnectionFactory);
        /*
         * 序列化设置
         */
        // key、hash的key 采用 String序列化方式
        template.setKeySerializer(RedisSerializer.string());
        template.setHashKeySerializer(RedisSerializer.string());
        // value、hash的value 采用 Jackson 序列化方式
        template.setValueSerializer(RedisSerializer.json());
        template.setHashValueSerializer(RedisSerializer.json());
        template.afterPropertiesSet();

        return template;
    }
}


3、再次测试

import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
public class MyTest {
    @Autowired
    private RedisTemplate redisTemplate;
    
    @Test
    public void test() throws JsonProcessingException {
        //开发一般都是使用json来传播存储对象
        User user = new User("酷小亚", 12);
        /**
         * 一定要有序列化  不然报SerializationException 序列化异常
         * 1、在实体类中直接继承序列化。
         * 2、或者添加如下代码
         * String s = new ObjectMapper().writeValueAsString(user);
         */
         redisTemplate.opsForValue().set("ku",user);
        System.out.println(redisTemplate.opsForValue().get("ku"));
    }
}

4、运行结果

SpringBoot整合Redis(通俗易懂版)_第3张图片

恭喜你,简单的入门啦!

不过, 聪明的你应该能够感察到,当我们使用RedisTemplate时,就需要频繁调用.opForxxx然后才能进行对应的操作。
工作中可不会这样使用哦,而是将这些常用的公共API抽取出来封装成为一个工具类,然后直接使用工具类来间接操作Redis,不但效率高并且易用。

下篇文章:

Redis封装工具类:https://blog.csdn.net/weixin_45737330/article/details/127224981


如果你觉得这篇文章对你有点帮助的话,就点个赞吧!
你的鼓励就是我写作下去的动力哦!

你可能感兴趣的:(Redis,SpringBoot2,redis,spring,boot,java)