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>
Test categories:
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. close the connection jedis.close (); } }
Small tools:
Profile jedis.properties
#fileName jedis.properties jedis.host=127.0.0.1 jedis.port=6379 jedis.maxTotal=30 jedis.maxIdle=10
Tools:
Import the java.util.ResourceBundle; / ** * @author haonan * @version 1.0 * @date 2020/4/6 16:29 * / public class JedisUtils { Private static JedisPool JP = null ; static { // the getBundle incoming configuration name of the file, do not write the name suffix ResourceBundle rb = ResourceBundle.getBundle ( "jedis" ); JedisPoolConfig jpc = new 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(); } }