Jedis操作

Jedis

  • Jedis: 一款java操作redis数据库的工具.
    所需jar包 或者 依赖
    在这里插入图片描述
    在这里插入图片描述pom.xml依赖
		<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.4.2</version>
        </dependency>

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<String, String> hhh = jedis.hgetAll("hhh");
        Set<String> 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<String> lll = jedis.lrange("lll", 0, -1);
        System.out.println(lll);
        List<String> 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<String> 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<String> zzz = jedis.zrange("zzz", 0, -1);
        //zrange(key, start, end)获得key中的集合(从小到大,没有分数,只有value值)
        Set<Tuple> zzz1 = jedis.zrangeWithScores("zzz", 0, -1);
        //zrangeWithScores(key, start, end)获得key中的集合(从小到大,有分数,不过注意一下返回的是一个Tuple元组,
        //里面的有相应的方法能将字节数组变为String类型)
        Set<String> zzz2 = jedis.zrangeByScore("zzz", 55, 60);
        //zrangeByScore(key, min, max)根据score的值最小和最大范围来获得key中的集合(从小到大,没有分数,只有value值)
        Set<Tuple> zzz3 = jedis.zrangeByScoreWithScores("zzz", 55, 60);
        //zrangeByScoreWithScores(key, min, max)根据score的值最小和最大范围来获得key中的集合
        //(从小到大,有分数有value值,不过和上面那个withscores方法一样返回的是Tuple元组)
        Set<String> 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<Tuple> 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);
    }

在这里插入图片描述

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值