栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Jedis操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Jedis操作

Jedis
  • Jedis: 一款java操作redis数据库的工具.
    所需jar包 或者 依赖

    pom.xml依赖
		
            redis.clients
            jedis
            2.9.0
        
        
            org.apache.commons
            commons-pool2
            2.4.2
        

Jedis的操作跟Redis在cli.exe的操作差不多

字符串String类型操作
public void test(){
        //Jedis jedis = new Jedis();无参构造 默认"localhost",6379端口
        Jedis jedis = new Jedis("localhost",6379);
        jedis.set("username","zhangsan");
        String username = jedis.get("username");
        jedis.del("username");
        System.out.println(username);
        jedis.close();
    }

哈希Hash类型操作
public void test(){
        Jedis jedis = new Jedis();
        jedis.hset("hhh","name","zkr");
        jedis.hset("hhh","age","20");
        jedis.hset("hhh","address","196.168.0.1");
        Map hhh = jedis.hgetAll("hhh");
        Set set = hhh.keySet();
        for (String key : set) {
            System.out.println(key+" : "+hhh.get(key));
        }
        System.out.println(hhh);
        jedis.close();
    }

列表List类型操作
@Test
    public void test4(){
        Jedis jedis = new Jedis("localhost",6379);
        Long lpush = jedis.lpush("lll", "a", "b", "c", "d");
        System.out.println(lpush);
        Long rpush = jedis.rpush("rrr", "a", "b", "c", "d");
        System.out.println(rpush);
        List lll = jedis.lrange("lll", 0, -1);
        System.out.println(lll);
        List rrr = jedis.lrange("rrr", 0, -1);
        System.out.println(rrr);
        jedis.close();
    }

集合set类型操作
@Test
    public void test5(){
        Jedis jedis = new Jedis("localhost",6379);
        jedis.sadd("sett","a","b","c");
        jedis.sadd("sett","a","d","e");
        Set set = jedis.smembers("sett");
        System.out.println(set);
        jedis.close();
    }

有序集合sortedset类型操作

有序集合比较特殊,所以获取方法也比较多,下面就在注释中解释

	@Test
    public void test6(){
        Jedis jedis = new Jedis("localhost",6379);
        jedis.zadd("zzz",50,"zhangsan");
        jedis.zadd("zzz",60,"lisi");
        jedis.zadd("zzz",55,"wangwu");
        Set zzz = jedis.zrange("zzz", 0, -1);
        //zrange(key, start, end)获得key中的集合(从小到大,没有分数,只有value值)
        Set zzz1 = jedis.zrangeWithScores("zzz", 0, -1);
        //zrangeWithScores(key, start, end)获得key中的集合(从小到大,有分数,不过注意一下返回的是一个Tuple元组,
        //里面的有相应的方法能将字节数组变为String类型)
        Set zzz2 = jedis.zrangeByScore("zzz", 55, 60);
        //zrangeByScore(key, min, max)根据score的值最小和最大范围来获得key中的集合(从小到大,没有分数,只有value值)
        Set zzz3 = jedis.zrangeByScoreWithScores("zzz", 55, 60);
        //zrangeByScoreWithScores(key, min, max)根据score的值最小和最大范围来获得key中的集合
        //(从小到大,有分数有value值,不过和上面那个withscores方法一样返回的是Tuple元组)
        Set zzz4 = jedis.zrevrange("zzz", 0, -1);
        //zrevrange(key, start, end)获得key中的集合(从大到小,没有分数,只有value值)
        System.out.println(zzz);
        System.out.println(zzz1);
        System.out.println(zzz2);
        System.out.println(zzz3);
        System.out.println(zzz4);
        //将元组中的字节数组变为String类型操作方法
        Iterator iterator = zzz3.iterator();
        while(iterator.hasNext()){
            Tuple tuple = iterator.next();
            System.out.println(tuple.getElement()+" "+tuple.getScore());
        }
        jedis.close();
    }

将jedis.zrangeWithScores返回值元组变为String类型输出是从这里学来的,点这里查看详情

Jedis根据连接池或者配置文件初始化 连接池获取
@Test
    public void test7(){
        //0.建立一个配置对象先配置一下东西,可以有也可以没有
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(50);
        config.setMaxIdle(10);
        //通过连接池创建Jedis对象
            //有配置文件时
            JedisPool jedisPool = new JedisPool(config,"localhost",6379);
            Jedis jedis = jedisPool.getResource();
            //无参构造
            JedisPool jedisPool1 =new JedisPool();
            Jedis jedis1 = jedisPool1.getResource();
        //相关操作
        jedis.set("pool","0");
        jedis1.set("pool1","1");
        String pool = jedis.get("pool");
        String pool1 = jedis1.get("pool1");
        System.out.println(pool);
        System.out.println(pool1);
        //关闭返回连接池中
        jedis.close();
        jedis1.close();
    }

配置文件初始化

设置jedis.properties配置对象

host=localhost
port=6379
maxTotal=50
maxIdle=10

设置JedisPoolUtils工具类

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

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JedisPoolUtils {
    private static JedisPool jedisPool;
    static {
        Properties pro = new Properties();
        InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
        try {
            pro.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
        config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
        jedisPool = new JedisPool(config,pro.getProperty("host"), Integer.parseInt(pro.getProperty("port")));
    }
    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
}

操作阶段

@Test
    public void test8() {
        Jedis jedis = JedisPoolUtils.getJedis();
        jedis.set("poolUtils","120");
        String poolUtils = jedis.get("poolUtils");
        System.out.println(poolUtils);
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/305469.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号