<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.3</version>
</dependency>
//设置ip地址和端口
Jedis jedis = new Jedis("127.0.0.1", 6379);
//保存数据
jedis.set("name", "张三");
//获取数据
String value = jedis.get("name");
System.out.println(value);
//释放资源
jedis.close();
//获取连接池的配置对象
JedisPoolConfig config = new JedisPoolConfig();
//设置最大的连接数
config.setMaxTotal(30);
//设置最大空闲连接数
config.setMaxIdle(10);
//获得连接池
JedisPool jedisPool = new JedisPool(config, "127.0.0.1", 6379);
//获得核心对象
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set("productName", "电脑");
System.out.println(jedis.get("productName"));
System.out.println(jedis.get("name"));
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
if (null != jedis) {
jedis.close();
}
if (null!=jedisPool){
jedisPool.close();
}
}