目录
一、pom.xml依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--如果使用lettuce连接池的话就不用导入jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
二、application.properties配置
## redis setting
spring.redis.host=127.0.0.1
spring.redis.password=123456
spring.redis.port=6379
## redis jedis pool setting
spring.redis.jedis.pool.max-active=20
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.min-idle=5
## redis lettuce pool setting
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.min-idle=5
三、java配置类
1.配置Redis连接池JedisPool
2.配置RedisTemplate存值取值序列化
package com.leadpms.qianlistandard.web.config;
import org.springframework.beans.factory.annotation.Autowired;
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.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author Shaoyu Liu
* @date 2021/5/28 15:50
**/
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
// @Bean
// public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPool,
// RedisStandaloneConfiguration jedisConfig) {
// JedisConnectionFactory connectionFactory = new JedisConnectionFactory(jedisConfig);
// connectionFactory.setPoolConfig(jedisPool);
// return connectionFactory;
// }
//
// @Configuration
// public static class JedisPoolConf {
// @Value("${spring.redis.host:127.0.0.1}")
// private String host;
// @Value("${spring.redis.port:6379}")
// private Integer port;
// @Value("${spring.redis.password:}")
// private String password;
// @Value("${spring.redis.database:0}")
// private Integer database;
//
// @Value("${spring.redis.jedis.pool.max-active:8}")
// private Integer maxActive;
// @Value("${spring.redis.jedis.pool.max-idle:8}")
// private Integer maxIdle;
// @Value("${spring.redis.jedis.pool.max-wait:-1}")
// private Long maxWait;
// @Value("${spring.redis.jedis.pool.min-idle:0}")
// private Integer minIdle;
//
// @Bean
// public JedisPoolConfig jedisPool() {
// JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// jedisPoolConfig.setMaxIdle(maxIdle);
// jedisPoolConfig.setMaxWaitMillis(maxWait);
// jedisPoolConfig.setMaxTotal(maxActive);
// jedisPoolConfig.setMinIdle(minIdle);
// return jedisPoolConfig;
// }
//
// @Bean
// public RedisStandaloneConfiguration jedisConfig() {
// RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
// config.setHostName(host);
// config.setPort(port);
// config.setDatabase(database);
// config.setPassword(RedisPassword.of(password));
// return config;
// }
// }
}

四、单元测试
import com.leadpms.qianlistandard.dao.entity.auto.User;
import com.leadpms.qianlistandard.service.redis.RedisService;
import com.leadpms.qianlistandard.web.WebApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author Shaoyu Liu
* @date 2021/5/28 16:57
**/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = WebApplication.class)
public class RedisTest2 {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void put() {
User u = new User();
u.setName("刘邵宇");
u.setAccount("liushaoyu");
redisTemplate.opsForValue().set("liushaoyu", u);
redisTemplate.opsForHash().put("user:", "1", u);
// 可以返回map映射,并且RedisConfig需要配置template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
Object userByKey = redisTemplate.opsForValue().get("liushaoyu");
// 只有hash类型可以强转为对象,并且RedisConfig需要配置template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
User userByHashKey = (User) redisTemplate.opsForHash().get("user:", "1");
// 只可以转为object类型 什么也没做,只是对key value hashkey hashvalue做了默认序列化处理,stringRedisTemplate返回的是一个字符串无法转为map类型
Object o = stringRedisTemplate.opsForHash().get("user:", "1");
System.out.println(1);
}
}

本文详细介绍了如何在SpringBoot项目中整合Redis,包括在pom.xml添加依赖,application.properties配置Redis连接池JedisPool,以及Java配置类中设置RedisTemplate进行序列化操作,并进行了单元测试验证。
2万+

被折叠的 条评论
为什么被折叠?



