整合篇2:Spring Boot整合Redis(Lettuce方式)

4,018 阅读1分钟

1.pom依赖引入

<!-- redis:缓存数据库 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.5.6</version>
</dependency>
<!-- commons-pool2:实现对象池化的框架,没有的话启动时redis报错 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.11.1</version>
</dependency>

2.yaml配置文件

#redis
redis:
  # redis数据库索引(默认为0)
  database: 0
  # redis服务器地址(默认为localhost)
  host: 182.92.153.8
  # redis端口(默认为6379)
  port: 6379
  # redis访问密码(默认为空)
  password:
  # redis连接超时时间(单位毫秒):不能设置为0,否则报无法连接
  timeout: 6000ms
  # redis高级客户端
  lettuce:
    pool:
      # 最大可用连接数(默认为8,负数表示无限)
      max-active: 8
      # 最大空闲连接数(默认为8,负数表示无限)
      max-idle: 8
      # 最小空闲连接数(默认为0,该值只有为正数才有用)
      min-idle: 0
      # 从连接池中获取连接最大等待时间(默认为-1,单位为毫秒,负数表示无限)
      max-wait: -1

3.LettuceRedisConfig配置类

package com.zhigong.heavenearth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * @author 田治功
 * @description Redis配置类:Lettuce客户端
 * @date 2021-10-31 1:36
 */
@Configuration
public class LettuceRedisConfig {

    @Bean
    public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());//StringRedisSerializer:序列化为String
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());//GenericJackson2JsonRedisSerializer:序列化为JSON,同时在json中加入@class属性,类的全路径包名,方便反系列化
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);//设置连接工厂
        return redisTemplate;
    }
}

4.数据操作

package com.zhigong.heavenearth.service;


import com.zhigong.heavenearth.common.Result;
import com.zhigong.heavenearth.dao.SkillStudyPlanMapper;
import com.zhigong.heavenearth.pojo.SkillStudyPlan;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 田治功
 * @description 技术学习规划表服务类
 * @date 2021-12-14
 */

@Service
public class SkillStudyPlanService {

    @Resource
    private SkillStudyPlanMapper skillStudyPlanMapper;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 查询所有技术学习规划
     *
     * @return
     */
    public Result selectAll() {

        SkillStudyPlan skillStudyPlan = skillStudyPlanMapper.selectOne(null);
        ValueOperations<String, SkillStudyPlan> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(skillStudyPlan.getStudyId().toString(), skillStudyPlan);
        SkillStudyPlan skillStudyPlan1 = skillStudyPlanMapper.selectAll();
        HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
        hashOperations.put("TEST", skillStudyPlan1.getStudyId().toString(), skillStudyPlan1);
        return Result.success(skillStudyPlan);
    }

}

5.RDM管理

image.png

6.Util工具类

redisTemplate中的工具类并不能完全满足日常的开发需求,需要进行定制化的工具类编写,余将根据日常需求整理相应的工具类,力图汇总较为完善和详细的工具类,后续更新