创建jedis对象工具类
/**
* 工具类:获取jedis对象
*/
public class JedisUtil {
//创建静态对象
private static JedisPool pool;
//构造器私有
private JedisUtil() {}
//静态封装,加载类 自动执行静态代码块中方法实现,
static {
//通过加载配置信息properties 读取ip port 连接对象基本信息
//classloader 反射读取 Properties类
ResourceBundle bundle = ResourceBundle.getBundle("redis"); //资源对象 读取配置文件redis.properties
String host = bundle.getString("redis.host"); //ip
String port = bundle.getString("redis.port"); //端口
String maxTotal = bundle.getString("redis.maxTotal"); //最大连接数
String maxIdle = bundle.getString("redis.maxIdle"); //最大等待数
String maxWait = bundle.getString("redis.maxWait"); //最大等待时间
JedisPoolConfig config = new JedisPoolConfig(); //jedis线程池配置对象
config.setMaxTotal(Integer.parseInt(maxTotal));
config.setMaxTotal(Integer.parseInt(maxIdle));
config.setMaxTotal(Integer.parseInt(maxWait));
pool = new JedisPool(config, host, Integer.parseInt(port)); //初始化
}
//创建静态方法获取对象
public static Jedis getJedis() {
return pool.getResource();
}
}
jdk序列化和反序列化
/**
* jdk序列化:(io流)将任意对象转换为字节数组
*/
public class SerializableUtil {
//序列化:对象转化为数组对象
public static byte[] Object2ByteArray(Object object) {
byte[] data = null;
try {
//1.将对象信息最终保存在 字节输出流中
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//2.利用对象流实现将对象写入到 字节数组输出流中
ObjectOutputStream oos = new ObjectOutputStream(bos);
//3.写出对象信息
oos.writeObject(object);
//4.获取对象写出字节信息
data = bos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
//反序列化:数组对象转化为任意对象
public static Object byteArray2Object(byte[] data) {
try {
//1.读取数组信息
ByteArrayInputStream bis = new ByteArrayInputStream(data);
//2.获取对象
ObjectInputStream ois = new ObjectInputStream(bis);
//3.读对象
Object obj = ois.readObject();
return obj;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
实体类
/**
* 实现序列化接口
*/
public class Student implements Serializable {
private int id;
private String name;
public Student() {
}
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
测试类
/**
* 如何将一个Student对象保存在redis
* set(byte[] key,byte[] value)
* get(byte[] key)
* 序列化:将对象保存redis
* 反序列化:从redis中读对象信息
*/
public class JedisOpsObjectTest {
@Test
public void method() {
//1.创建一个student对象
Student s = new Student(1, "tom");
Student s2 = new Student(2, "jack");
//2.使用通过连接池中的jedis对象保存student信息
Jedis jedis = JedisUtil.getJedis();
System.out.println("创建jedis对象:" + jedis);
jedis.flushDB();
//3.调用set方法实现保存数据;
//解决问题:IO 将任意的对象转换为byte[] 数组
byte[] key = SerializableUtil.Object2ByteArray("s"); //对象转数组
byte[] value = SerializableUtil.Object2ByteArray(s);
byte[] value2 = SerializableUtil.Object2ByteArray(s2);
//set(byte[] key, byte[] value)
jedis.set(key, value); //jdk序列化tom,key值:字节
jedis.set("s".getBytes(), value2); //字符串序列化jack,key值:s
//4.关闭连接
jedis.close();
}
@Test
public void method2() {
//1.获取连接对象 jedis
Jedis jedis = JedisUtil.getJedis();
//2.根据key值获取value对象
//方式1:字符串序列化jack
byte[] bytes = jedis.get("s".getBytes());
System.out.println(SerializableUtil.byteArray2Object(bytes)); //Student{id=2, name='jack'}
//方式2:JDK序列化tom:io流操作
byte[] key = SerializableUtil.Object2ByteArray("s");
byte[] bytes2 = jedis.get(key);
System.out.println(SerializableUtil.byteArray2Object(bytes2)); //Student{id=1, name='tom'}
}
}