1.引入依赖
        <!-- Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Redis 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
2.配置
文件path: /项目目录/src/main/resources/application.properties
# Redis库的编号
spring.redis.database=0
# Redis实例地址
spring.redis.host=127.0.0.1
# Redis实例端口号,默认6379
spring.redis.port=6379
# Redis登录密码
#spring.redis.password=Easy@0122
# Redis连接池最大连接数
spring.redis.jedis.pool.max-active=10
# Redis连接池最大空闲连接数
spring.redis.jedis.pool.max-idle=10
# Redis连接池最小空闲连接数
spring.redis.jedis.pool.min-idle=0
等同于 文件path:/项目目录/src/main/resources/application.yml
spring:
  redis:
    database: 0           # Redis库的编号
    host: 127.0.0.1       # Redis实例地址
    port: 6379            # Redis实例端口号,默认6379
    password: Easy@0122    # Redis登录密码
    jedis:
      pool:
        max-active: 10    # Redis连接池最大连接数
        max-idle: 10      # Redis连接池最大空闲连接数
        min-idle: 0       # Redis连接池最小空闲连接数
3.序列化配置
文件path: /项目目录/src/main/java/com/example/demo/config/RedisConfig.java
package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        // 序列化器
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        // 设置key和hashKey的序列化器
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        // 设置value和hashValue的序列化器
        template.setValueSerializer(jsonRedisSerializer);
        template.setHashValueSerializer(jsonRedisSerializer);
        // return  template
        return template;
    }
}
4.如何使用
文件path: /项目目录/src/main/java/com/example/demo/controller/HomeController.java
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
@RestController
public class HomeController {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @GetMapping("/")
    public String home() {
        return "Hello World";
    }
    @GetMapping("/set")
    public String set() {
        Map<String, Object> user = new HashMap<>();
        user.put("name", "小明");
        user.put("enName", "xiao" + "ming");
        user.put("age", 18);
        redisTemplate.opsForValue().set("aaa", "aaa" + "aaa");
        redisTemplate.opsForValue().set("bbb", "bbb" + "bbb");
        redisTemplate.opsForValue().set("ccc", user);
        return "set";
    }
    @GetMapping("/get")
    public String get(HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        JSONObject entries = new JSONObject();
        entries.set("aaa", redisTemplate.opsForValue().get("aaa"));
        entries.set("bbb", redisTemplate.opsForValue().get("bbb"));
        entries.set("ccc", redisTemplate.opsForValue().get("ccc"));
        return JSONUtil.toJsonStr(entries);
    }
}