不会吧不会吧,不会现在还有人不会整合redis和springboot吧!!!

Posted 爱分享的板栗老哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了不会吧不会吧,不会现在还有人不会整合redis和springboot吧!!!相关的知识,希望对你有一定的参考价值。

Redis——通过jedis操作和整合springboot

Jedis项目搭建

Jedis是Redis官方推荐的Java连接开发工具。要在Java开发中使用好Redis中间件,必须对Jedis熟悉才能
写成漂亮的代码

1、导入相关依赖

    <dependencies>
        <!--导入jedis包-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.6.0</version>
        </dependency>
         <!--加群1025684353一起吹水聊天-->
        <!--导入fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.70</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
    </dependencies>

2、application.properties配置redis配置

领取资料

# 应用名称
spring.application.name=redis_springboot
#Redis服务器地址
spring.redis.host=192.168.137.4
#Redis服务器连接端口
spring.redis.port=6379
#Redis数据库索引(默认为0)
# 加群1025684353一起吹水聊天-->
spring.redis.database= 0
#连接超时时间(毫秒)
spring.redis.timeout=1800000
#连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=20
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=5
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0

3、编写测试类测试是否ping成功

注意:必须要在配置文件取消本地和安全模式

package com.study;

import redis.clients.jedis.Jedis;

public class TestPing {
    public static void main(String[] args) {
    	// 加群1025684353一起吹水聊天-->
        //1.new Jedis对象
        Jedis jedis = new Jedis("192.168.137.4",6379);
        System.out.println(jedis.ping());
    }
}

Jedis常用API

领取资料

一、key操作

package com.study;

import redis.clients.jedis.Jedis;

public class TestKey {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.137.4",6379);
		 //加群1025684353一起吹水聊天-->
        System.out.println("清空数据:"+jedis.flushDB());
        System.out.println("判断某个键是否存在:"+jedis.exists("username"));
        System.out.println("新增<username,zhangsan>的键值对:"+jedis.set("username","zhangsan"));
        System.out.println("新增<password,password>的键值对:"+jedis.set("password","password"));
        System.out.println("查看所有的key:"+jedis.keys("*"));
        System.out.println("删除key键password:"+jedis.del("password"));
        System.out.println("判断key键password是否存在:"+jedis.exists("password"));
        System.out.println("查看username所存储的值的类型:"+jedis.type("username"));
        System.out.println("随机返回key空间的一个:"+jedis.randomKey());
        System.out.println("重命名key:"+jedis.rename("username","name"));
        System.out.println("取出改后的name:"+jedis.get("name"));
        System.out.println("按索引切换数据库:"+jedis.select(0));
        System.out.println("清空数据库:"+jedis.flushDB());
        System.out.println("返回当前数据库中key的数目:"+jedis.dbSize());
        System.out.println("清空所有数据库:"+jedis.flushAll());
    }
}

二、string操作

package com.study;

import redis.clients.jedis.Jedis;

import java.util.concurrent.TimeUnit;

public class TestString {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.137.4", 6379);
         //加群1025684353一起吹水聊天-->

        jedis.flushDB();
        System.out.println("========添加数据=======");
        System.out.println(jedis.set("key1","value1"));
        System.out.println(jedis.set("key2","value2"));
        System.out.println(jedis.set("key3","value3"));
        System.out.println("删除键key2:"+jedis.del("key2"));
        System.out.println("获取键key2:"+jedis.get("key2"));
        System.out.println("修改key1:"+jedis.set("key1","value1Changed"));
        System.out.println("获取key1的值:"+jedis.get("key1"));
        System.out.println("添加多个键值对:"+jedis.mset("key01","value01","key02","value02","key03","value03"));
        System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03"));
        System.out.println("删除多个键值对:"+jedis.del("key01","key02"));
        //加群1025684353一起吹水聊天-->

        jedis.flushDB();
        System.out.println("========新增键值对防止覆盖原先值========");
        System.out.println(jedis.setnx("key1","value1"));
        System.out.println(jedis.setnx("key2","value2"));
        System.out.println(jedis.setnx("key2","value2-new"));
        System.out.println("获取多个键值对的值"+jedis.mget("key1","key2"));

