快速入门Redis -- JavaApi操作Redis(全网最详细)

  在日常的工作学习中需要操作Redis,JavaApi操作Redis是一个必不可少的技术那么我们直接进入正题

 目录

         1.创建maven项目导入相应的jar包

         2.创建初始化方法

         3.操作Redis字符串类型

         4.操作Redis Hash类型

         5.操作Redis list类型

         6.操作Redis set类型

         7.关闭资源


1.创建maven项目导入相应的jar包

   <dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.1.0</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <!--    <verbal>true</verbal>-->
                </configuration>
            </plugin>
        </plugins>
    </build>

2.创建初始化方法

    // 实例连接池
    private JedisPool pool;

    /**
     * 初始化操作
     */
    @BeforeTest
    public void init() {
        pool = new JedisPool("node01", 6379);
    }

3.操作Redis字符串类型

    /**
     * 对字符串的操作
     */
//    @Test
    public void str() {
        // 创建连接对象
        Jedis resource = pool.getResource();
        // 添加
        resource.set("str01", "张三");
        // 查询
        String str01 = resource.get("str01");
        System.out.println("查询str01的结果\t" + str01);
        //添加多个
        resource.mset("str02", "李四", "str03", "1");
        // 查询多个
        List<String> str02 = resource.mget("str02", "str03");
        System.out.println("查询str01 str02的结果\t" + str02);
        // 设置过期时间
        resource.setex("hello", 10, "hello");
        // key 存在是添加 成功返回1  失败返回 0
        System.out.println(resource.setnx("hello", "hello"));
        // 查询部分 startOffset开始的索引 endOffset 结束的索引  hell
        System.out.println("查询hello\t" + resource.getrange("hello", 0, 3));
        //查询长读 输入key
        System.out.println("查询hello的长度\t" + resource.strlen("hello"));
        // 追加
        resource.append("hello", "append");
        // 替换 key :操作的的key   offset:从索引几开始替换  value:替换的内容
        resource.setrange("hello", 0, "AAA");
        System.out.println("查询hello\t" + resource.get("hello"));
        // 累加1
        resource.incr("str03");
        System.out.println("str03加1的结果\t" + resource.get("str03"));
        // 设置累加N
        resource.incrBy("str03", 20);
        System.out.println("查询str03加20后的结果\t" + resource.get("str03"));
        // 减1
        resource.decr("str03");
        System.out.println("查询str03减1的结果\t" + resource.get("str03"));
        // 减N
        resource.decrBy("str03", 10);
        System.out.println("查询str03减10的结果\t" + resource.get("str03"));
    }

4.操作Redis Hash类型

    /**
     * 对 hash操作
     */
//    @Test
    public void hash() {
        // 获取连接对象
        Jedis resource = pool.getResource();
        // 添加数据
        resource.hset("hkey01", "name1", "李四");
        resource.hset("hkey02", "name2", "张三");
        //不存在是添加
        resource.hsetnx("hkey02", "name2", "赵刘");
        // 一次性设置多个值
        Map<String, String> hash = new HashMap<>();
        hash.put("name", "zhangsan");
        hash.put("age", "18");
        hash.put("sex", "1");
        resource.hmset("hkey03", hash);
        // 查询hkey02
        System.out.println("查询hkey02 name2\t" + resource.hget("hkey02", "name2"));
        // 查询是否存在 hkey02中的name2 存在返回true
        System.out.println("判断hkey02中的name2是否存在\t" + resource.hexists("hkey02", "name2"));
        // 查询 hkey03 下的所有值
        System.out.println("hkey03的所有的值\t" + resource.hgetAll("hkey03"));
        // 查询 hkey03的所有字段
        System.out.println("hkey03的字段\t" + resource.hkeys("hkey03"));
        // 查询hkey03的数量
        System.out.println("查询hkey03的数量\t" + resource.hlen("hkey03"));
        // 查询hkey03的多个值 name age
        System.out.println("查询hkey03的多个值\t" + resource.hmget("hkey03", "name", "age"));
        // 删除数据
        resource.hdel("hkey03", "sex");
        // 累加(1-N) hkey03中的age 10
        resource.hincrBy("hkey03", "age", 10);
        System.out.println("hkey03中的age结果\t" + resource.hget("hkey03", "age"));
    }

