springboot访问redis用RedisTemplate还是jedis
mini云码 发布日期: 2025-11-02 22:55
在springboot出现之前,老一辈的java程序员,习惯了使用jedis来操作redis。
而在springboot出现之后,就不再需要使用jedis来操作redis了,因为有更简单的RedisTemplate,RedisTemplate只需要在配置文件做一些配置,就可以直接注入RedisTemplate了,无需自己做客户端的初始化工作。相比jedis来说,方便了初始化方面的步骤。
spring.redis.host=127.0.0.1
spring.redis.port=6379
//假如是集群版则设置spring.redis.nodes属性,不设置host和port
#spring.redis.password=123456
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=300在pom.xml文件引入对应的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>3.1.4</version><!--版本号由你自己项目的springboot版本决定-->
</dependency>
在配置文件配置好后,就可以直接引用了:
@Autowired
private RedisTemplate redisTemplate;
public Object get(String key){
return redisTemplate.opsForValue().get(key);
}
