-
pom.xml
<!--redis 相关jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> -
application.properties
spring.redis.host= 127.0.0.1 spring.redis.port= 6379 -
config
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @Configuration @PropertySource(value="classpath:bootstrap.properties") public class RedisConfig { private Logger log = (Logger) LoggerFactory.getLogger(RedisConfig.class); @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Bean public JedisPool getJedisPool(){ log.info("==>初始化jedis连接池"); JedisPoolConfig config = new JedisPoolConfig(); JedisPool pool = new JedisPool(config, host, port); return pool; } } -
在 要用的类中注入:
@Autowired private JedisPool jedisPool; -
方法中使用:
Jedis redis = jedisPool.getResource(); redis.set("key值","value"); redis.lpush("key值","value"); redis.expire("key值",时间:秒); //llen 的方法 取出list长度 redis.llen(phoneNumber + format);
springboot 中的redis配置(Jedis )(非集群)
最新推荐文章于 2025-04-26 16:31:51 发布
本文详细介绍了如何在SpringBoot应用中配置并使用Jedis连接Redis,包括pom.xml的依赖添加,application.properties的配置,以及相关类的注入和使用方法。
538

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



