jedis工具类
package cn.liyang.utils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.ResourceBundle;
/**
* @author liyang
* @date 2019/7/30 9:50
* @description:数据库连接的工具类 目的: 返回连接数据库的对象 读取config.properties配置文件
*/
public class JedisUtils {
private static JedisPool pool ;
//读取配置文件
static {
ResourceBundle resourceBundle = ResourceBundle.getBundle("config");
//获取服务器的IP 端口号
String host = resourceBundle.getString("host");
//获取最大连接数 空闲数
int port = Integer.parseInt(resourceBundle.getString("port"));
int maxTotal = Integer.parseInt(resourceBundle.getString("maxTotal"));
int maxIdle = Integer.parseInt(resourceBundle.getString("maxIdle"));
//创建连接池的配置信息
JedisPoolConfig config = new JedisPoolConfig();
//设置相关的信息
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
//创建连接池对象
pool = new JedisPool(config, host, port);
}
public static Jedis getJedis() {
return pool.getResource();
}
//关闭连接
public static void close(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
//关闭连接池
public static void close(JedisPool pool) {
if (pool != null) {
pool.close();
}
}
}
host=localhost
port=6379
maxTotal=50
maxIdle=20
package cn.liyang.test;
import cn.liyang.utils.JedisUtils;
import redis.clients.jedis.Jedis;
/**
* @author liyang
* @date 2019/7/30 10:06
* @description:
*/
public class JedisUtilsTest {
public static void main(String[] args) {
Jedis jedis = JedisUtils.getJedis();
System.out.println(jedis.get("name"));
}
}
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
</dependencies>
