Spring boot是1.5版本。
- Maven
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>1.5.17.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
- 通用类
... @Configuration public class JedisUtil { @Value("${redis.host}") private String host; @Value("${redis.port}") private int port; @Value("${redis.password}") private String password; @Value("${redis.pool.max-idle}") private int maxIdle; @Value("${redis.pool.min-idle}") private int minIdle; @Value("${redis.pool.max-total}") private int maxTotal; @Value("${redis.pool.max-wait-millis}") private long maxWaitMillis; @Bean public JedisConnectionFactory redisConnectionFactory() { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(maxIdle); config.setMinIdle(minIdle); config.setMaxTotal(maxTotal); config.setMaxWaitMillis(maxWaitMillis); JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setPoolConfig(config); factory.setHostName(host); factory.setPort(port); if (null != password && !"".equals(password)) { factory.setPassword(password); } return factory; } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(redisConnectionFactory); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); template.setKeySerializer(stringRedisSerializer); template.setHashKeySerializer(stringRedisSerializer); template.setValueSerializer(stringRedisSerializer); template.setHashValueSerializer(stringRedisSerializer); return template; } } ...
- 配置节
redis: host: 127.0.0.2 port: 6379 password: abcdefg pool: max-idle: 8 min-idle: 1 max-total: 8 max-wait-millis: 6000
- 调用
... redisTemplate.opsForHash().put(key1, key2, value); ...