当前位置:   article > 正文

在idea中操作redis数据库(五种数据类型)_idea redis

idea redis

 创建JedisTests测试类,为了避免重复重建和销毁Jedis对象,使用注解创建和销毁

@Before  @After
  1. private Jedis jedis;
  2. @Before
  3. public void redisConnection(){
  4. jedis= JedisPoolUtil.getJedis();
  5. }
  6. @After
  7. public void redisClose(){
  8. jedis.close();
  9. }

string类型
  1. @Test
  2. public void redisString(){
  3. //添加成功返回ok
  4. System.out.println(jedis.set("k","hello redis"));
  5. //获取键的值
  6. System.out.println(jedis.get("k"));;
  7. //获取键的数据类型 不存在返回none
  8. System.out.println(jedis.type("k"));
  9. //获取key对应value的长度 不存在返回0
  10. System.out.println(jedis.strlen("k"));
  11. //查询key的过期时间 不存在返回-2 -1永不过期
  12. System.out.println(jedis.ttl("k"));
  13. //设置多组键值对
  14. jedis.mset("k1","value1","k2","value2");
  15. //查询多个键的值
  16. System.out.println( jedis.mget("k1","k2"));
  17. }

集合:
  1. @Test
  2. public void redisList(){
  3. //返回值 1
  4. /* System.out.println( jedis.rpush("list","小元,小满,小白"));;
  5. jedis.lpush("list","小李,小鸣,小佐");*/
  6. /* System.out.println(jedis.llen("list"));
  7. System.out.println(jedis.lpop("list"));//返回被删除的元素
  8. System.out.println(jedis.rpop("list"));//返回被删除的元素
  9. System.out.println(jedis.llen("list"));*/
  10. System.out.println(jedis.lrange("list",0,-1));
  11. System.out.println(jedis.lindex("list",0));
  12. }

set集合:
  1. @Test
  2. public void redisSet(){
  3. //返回添加的个数
  4. /* System.out.println(jedis.sadd("number","java"));
  5. System.out.println(jedis.sadd("number","redis","script","javaEE"));
  6. */
  7. //查询set中的所有元素
  8. System.out.println(jedis.smembers("number"));
  9. /*
  10. System.out.println(jedis.spop("number"));//随机删除左边第一个元素 返回删除的数据
  11. System.out.println(jedis.srem("number","java"));//指定删除
  12. System.out.println(jedis.smembers("number"));
  13. */
  14. //判断某元素是否存在 true存在
  15. System.out.println(jedis.sismember("number","java"));
  16. //随机查询
  17. System.out.println(jedis.srandmember("number",1));
  18. //获取长度
  19. System.out.println(jedis.scard("number"));
  20. }

哈希:
  1. @Test
  2. public void redisHash(){
  3. /* System.out.println(jedis.hset("user","username","tom")); 1
  4. System.out.println(jedis.hget("user","username" ));*/
  5. /* Map m =new HashMap<>();
  6. m.put("age","18");
  7. m.put("gender","男");
  8. System.out.println(jedis.hmset("user",m));// ok
  9. System.out.println(jedis.hmget("user","age","gender"));*/
  10. /* //获取所有的键
  11. System.out.println(jedis.hkeys("user"));
  12. //获取所有的值
  13. System.out.println(jedis.hvals("user"));
  14. //获取长度
  15. System.out.println(jedis.hlen("user"));
  16. //键中是否有某属性
  17. System.out.println(jedis.hexists("user","username"));
  18. */
  19. //查看所有的键值
  20. // System.out.println(jedis.hgetAll("user"));
  21. //删除某属性
  22. System.out.println(jedis.hdel("user","gender"));
  23. }

有序集合:

  1. @Test
  2. public void redisOrderedSets(){
  3. /* jedis.zadd("odd",10,"java");
  4. jedis.zadd("odd",20,"javaEE");
  5. jedis.zadd("odd",30,"javaWeb");
  6. jedis.zadd("odd",40,"javaScprit");*/
  7. //查询所有
  8. System.out.println(jedis.zrange("odd",0,-1));
  9. //分数从高到低
  10. System.out.println(jedis.zrevrange("odd",0,-1));
  11. //查询指定区间的数据
  12. System.out.println(jedis.zrangeByScore("odd",30,40));
  13. //返回指定的分数取值
  14. System.out.println(jedis.zscore("odd","javaWeb"));
  15. //查询元素的个数
  16. System.out.println(jedis.zcard("odd"));
  17. /* System.out.println(jedis.zrem("odd","java","javaEE"));//返回删除的个数
  18. System.out.println(jedis.zcard("odd"));*/
  19. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/512651
推荐阅读
相关标签
  

闽ICP备14008679号