赞
踩
目录
./src/redis-server redis.conf
检查
lsof -i:6379
- package com.zking.ssm.redis;
-
- import redis.clients.jedis.Jedis;
-
- /**
- * @author 白未
- * @site 3185579318
- * @company xxx公司
- * @create 2022-10-26 11:47
- */
- public class Demo1 {
- public static void main(String[] args){
- Jedis jedis = new Jedis("192.168.118.129", 6379);
- jedis.auth("123456");
- System.out.println(jedis.ping());
- }
- }
Redis字符串(String)
- public static void main(String[] args){
- Jedis jedis = new Jedis("192.168.118.129", 6379);
- jedis.auth("123456");
- System.out.println(jedis.ping());
- jedis.select(7);
-
- jedis.set("uname","颠颠");
- jedis.set("sex","女");
- jedis.set("age","1");
- }
System.out.println(jedis.get("uname"));
- Long age = jedis.del("age");//删除年龄
- System.out.println(age);
jedis.set("sex","秘密");
jedis.setex("hobby",50,"干饭");
System.out.println(jedis.ttl("hobby"));
Redis哈希(Hash)
- public static void main(String[] args){
- Jedis jedis = new Jedis("192.168.118.129", 6379);
- jedis.auth("123456");
- jedis.select(7);
-
- // 操作哈希 , 其实就是存储对象
- jedis.hset("stu1","sname","颠颠");
- jedis.hset("stu1","age","18");
- jedis.hset("stu1","sex","女");
-
- }
- // 获取值
- Map<String, String> stu = jedis.hgetAll("stu1");
- System.out.println(stu);
- // 删除对象 中 的某一属性
- jedis.hdel("stu1","age");
- // 判断对象中的某一项属性是否存在
- System.out.println(jedis.hexists("stu1", "sex"));
- // 获取对象中的属性长度
- System.out.println(jedis.hlen("stu1"));
Redis列表(List)
- //存储值
- jedis.lpush("hobby","篮球","足球","桌球","羽毛球","排球");
- // 取值 lindex
- System.out.println(jedis.lindex("hobby", 1));
- // 获取列表的长度
- System.out.println(jedis.llen("hobby"));
- // 查看指定范围内的元素
- List<String> hobby = jedis.lrange("hobby", 0, 3);
- for (String s : hobby){
- System.out.println(s);
- }
Redis集合(Set)
- // 存储
- jedis.sadd("toys","娃娃机","滑滑梯","芭比娃娃");
- // 存在
- System.out.println(jedis.exists("toys"));
- // 获取集合中的元素数量
- System.out.println(jedis.scard("toys"));
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。