Jedis是Java语言开发的Redis客户端工具包。
Jedis只是对Redis命令的封装,掌握Redis命令便可轻易上手。
打开Xshell连接虚拟机,切换目录到redis安装目录,执行vim redis.conf打开redis配置文件,找到如图位置

protected-mode - 是否开启保护模式,开启保护模式后只有指定的IP可以访问Redis,在开发需要,先设置为no,然后往上寻找到如图位置
把bind的值改为0.0.0.0保存,让所有主机的IP都可以访问进来(只限开发环境这么改,生产环境勿用)
然后执行./src/redis-server redis.conf启动redis。
然后防火墙放开6379端口,执行firewall-cmd --zone=public --add-port=6379/tcp --permanent,然后重载firewall-cmd --reload
启动IDEA,新建工程,


在pom.xml中添加jedis依赖
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
com.ql.jedis包下创建JedisTestor类
package com.ql.jedis;
import redis.clients.jedis.Jedis;
import java.util.List;
public class JedisTestor {
public static void main(String[] args) {
Jedis jedis = null;
try {
jedis = new Jedis("192.168.52.128", 6379);
jedis.auth("12345");
jedis.select(2);
System.out.println("Redis连接成功");
//字符串
jedis.set("sn", "789789789");
String sn = jedis.get("sn");
System.out.println(sn);
jedis.mset(new String[]{"title", "奶粉","num","20"});
List<String> mget = jedis.mget(new String[]{"sn", "title", "num"});
System.out.println(mget);
Long num = jedis.incr("num");
System.out.println(num);
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.close();
}
}
}
/*
Redis连接成功
789789789
[789789789, 奶粉, 20]
21
*/
//Hash
jedis.hset("student:1001","name","张晓明");
String name = jedis.hget("student:1001", "name");
System.out.println(name);
Map<String,String> studentMap = new HashMap<>();
studentMap.put("name","李兰");
studentMap.put("age","18");
studentMap.put("id","1002");
jedis.hmset("student:1002", studentMap);
Map<String, String> stringStringMap = jedis.hgetAll("student:1002");
System.out.println(stringStringMap);
/*
张晓明
{name=李兰, age=18, id=1002}
*/
//List
jedis.del("list");
jedis.rpush("list", new String[]{"d","e","f"});
jedis.lpush("list",new String[]{"c","b","a"});
List<String> list = jedis.lrange("list", 0, -1);
System.out.println(list);
jedis.rpop("list");
jedis.lpop("list");
List<String> list1 = jedis.lrange("list", 0, -1);
System.out.println(list1);
/*
[a, b, c, d, e, f]
[b, c, d, e]
*/
首先com.ql.jedis包下创建Goods 实体类。
package com.ql.jedis;
public class Goods {
private Integer goodsId;
private String goodsName;
private String description;
private Float price;
public Integer getGoodsId() {
return goodsId;
}
public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
}
然后在pom.xml文件中添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
创建CacheSample.java文件
package com.ql.jedis;
import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CacheSample {
public CacheSample(){
Jedis jedis = new Jedis("192.168.52.128");
try {
List<Goods> goodsList = new ArrayList<>();
goodsList.add(new Goods(1001, "苹果","",3f));
goodsList.add(new Goods(1002, "橙子","",4f));
goodsList.add(new Goods(1003, "香蕉","",5f));
jedis.auth("12345");
jedis.select(3);
for (Goods goods: goodsList){
String json = JSON.toJSONString(goods);
System.out.println(json);
String key = "goods:"+goods.getGoodsId();
jedis.set(key,json);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.close();
}
}
public static void main(String[] args) {
new CacheSample();
System.out.println("请输入要查询的商品编号:");
String goodsId = new Scanner(System.in).next();
Jedis jedis = new Jedis("192.168.52.128");
try {
jedis.auth("12345");
jedis.select(3);
String key = "goods:"+goodsId;
if(jedis.exists(key)){
String json = jedis.get(key);
System.out.println(json);
Goods goods = JSON.parseObject(json, Goods.class);
System.out.println(goods.getGoodsName());
System.out.println(goods.getPrice());
}else{
System.out.println("您输入的商品编号不存在,请重新输入!");
}
}catch (Exception e){
e.printStackTrace();
}
}
}
/*
{"description":"","goodsId":1001,"goodsName":"苹果","price":3.0}
{"description":"","goodsId":1002,"goodsName":"橙子","price":4.0}
{"description":"","goodsId":1003,"goodsName":"香蕉","price":5.0}
请输入要查询的商品编号:
1002
{"description":"","goodsId":1002,"goodsName":"橙子","price":4.0}
橙子
4.0
*/