        System.out.println("========新增键值对并设置有效时间==========");
        System.out.println(jedis.setex("key3",2,"value3")); //有效时间为2秒
        System.out.println(jedis.get("key3"));
        try {
            TimeUnit.SECONDS.sleep(3);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println(jedis.get("key3"));

        System.out.println("========获取原值,更新为新值=======");
        System.out.println(jedis.getSet("key2","key2GetSet"));
        System.out.println(jedis.get("key2"));

        System.out.println("获得key2的值的字串:"+jedis.getrange("key2",2,4));
    }
}

三、set操作

领取资料

package com.study;

import redis.clients.jedis.Jedis;

public class TestSet {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.137.4",6379);
        jedis.flushDB();
         //加群1025684353一起吹水聊天-->
        System.out.println("============向集合中添加元素(不重复)============");
        System.out.println(jedis.sadd("eleSet", "e1","e2","e4","e3","e0","e8","e7","e5"));
        System.out.println(jedis.sadd("eleSet", "e6"));
        System.out.println(jedis.sadd("eleSet", "e6"));
        System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet"));
        System.out.println("删除一个元素e0:"+jedis.srem("eleSet", "e0"));
        System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet"));
        System.out.println("删除两个元素e7和e6:"+jedis.srem("eleSet", "e7","e6"));
        System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet"));
        System.out.println("随机的移除集合中的一个元素:"+jedis.spop("eleSet"));
        System.out.println("随机的移除集合中的一个元素:"+jedis.spop("eleSet"));
        System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet"));
        System.out.println("eleSet中包含元素的个数:"+jedis.scard("eleSet"));
        System.out.println("e3是否在eleSet中:"+jedis.sismember("eleSet", "e3"));
        System.out.println("e1是否在eleSet中:"+jedis.sismember("eleSet", "e1"));
        System.out.println("e1是否在eleSet中:"+jedis.sismember("eleSet", "e5"));
        System.out.println("=================================");
        System.out.println(jedis.sadd("eleSet1", "e1","e2","e4","e3","e0","e8","e7","e5"));
        System.out.println(jedis.sadd("eleSet2", "e1","e2","e4","e3","e0","e8"));
        System.out.println("将eleSet1中删除e1并存入eleSet3 中:"+jedis.smove("eleSet1", "eleSet3", "e1"));//移到集合元素
        System.out.println("将eleSet1中删除e2并存入eleSet3 中:"+jedis.smove("eleSet1", "eleSet3", "e2"));
        System.out.println("eleSet1中的元素:"+jedis.smembers("eleSet1"));
        System.out.println("eleSet3中的元素:"+jedis.smembers("eleSet3"));
        System.out.println("============集合运算=================");
        System.out.println("eleSet1中的元素:"+jedis.smembers("eleSet1"));
        System.out.println("eleSet2中的元素:"+jedis.smembers("eleSet2"));
        System.out.println("eleSet1和eleSet2的交 集:"+jedis.sinter("eleSet1","eleSet2"));
        System.out.println("eleSet1和eleSet2的并 集:"+jedis.sunion("eleSet1","eleSet2"));
        System.out.println("eleSet1和eleSet2的差 集:"+jedis.sdiff("eleSet1","eleSet2"));//eleSet1中有,eleSet2中没有
        jedis.sinterstore("eleSet4","eleSet1","eleSet2");//求交集并将交集保存到dstkey的集合
        System.out.println("eleSet4中的元素:"+jedis.smembers("eleSet4"));
    }
}

四、List操作

package com.study;

import redis.clients.jedis.Jedis;

public class TestList {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.137.4", 6379);
        jedis.flushDB();
         //加群1025684353一起吹水聊天-->
        System.out.println("==========&#

以上是关于不会吧不会吧,不会现在还有人不会整合redis和springboot吧!!!的主要内容,如果未能解决你的问题,请参考以下文章

不会吧不会吧,难道还有人不了解MySQL索引底层原理?

程序员必备技能-git 不会到还有人不会用吧,不会吧不会吧?

不会有人看完这篇文章还不会C++的命名空间吧?不会吧?

不会有人看完这篇文章还不会C++的命名空间吧?不会吧?

都2021年了不会还有人不会 用SQL吧

不会真的有人 GitHub 还可以这样吧!