1.pom.xml
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2.在Application主类中
初始化一个jedis连接池。
@Bean
public JedisPool jedisPool() {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(100);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000 * 10);
config.setTestOnBorrow(true);
return new JedisPool(config, "localhost", 6379);
}
3.在需要使用的类中
@Autowired
private JedisPool jedisPool;
将jedisPool注入进来。
public void xxx(xxx xxx) {
Jedis jedis = jedisPool.getResource();
jedis.set(xxx, xxx);
}
在方法中获取jedis,然后调用jedis的方法。