Java连接Redis之redis的增删改查

一、新建一个 maven 工程,工程可以以 jar 的形式或 war 都行,然后导入正确的依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<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>com.test</groupId>
  <artifactId>redis</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.5.0</version>
        </dependency>
 
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
     
     
     
    <build>
        <plugins>
 
 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
 
 
 
 
        </plugins>
 
 
 
    </build>
 
     
     
</project>

 二、建一个连接 redis 数据库的工具类

package com.xiangjun.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil {
  // 服务器 IP 地址
private static String ADDR = "23.228.103.10";
  
  // 端口
private static int PORT = 6379;
  // 密码
private static String AUTH = "123456";
  // 连接实例的最大连接数
private static int MAX_ACTIVE = 1024;
  
  //控制一个 pool 最多有多少个状态为 idle(空闲的) 的 jedis 实例,默认值也是 8。
private static int MAX_IDLE = 200;
  
//等待可用连接的最大时间,单位毫秒,默认值为 -1,表示永不超时。如果超过等待时间,则直接抛出 JedisConnectionException
private static int MAX_WAIT = 10000;
    
  // 连接超时的时间  
private static int TIMEOUT = 10000;
  // 在 borrow 一个 jedis 实例时,是否提前进行 validate 操作;如果为 true,则得到的 jedis 实例均是可用的;
private static boolean TEST_ON_BORROW = true;

</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> JedisPool jedisPool = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 初始化Redis连接池
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> {

    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {

        JedisPoolConfig config </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPoolConfig();
        config.setMaxTotal(MAX_ACTIVE);
        config.setMaxIdle(MAX_IDLE);
        config.setMaxWaitMillis(MAX_WAIT);
        config.setTestOnBorrow(TEST_ON_BORROW);
        jedisPool </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);

    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {

        e.printStackTrace();
    }

}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 获取Jedis实例
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">synchronized</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Jedis getJedis() {

    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {

        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (jedisPool != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
            Jedis resource </span>=<span style="color: rgba(0, 0, 0, 1)"> jedisPool.getResource();
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> resource;
        } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
        }

    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
        e.printStackTrace();
        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    }

}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">*
 * 
 * 释放资源
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> returnResource(<span style="color: rgba(0, 0, 255, 1)">final</span><span style="color: rgba(0, 0, 0, 1)"> Jedis jedis) {
        </span><span style="color: rgba(0, 0, 255, 1)">if</span>(jedis != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
            jedisPool.returnResource(jedis);
        }
    
}

}

 

三、redis 的增删改查的测试

package com.xiangjun.redis;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import redis.clients.jedis.Jedis;

