看到好多大佬写的整合 SpringBoot + Redis 文章已经比较旧了 而且很繁琐
我整合优化了一下 欢迎大佬来敲打
yml 配置文件
server:
port: 8081
spring:
# 数据库相关配置 我用的是 mysql 8.0.x
datasource:
url: jdbc:mysql://127.0.0.1:3306/你自己的库名称?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
driver-class-name: com.mysql.cj.jdbc.Driver
username: xxxx
password: xxxx
# Redis 相关配置 我用的 redis 6.0.X 关于6.0的新特性自己去了解
redis:
host: 127.0.0.1
port: 6379
#建议都把redis密码配置上
password: 123456
#连接超时时间(毫秒)
timeout: 2000
pool:
#最大连接数(负数表示没有限制)
max-active: 100
#最大空闲连接
max-idle: 10
#最大阻塞等待时间(负数表示没有限制)
max-wait: 100000
database: 0
mybatis-plus:
mapper-locations: classpath:mapper/*Mapper.xml
#你自己的实体类包名
type-aliases-package: com.xxx.xxx.xxx
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
maven全部依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<!-- my baits plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!-- json 工具包 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
<!-- @Data 工具包 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
新建一个配置类
package com.manager.violet.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**/
@Configuration
@Data
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Bean
public RedisTemplate<?,?> redisTemplate() {
RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
redisConfig.setHostName(host);
redisConfig.setPassword(password);
redisConfig.setPort(port);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisConfig);
//让配置生效 否则会报错
//java.lang.IllegalStateException: JedisConnectionFactory was not initialized through afterPropertiesSet()
jedisConnectionFactory.afterPropertiesSet();
RedisTemplate<?,?> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(jedisConnectionFactory);
//key序列化方式;但是如果方法上有Long等非String类型的话,会报类型转换错误;
RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型不可以会出现异常信息;
redisTemplate.setKeySerializer(redisSerializer);
redisTemplate.setHashKeySerializer(redisSerializer);
//JdkSerializationRedisSerializer序列化方式;
JdkSerializationRedisSerializer jdkRedisSerializer = new JdkSerializationRedisSerializer();
redisTemplate.setValueSerializer(jdkRedisSerializer);
redisTemplate.setHashValueSerializer(jdkRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
使用方法
以上内容确认无误后
@Autowired
private RedisTemplate redisTemplate;
//RedisTemplate 这个工具类有很多方法 操作不同的数据类型
//比如还有 redisTemplate.opsForSet(); 操作Set类型 详情自行百度
//这里仅以 String 为例
ValueOperations valueOperations = redisTemplate.opsForValue();
public void test(){
{
//存
valueOperations.set(key, value);
}
{
//存并设置过期时间
valueOperations.set(key, value);
redisTemplate.expire(key, secondTime, TimeUnit.SECONDS);
}
{
//取
valueOperations.get(key)
}
}