Redis—尚硅谷—周阳

  • Post author:
  • Post category:其他


十一.Redis 的 Java 客户端 Jedis 和 SpringBoot 整合 Redis


1)jedis 所需 jar 包:

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.2.0</version>
        </dependency>



2)jedis 常用操作:



a:测试连通性:

    public static void main(String[] args) {

        Jedis jedis = new Jedis("114.215.173.88",6379);
        System.out.println(jedis.ping());

        String ww = jedis.set("user:2022", "王五");
        System.out.println(ww);

    }



b:5 + 1:


(5种数据类型)

    public static void main(String[] args) {

        Jedis jedis = new Jedis("114.215.173.88", 6379);

        /**
         *  key 操作
         */
        Set<String> keys = jedis.keys("*");
        for (String str : keys) {
            String value = jedis.get(str);
            System.out.println(str + " : " + value);
        }
        // 输出:
        // user:2021 : 张三
        // user:2022 : 王五

        /**
         *  List 操作(存放、删除)
         */
        Long lpush = jedis.lpush("my:list", "a", "b", "c");
        List<String> lrange = jedis.lrange("my:list", 0, -1);
        System.out.println(lrange.toString());
        // 输出:
        // [c, b, a]

        /**
         *  操作set
         */
        Long sadd = jedis.sadd("my:set", "a", "b", "c");
        Set<String> set = jedis.smembers("my:set");
        System.out.println(set.toString());
        // 输出:
        // [b, c, a]

        /**
         * 操作 hash
         */
        HashMap<String, String>



版权声明:本文为qq_43056248原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。