标签:配置文件 import str package 关闭 stat depend resource ack
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>jedis</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
测试类:
package com.haonan.jedis; import org.junit.Test; import redis.clients.jedis.Jedis; /** * @author haonan * @version 1.0 * @date 2020/4/6 16:23 */ public class JedisTest { @Test public void testjedis() { //1.连接redis Jedis jedis = new Jedis("localhost", 6379); //2.操作redis jedis.set("kagome", "inuyasha"); String kagome = jedis.get("kagome"); System.out.println(kagome); //3.关闭连接 jedis.close(); } }
小工具类:
配置文件jedis.properties
#fileName jedis.properties jedis.host=127.0.0.1 jedis.port=6379 jedis.maxTotal=30 jedis.maxIdle=10
工具类:
import java.util.ResourceBundle; /** * @author haonan * @version 1.0 * @date 2020/4/6 16:29 */ public class JedisUtils { private static JedisPool jp = null; static { //getBundle传入配置文件的名称,不用写后缀名称 ResourceBundle rb = ResourceBundle.getBundle("jedis"); JedisPoolConfig jpc = new JedisPoolConfig(); jpc.setMaxTotal(Integer.parseInt(rb.getString("jedis.maxTotal"))); jpc.setMaxIdle(Integer.parseInt(rb.getString("jedis.maxIdle"))); jp = new JedisPool(jpc, rb.getString("jedis.host"), Integer.parseInt(rb.getString("jedis.port"))); } public static Jedis getJedis() { return jp.getResource(); } }
标签:配置文件 import str package 关闭 stat depend resource ack
原文地址:https://www.cnblogs.com/luohaonan/p/12642740.html