一、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); } }