public class TestRedis {

</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Jedis jedis;

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * 连接redis服务器
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> connectRedis() {
    jedis</span>=<span style="color: rgba(0, 0, 0, 1)">RedisUtil.getJedis();
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * redis操作字符串
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testString() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加数据</span>
    jedis.set("name", "youcong"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.get(</span>"name"<span style="color: rgba(0, 0, 0, 1)">));
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">拼接字符串</span>
    jedis.append("name", ".com"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.get(</span>"name"<span style="color: rgba(0, 0, 0, 1)">));
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除数据</span>
    jedis.del("name"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.get(</span>"name"<span style="color: rgba(0, 0, 0, 1)">));
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置多个键值对</span>
    jedis.mset("name","yc","age","22","qq","1933108196"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.incr(</span>"age");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加1操作</span>
    System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" +jedis.get("qq"<span style="color: rgba(0, 0, 0, 1)">));
}


</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * redis操作map集合
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testMap() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加数据</span>
    Map&lt;String,String&gt; map = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;String,String&gt;<span style="color: rgba(0, 0, 0, 1)">();
    
    map.put(</span>"name", "yc"<span style="color: rgba(0, 0, 0, 1)">);
    map.put(</span>"age", "22"<span style="color: rgba(0, 0, 0, 1)">);
    map.put(</span>"qq", "1933108196"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.hmset(</span>"user"<span style="color: rgba(0, 0, 0, 1)">, map);
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取出users中的Name,执行结果:[minxr]--&gt;注意结果是一个泛型的List
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第一个参数是存入redis中map对象的key,后面跟的是放入map中对象的key,后面的key可以是多个,是可变的</span>
    List&lt;String&gt; rsmap = jedis.hmget("user", "name","age","qq"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(rsmap);
    

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除map中的某个键值</span>
    jedis.hdel("user", "age"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.hmget(</span>"user", "age"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">因为删除了,所以返回的是Null</span>
    System.out.println(jedis.hlen("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回key为user的键中存放的值的个数2</span>
    System.out.println(jedis.exists("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否存在key为user的记录,返回true</span>
    System.out.println(jedis.hkeys("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回map对象中的所有key</span>
    System.out.println(jedis.hvals("user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回map对象中的所有value</span>
Iterator<String> iter = jedis.hkeys("user").iterator(); while(iter.hasNext()) { String key = iter.next(); System.out.println(key+":" + jedis.hmget("user", key));}
}

</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * redis操作List集合
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testList() {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">开始前,先移除所有的内容</span>
    jedis.del("java framework"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.lrange(</span>"java framework", 0, -1<span style="color: rgba(0, 0, 0, 1)">));
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">先向key java framework 中存放三条数据</span>
    jedis.lpush("java framework","spring"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.lpush(</span>"java framework", "struts"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.lpush(</span>"java framework", "hibernate"<span style="color: rgba(0, 0, 0, 1)">);
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">再取出所有数据jedis.lrange是按范围取出
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有</span>
    System.out.println(jedis.lrange("java framework", 0, -1<span style="color: rgba(0, 0, 0, 1)">));
    
    jedis.del(</span>"java framework"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.rpush(</span>"java framework", "spring"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.rpush(</span>"java framework", "struts"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.rpush(</span>"java framework","hibernate"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.lrange(</span>"java framework", 0, -1<span style="color: rgba(0, 0, 0, 1)">));
    
    
}


</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * redis操作set集合
 * 
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSet() {
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加</span>
    jedis.sadd("user", "liuling"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.sadd(</span>"user", "xinxin"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.sadd(</span>"user","ling"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.sadd(</span>"user", "zhangxinxin"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.sadd(</span>"user", "who"<span style="color: rgba(0, 0, 0, 1)">);
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除</span>
    jedis.srem("user", "who"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.smembers(</span>"user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取所有加入的value</span>
    System.out.println(jedis.sismember("user", "who"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">判断who是否是user集合的元素</span>
    System.out.println(jedis.srandmember("user"<span style="color: rgba(0, 0, 0, 1)">));
    System.out.println(jedis.scard(</span>"user"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回集合的元素个数</span>
}


</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * redis排序
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testSort() {
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">jedis 排序
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注意,此处的rpush和lpush是List的操作。是一个双向链表(但从表现来看的)</span>
    jedis.del("a");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">先清除数据,再加入数据进行测试</span>
    jedis.rpush("a", "1"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.lpush(</span>"a", "6"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.lpush(</span>"a", "3"<span style="color: rgba(0, 0, 0, 1)">);
    jedis.lpush(</span>"a", "9"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(jedis.lrange(</span>"a", 0, -1<span style="color: rgba(0, 0, 0, 1)">));
    System.out.println(jedis.sort(</span>"a"));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">[1,3,6,9] </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">输入排序后结果</span>
    System.out.println(jedis.lrange("a", 0, -1<span style="color: rgba(0, 0, 0, 1)">));
    
    
    
    
}


</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)">
 * redis连接池
 </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> testRedisPool() {
    
    RedisUtil.getJedis().set(</span>"newname", "test"<span style="color: rgba(0, 0, 0, 1)">);
    
    System.out.println(RedisUtil.getJedis().get(</span>"newname"<span style="color: rgba(0, 0, 0, 1)">));
    
    
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
    TestRedis test </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> TestRedis();
    test.connectRedis();
    test.testSort();
}

}