第一步:先创建redisUtil
package com.runtai.redis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisUtil {
private static String ip="localhost";
private static int port=6379;
private static int timeout=10000;
private static JedisPool pool=null;
static{
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxTotal(1024);//最大连接数
config.setMaxIdle(200);//最大空闲实例数
config.setMaxWaitMillis(10000);//等连接池给连接的最大时间,毫秒
config.setTestOnBorrow(true);//borrow一个实例的时候,是否提前vaildate操作
pool=new JedisPool(config,ip,port,timeout);
}
//得到redis连接
public static Jedis getJedis(){
if(pool!=null){
return pool.getResource();
}else{
return null;
}
}
//关闭redis连接
public static void close(final Jedis redis){
if(redis != null){
redis.close();
}
}
}
第二步:创建序列化工具SerializeUtil
package com.runtai.redis;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializeUtil {
/**
*
* 序列化
*/
public static byte[] serialize(Object obj) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
// 序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
byte[] byteArray = baos.toByteArray();
return byteArray;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
*
* 反序列化
*
* @param bytes
* @return
*/
public static Object unSerialize(byte[] bytes) {
ByteArrayInputStream bais = null;
try {
// 反序列化为对象
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
第三步:创建user实体类对象
package com.runtai.entity;
import java.io.Serializable;
//实现redis得继承Serializable接口进行序列化
public class User implements Serializable{
//定义一个序列号
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String password;
private String headPhoto; //头像图片
private String src; //头像图片
private String kind;
private String createTime;
private Integer status;//1正常2删除
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getHeadPhoto() {
return headPhoto;
}
public void setHeadPhoto(String headPhoto) {
this.headPhoto = headPhoto;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
第四步:开始利用redis存取对象
@Test
public void textRrdis() {
Jedis redis=RedisUtil.getJedis();
//设置user对象
User user =new User();
user.setId(1);
user.setName("小明");
redis.del("luoxu");
//把user对象序列化后存储进redis
redis.set("user".getBytes(),SerializeUtil.serialize(user));
//从redis取出user后,把user对象反序列化
User user2=(User) SerializeUtil.unSerialize(redis.get("user".getBytes()));
System.out.println(JSON.toJSON(user2));
}