Redis数据库五大数据类型的增删改查实例——使用Jedis实现

一、运行环境

该实例在maven中运行。

在pom文件中加入jedis依赖:

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.3.0</version>
</dependency>

二、代码实现

  • JedisPoolUtil文件:该文件用于创建JedisPoolConfig对象,通过该对象获取Jedis对象。
package cn.krain;

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

/** * @author CC * @data 2020/8/14 - 15:52 */
public class JedisPoolUtil {
   

    private static JedisPool pool = null;

    public static JedisPool getJedisPool(int port, String IP){
   

        if (pool==null){
   
            //创建JedisPoolConfig,给config设置连接池的参数,使用config对象创建JedisPool
            JedisPoolConfig config = new JedisPoolConfig();

            //设置最大线程数,一个线程就是一个redis
            config.setMaxTotal(20);

            //设置最大空闲数
            config.setMaxIdle(2);

            //设置检查为true,确保拿到的jedis是可用的
            config.setTestOnBorrow(true);

            //创建pool对象
            pool = new JedisPool(config, IP, port, 6000, "123456");
        }
        return pool;
    }

    //关闭pool
    public static void close(){
   
        if (pool!=null){
   
            pool.close();
        }
    }

}
  • JedisPoolTest文件:通过Jedis对redis数据库进行增删改查。
package cn.krain;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;
import java.util.Set;

/** * @author CC * @data 2020/8/14 - 16:08 */
public class JedisPoolTest {
   

    String IP = "192.168.174.132";         //redis所在服务器IP地址
    int port = 6379;        //端口号

    JedisPool pool = JedisPoolUtil.getJedisPool(port, IP);

    Jedis jedis = null;

    @Test
    public void StringTest(){
   
        try {
   
            //从pool中获取jedis对象
            jedis = pool.getResource();

            //初始化一个String
            jedis.set("str1","value1");
            String str1 = jedis.get("str1");
            Long num = jedis.strlen("str1");       //获取指定key的字符长度
            jedis.set("str2","value2");
            List<String> strList = jedis.mget("str1","str2");   //一次获取多个string类型数据

            System.out.println("str1="+str1);

            System.out.println("str1的长度为:"+num);

            for(String str : strList){
   
                System.out.println(str);
            }
        }finally {
   
            pool.close();       //关闭jedis,将获取的jedis放回到pool中
        }
    }

    @Test
    public void HashTest(){
   

        try {
   
            //从pool中获取jedis对象
            jedis = pool.getResource();

            //初始化一个hash集合
            jedis.hset("hash1","k1","v1");
            jedis.hset("hash1","k2","v2");
            jedis.hset("hash1","k3","v3");

            String k2 = jedis.hget("hash1","k2");

            System.out.println("k2的值:"+k2);

            Map<String,String> map = jedis.hgetAll("hash1");

            for (int i = 1; i <= map.size(); i++) {
   
                System.out.println(map.get("k"+i));
            }
        }finally {
   
            pool.close();   //关闭jedis,将获取的jedis放回到pool中
        }
    }

    @Test
    public void ListTest(){
   
        try {
   
            //从pool中获取jedis对象
            jedis = pool.getResource();

            //初始化一个list
            jedis.lpush("list1","value1", "value2", "value3");

            //根据下表获取list中的值
            String value = jedis.lindex("list1", 1);

            System.out.println(value);
        }finally {
   
            pool.close();   //关闭jedis,将获取的jedis放回到pool中
        }
    }

    @Test
    public void SetTest(){
   
        try {
   
            //从pool中获取jedis对象
            jedis = pool.getResource();

            //初始化一个set集合
            jedis.sadd("set1","value1", "value2", "value3");

            Long num = jedis.scard("set1");      //统计set中数据的个数
            System.out.println("set中数据的条数:"+num);

            Set<String> set = jedis.smembers("set1");
            for(String str : set){
   
                System.out.println(str);
            }
        }finally {
   
            pool.close();   //关闭jedis,将获取的jedis放回到pool中
        }
    }

    @Test
    public void ZSetTest(){
   
        try {
   
            //从pool中获取jedis对象
            jedis = pool.getResource();

            //初始化一个ZSet
            jedis.zadd("zset1",50,"java");
            jedis.zadd("zset1",30,"mysql");
            jedis.zadd("zset1",20,"jsp");

            Set<String> set =  jedis.zrange("zset1",0,1);
            for(String str : set){
   
                System.out.println(str);
            }
        }finally {
   
            pool.close();   //关闭jedis,将获取的jedis放回到pool中
        }
    }

}
全部评论

相关推荐

双非坐过牢:非佬,可以啊10.28笔试,11.06评估11.11,11.12两面,11.19oc➕offer
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务