码迷,mamicode.com
首页 > 数据库 > 详细

db#redis#jedis

时间:2020-04-06 17:10:59      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:配置文件   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();
    }
}

 

db#redis#jedis

标签:配置文件   import   str   package   关闭   stat   depend   resource   ack   

原文地址:https://www.cnblogs.com/luohaonan/p/12642740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!