在SpringBoot2中使用Jedis:
引入maven依赖:
org.springframework.boot spring-boot-starter-data-redis
application.properties配置文件内容:
server.port=8080 #Redis服务器地址 spring.redis.host=192.168.75.200 #Redis服务器连接端口 spring.redis.port=6379 #Redis数据库索引(默认为0) spring.redis.database= 0 #连接超时时间(毫秒) spring.redis.timeout=1800000 #连接池最大连接数(使用负值表示没有限制) spring.redis.lettuce.pool.max-active=20 #最大阻塞等待时间(负数表示没限制) spring.redis.lettuce.pool.max-wait=-1 #连接池中的最大空闲连接 spring.redis.lettuce.pool.max-idle=5 #连接池中的最小空闲连接 spring.redis.lettuce.pool.min-idle=0
测试使用redisTemplate:
@EnableAutoConfiguration
@RestController
@RequestMapping("/redisTest")
public class RedisTestController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping
public String testRedis() {
//设置值到redis
redisTemplate.opsForValue().set("name","lucy");
//从redis获取值
String name = (String)redisTemplate.opsForValue().get("name");
return name;
}
}
执行方法后发现Redis保存的数据:
这是因为RedisTemplate未进行序列化,进行序列化操作:
编写配置类:
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate
配置类中的RedisConnectionFactory 需要引入依赖:
org.apache.commons commons-pool2
但引入此依赖时出现问题:
pom.xml文件解析报错:
Could not transfer artifact org.apache.commons:commons-pool2:pom:2.6.2 from/to central (https://repo.maven.apache.org/maven2): transfer failed for https://repo.maven.apache.org/maven2/org/apache/commons/commons-pool2/2.6.2/commons-pool2-2.6.2.pom
解决方法:
在这两处都加上:-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true
再重启IDEA之后问题解决!
重新操作Redis数据库:
新加的name成功:



