您的位置:首页 > 数据库 > Redis

Redis 数据库连接池编写

2020-04-01 19:03 846 查看

个人博客

Utils 类编写

public class JedisPoolUtils {

private static JedisPool jedisPool;

static{
//读取配置文件
InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
//创建Properties对象
Properties pro = new Properties();
//关联文件
try {
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
}
//获取数据,设置到JedisPoolConfig中
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));

//初始化JedisPool
jedisPool = new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port")));

}
/**
* 获取连接方法
*/
public static Jedis getJedis(){
return jedisPool.getResource();
}
}

获取连接池工具类

//通过连接池工具类获取
Jedis jedis = JedisPoolUtils.getJedis();
  • 点赞
  • 收藏
  • 分享
  • 文章举报
William_GJIN 发布了21 篇原创文章 · 获赞 0 · 访问量 447 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: