如何配置SpringBoot连接Jedis数据库?
时间: 2024-05-15 20:14:49 浏览: 16
首先,在pom.xml文件中添加Redis和Jedis依赖,然后在application.properties文件中配置Redis连接信息,例如:
```
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=yourpassword
```
接着,在Java代码中注入Jedis连接池,并在需要使用Redis的地方调用Jedis连接池中的资源,例如:
```java
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private int timeout;
@Bean
public JedisPool redisPoolFactory() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(30);
jedisPoolConfig.setMaxWaitMillis(1000 * 100);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
return jedisPool;
}
}
@Autowired
private JedisPool jedisPool;
public void set(String key, String value) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
jedis.close();
}
}
}
```
以上就是配置SpringBoot连接Jedis数据库的基本步骤,可以根据具体情况进行调整。