5.操作Redis list类型

    /**
     * 对list 操作
     */
//    @Test
    public void list() {
        // 获取连接对象
        Jedis resource = pool.getResource();
        // 可以添加一个或多个
        resource.lpush("listkey", "zhangsan", "lisi");
        // 插入头部
        resource.lpushx("listkey", "wangwu");
        //右边插入
        resource.rpush("listkey", "AA", "BB");
        // 插入尾部
        resource.rpushx("listkey", "CC");
        // 插入某个数据之前
        resource.linsert("listkey", BinaryClient.LIST_POSITION.BEFORE, "AA", "aa");
        // 插入某个数据之后
        resource.linsert("listkey", BinaryClient.LIST_POSITION.AFTER, "CC", "DD");
        // 查询listkeyd的所有数据 -1 默认查询所有数据
        System.out.println("查询listkey的所有数据\t" + resource.lrange("listkey", 0, -1));
        // 通过索引查询
        System.out.println("通过索引查询\t" + resource.lindex("listkey", 0));
        // 查询listkey的长度
        System.out.println("查询listkey的数据长度\t" + resource.llen("listkey"));
        // 数据修减 保留 0 到 3 的索引
        resource.ltrim("listkey", 0, 3);
        System.out.println("查询listkey修减后的数据\t" + resource.lrange("listkey", 0, -1));
        // 移除listkey的第一个数据
        resource.lpop("listkey");
        //移除listkey的最后一个数据
        resource.rpop("listkey");
        System.out.println("查询listke移除第一个和最后一个的数据\t" + resource.lrange("listkey", 0, -1));
        // 移除最后一给数据保存到另一个列表
        resource.rpoplpush("listkey", "listkey");
        System.out.println("查询listkey数据\t" + resource.lrange("listkey", 0, -1));
    }

6.操作Redis set类型

/**
     * 对set 操作
     */
//    @Test
    public void set() {
        // 获取连接对象
        Jedis resource = pool.getResource();
        // 向集合添加一个或多个
        resource.sadd("skey1", "zhnangsan", "lisi");
        resource.sadd("skey2", "wangwu", "lisi");
        // 查询结合的所有数据
        System.out.println("查询skey1的数据:" + resource.smembers("skey1"));
        // 查询数据总量
        System.out.println("查询skey1的数据总量:" + resource.scard("skey1"));
        // 查询差值
        System.out.println("查询skey1和skey2的差值" + resource.sdiff("skey1", "skey2"));
        // 查询差值写入到新的集合
        resource.sdiffstore("skey3", "skey1", "skey2");
        // 查询交集
        System.out.println(resource.sinter("skey1", "skey2"));
        // 查询交集写入到新的集合
        resource.sinterstore("skey4", "skey1", "skey2");
        // 查询某一个数据是否在集合中
        System.out.println("查询zhnangsan是否在skey1\t" + resource.sismember("skey1", "zhnangsan"));
        // 返回集合随机N个数据
        System.out.println("返回随机skey1\t" + resource.srandmember("skey1", 1));
        // 查询并集
        System.out.println("查询skey1与skey2的并集\t" + resource.sunion("skey1", "skey2"));
        // 查询并集写入到一个新的结合
        resource.sunionstore("skey5", "skey1", "skey2");
        //将数据在一个集合移动到另一个集合
        resource.smove("skey1", "skey2", "zhangsan");
        // 移除结果中的某个数据
        resource.srem("skey2", "skey2", "zhangsan");
    }

7.关闭资源

    /**
     * 关闭资源
     */
    @AfterTest
    public void close() {
        pool.close();
    }

在这里对Redis JavaApi的操作就结束了,喜欢的话点赞,加个关注

1

发布了88 篇原创文章 · 获赞 99 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_43791724/article/details/104867705