JedisPoolUtils工具类

JedisPoolUtils工具类

  • 步骤及:
  • 0.创建数据库连接池类的成员变量(使用后面作为对象)
  • 1.加载配置文件,获取配置文件的参数,并将参数设置为成员变量(数据库连接池对象)的配置参数
  • 2.提供获取数据库连接的方法
  • 3.关闭连接:归还
  • 需要的配置文件:jedis.properties
  • 数据库需要的jar包:commons-pool2-2.3.jar和jedis-2.7.0.jar

public class JedisPoolUtils {

// 创建Jedispool成员变量
private static JedisPool JedisPool;

// 1.加载配置文件:静态代码块
static {
    // 1.1 创建一个Properties对象
    Properties pro = new Properties();
    // 1.2 获取配置文件的连接
    InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
    try {
        pro.load(is);
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 1.3 设置连接池配置信息
    JedisPoolConfig config = new JedisPoolConfig();
    // 将字符串转为整型
    config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
    config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
    // 1.4 设置好数据库连接池对象
    JedisPool = new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port")));
}


// 2.获取连接的方法
public static Jedis getJedis(){
    return JedisPool.getResource();
}

// 3.关闭连接
public static void close(Jedis jedis){
    if (jedis != null){
        jedis.close();
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值