• Redis(一)--Redis入门(3)--Java客户端-Jedis


    Java客户端-Jedis

    Jedis是Java语言开发的Redis客户端工具包。
    Jedis只是对Redis命令的封装,掌握Redis命令便可轻易上手。

    环境准备

    打开Xshell连接虚拟机,切换目录到redis安装目录,执行vim redis.conf打开redis配置文件,找到如图位置
    在这里插入图片描述
    protected-mode - 是否开启保护模式,开启保护模式后只有指定的IP可以访问Redis,在开发需要,先设置为no,然后往上寻找到如图位置在这里插入图片描述
    把bind的值改为0.0.0.0保存,让所有主机的IP都可以访问进来(只限开发环境这么改,生产环境勿用)
    然后执行./src/redis-server redis.conf启动redis。
    然后防火墙放开6379端口,执行firewall-cmd --zone=public --add-port=6379/tcp --permanent,然后重载firewall-cmd --reload

    Jedis的使用入门

    启动IDEA,新建工程,
    在这里插入图片描述
    在这里插入图片描述
    在pom.xml中添加jedis依赖

        <dependencies>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
            </dependency>
        </dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    com.ql.jedis包下创建JedisTestor类

    package com.ql.jedis;
    
    import redis.clients.jedis.Jedis;
    
    import java.util.List;
    
    public class JedisTestor {
        public static void main(String[] args) {
            Jedis jedis = null;
            try {
                jedis = new Jedis("192.168.52.128", 6379);
                jedis.auth("12345");
                jedis.select(2);
                System.out.println("Redis连接成功");
                //字符串
                jedis.set("sn", "789789789");
                String sn = jedis.get("sn");
                System.out.println(sn);
                jedis.mset(new String[]{"title", "奶粉","num","20"});
                List<String> mget = jedis.mget(new String[]{"sn", "title", "num"});
                System.out.println(mget);
                Long num = jedis.incr("num");
                System.out.println(num);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                jedis.close();
            }
        }
    }
    /*
    Redis连接成功
    789789789
    [789789789, 奶粉, 20]
    21
    */
    
    • 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

    Jedis操作Hash类型

      //Hash
      jedis.hset("student:1001","name","张晓明");
      String name = jedis.hget("student:1001", "name");
      System.out.println(name);
      Map<String,String> studentMap = new HashMap<>();
      studentMap.put("name","李兰");
      studentMap.put("age","18");
      studentMap.put("id","1002");
      jedis.hmset("student:1002", studentMap);
      Map<String, String> stringStringMap = jedis.hgetAll("student:1002");
      System.out.println(stringStringMap);
      /*
    张晓明
    {name=李兰, age=18, id=1002}
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    Jedis操作List类型

    //List
    jedis.del("list");
    jedis.rpush("list", new String[]{"d","e","f"});
    jedis.lpush("list",new String[]{"c","b","a"});
    List<String> list = jedis.lrange("list", 0, -1);
    System.out.println(list);
    jedis.rpop("list");
    jedis.lpop("list");
    List<String> list1 = jedis.lrange("list", 0, -1);
    System.out.println(list1);
    /*
    [a, b, c, d, e, f]
    [b, c, d, e]
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    利用Jedis缓存数据

    首先com.ql.jedis包下创建Goods 实体类。

    package com.ql.jedis;
    
    public class Goods {
        private Integer goodsId;
        private String goodsName;
        private String description;
        private Float price;
    
        public Integer getGoodsId() {
            return goodsId;
        }
    
        public void setGoodsId(Integer goodsId) {
            this.goodsId = goodsId;
        }
    
        public String getGoodsName() {
            return goodsName;
        }
    
        public void setGoodsName(String goodsName) {
            this.goodsName = goodsName;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public Float getPrice() {
            return price;
        }
    
        public void setPrice(Float price) {
            this.price = price;
        }
    }
    
    
    • 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

    然后在pom.xml文件中添加依赖

     <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>fastjson</artifactId>
         <version>1.2.76</version>
     </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    创建CacheSample.java文件

    package com.ql.jedis;
    
    import com.alibaba.fastjson.JSON;
    import redis.clients.jedis.Jedis;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class CacheSample {
        public CacheSample(){
            Jedis jedis = new Jedis("192.168.52.128");
            try {
                List<Goods> goodsList = new ArrayList<>();
                goodsList.add(new Goods(1001, "苹果","",3f));
                goodsList.add(new Goods(1002, "橙子","",4f));
                goodsList.add(new Goods(1003, "香蕉","",5f));
                jedis.auth("12345");
                jedis.select(3);
                for (Goods goods: goodsList){
                    String json = JSON.toJSONString(goods);
                    System.out.println(json);
                    String key = "goods:"+goods.getGoodsId();
                    jedis.set(key,json);
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                jedis.close();
            }
        }
    
        public static void main(String[] args) {
            new CacheSample();
            System.out.println("请输入要查询的商品编号:");
            String goodsId = new Scanner(System.in).next();
            Jedis jedis = new Jedis("192.168.52.128");
            try {
                jedis.auth("12345");
                jedis.select(3);
                String key = "goods:"+goodsId;
                if(jedis.exists(key)){
                    String json = jedis.get(key);
                    System.out.println(json);
                    Goods goods = JSON.parseObject(json, Goods.class);
                    System.out.println(goods.getGoodsName());
                    System.out.println(goods.getPrice());
                }else{
                    System.out.println("您输入的商品编号不存在,请重新输入!");
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    /*
    {"description":"","goodsId":1001,"goodsName":"苹果","price":3.0}
    {"description":"","goodsId":1002,"goodsName":"橙子","price":4.0}
    {"description":"","goodsId":1003,"goodsName":"香蕉","price":5.0}
    请输入要查询的商品编号:
    1002
    {"description":"","goodsId":1002,"goodsName":"橙子","price":4.0}
    橙子
    4.0
    */
    
    • 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
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
  • 相关阅读:
    ChatGPT App迎来重大更新;人工智能应用于应对气候变化
    java springboot获取GitLab上的文件内容
    理解编辑距离算法
    数据结构——堆排序
    Idea中 css 、js 压缩插件会自动生成xxx.min.css、xxx.min.js文件
    MQ常见的问题(kafka保证消息不丢失)
    开始使用AspectJ-实现步骤@Aspect,@Before,还有其中的JoinPoint参数
    el-select 绑定对象和el-checkbox-group用法
    QT中文乱码解决方案与乱码的原因
    从kafka如何保证数据一致性看通常数据一致性设计
  • 原文地址:https://blog.csdn.net/qq_32091929/article/details/125400824