0


java操作redis

java操作Redis

使用Java来操作Redis,Jedis是Redis官方推荐使用的Java连接redis的客户端。

1 环境准备

1.1. 引入依赖

<!--引入jedis连接依赖--><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.2.0</version></dependency><!--fastjson--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.70</version></dependency>

1.2.创建jedis对象

publicstaticvoidmain(String[] args){//1.创建jedis对象Jedis jedis =newJedis("192.168.75.101",6379);//1.redis服务必须关闭防火墙  2.redis服务必须开启远程连接
   jedis.select(0);//选择操作的库默认0号库//2.执行相关操作//3.释放资源
   jedis.close();}
  1. 开放端口6379firewall-cmd --zone=public --add-port=6379/tcp --permanet1重启防火墙服务systemctl restart firewalld.service1. 阿里云服务器控制台配置安全组2. 重启redis-server[root@AlibabaECS bin]# redis-server myconfig/redis.conf
  • 操作命令TestPing.javapublicclassTestPing{publicstaticvoidmain(String[] args){Jedis jedis =newJedis("192.168.xx.xxx",6379);String response = jedis.ping();System.out.println(response);// PONG}}
  • 断开连接

1.3.事务

publicclassTestTX{publicstaticvoidmain(String[] args){Jedis jedis =newJedis("198.168.xxx.xx",6379);JSONObject jsonObject =newJSONObject();
        jsonObject.put("hello","world");
        jsonObject.put("name","kuangshen");// 开启事务Transaction multi = jedis.multi();String result = jsonObject.toJSONString();// jedis.watch(result)try{
            multi.set("user1", result);
            multi.set("user2", result);// 执行事务
            multi.exec();}catch(Exception e){// 放弃事务
            multi.discard();}finally{// 关闭连接System.out.println(jedis.get("user1"));System.out.println(jedis.get("user2"));
            jedis.close();}}}

2 操作key相关API

privateJedis jedis;@Beforepublicvoidbefore(){this.jedis =newJedis("192.168.XXX.XXX",7000);}@Afterpublicvoidafter(){
        jedis.close();}//测试key相关@TestpublicvoidtestKeys(){//删除一个key
        jedis.del("name");//删除多个key
        jedis.del("name","age");//判断一个key是否存在exitsBoolean name = jedis.exists("name");System.out.println(name);//设置一个key超时时间 expire pexpireLong age = jedis.expire("age",100);System.out.println(age);//获取一个key超时时间 ttlLong age1 = jedis.ttl("newage");System.out.println(age1);//随机获取一个keyString s = jedis.randomKey();//修改key名称
        jedis.rename("age","newage");//查看可以对应值的类型String name1 = jedis.type("name");System.out.println(name1);String maps = jedis.type("maps");System.out.println(maps);}

3操作String相关API

//测试 String 相关@TestpublicvoidtestString(){//set
        jedis.set("name","秦羽");//getString s = jedis.get("name");System.out.println(s);//mset
        jedis.mset("content","tj","address","xqq");//mgetList<String> mget = jedis.mget("name","content","address");
        mget.forEach(v->System.out.println("v = "+ v));//getsetString set = jedis.getSet("name","小明");System.out.println(set);//............}

4操作List相关API

//测试 List 相关@TestpublicvoidtestList(){//lpush
        jedis.lpush("names1","张三","王五","赵柳","win7");//rpush
        jedis.rpush("names1","xiaomingming");//lrangeList<String> names1 = jedis.lrange("names1",0,-1);
        names1.forEach(name->System.out.println("name = "+ name));//lpop rpopString names11 = jedis.lpop("names1");System.out.println(names11);//llen
        jedis.linsert("lists",BinaryClient.LIST_POSITION.BEFORE,"xiaohei","xiaobai");//........}

5操作Set的相关API

//测试SET相关@TestpublicvoidtestSet(){//sadd
  jedis.sadd("names","zhangsan","lisi");//smembers
  jedis.smembers("names");//sismember
  jedis.sismember("names","qinyu");//...}

6 操作ZSet相关API

//测试 ZSET 相关@TestpublicvoidtestZset(){//zadd
  jedis.zadd("names",10,"张三");//zrange
  jedis.zrange("names",0,-1);//zcard
  jedis.zcard("names");//zrangeByScore
  jedis.zrangeByScore("names","0","100",0,5);//..}

7 操作Hash相关API

//测试 HASH 相关@TestpublicvoidtestHash(){//hset
  jedis.hset("maps","name","zhangsan");//hget
  jedis.hget("maps","name");//hgetall
  jedis.hgetAll("mps");//hkeys
  jedis.hkeys("maps");//hvals
  jedis.hvals("maps");//....}
标签: redis java 数据库

本文转载自: https://blog.csdn.net/qq_43585922/article/details/128266627
版权归原作者 秦 羽 所有, 如有侵权,请联系我们删除。

“java操作redis”的评论:

还没有评论