前提,对象必须要序列化,即对象的实体类要实现序列化接口。
代码如下所示:
用户实体类:
=================UserEntity.java===============
package com.wx.entitys;
import java.io.Serializable;
public class UserEntity implements Serializable{
private static final long serialVersionUID = 3660009371337848373L;
private Integer userId;
private String userName;
private String passWord;
private String email;
public UserEntity() {
}
public UserEntity(Integer userId, String userName,
String passWord, String email) {
super();
this.userId = userId;
this.userName = userName;
this.passWord = passWord;
this.email = email;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Redis工具类
=================RedisUtil.java===============
package com.wx.utils;
import redis.clients.jedis.Jedis;
public class RedisUtil {
private static String ip="169.254.130.122";
private static int port=6379;
private static String passWord="test123";
//把redis连接对象放到本地线程中
private static ThreadLocal<Jedis> local=new ThreadLocal<Jedis>();
private RedisUtil(){}
//建立连接
public static Jedis getConn(){
//Redis对象
Jedis jedis =local.get();
try {
if(jedis==null){
//根据ip和端口号建立连接
jedis=new Jedis(ip,port);
//设置密码
jedis.auth(passWord);
local.set(jedis);
}
} catch (Exception e) {
e.printStackTrace();
}
return jedis;
}
//关闭连接
public static void closeConn(){
//从本地线程中获取
Jedis jedis =local.get();
if(jedis!=null){
jedis.close();
}
local.set(null);
}
}
对象存取工具类
=================RedisObjUtil.java===============
package com.wx.utils;
import java.io.*;
import redis.clients.jedis.Jedis;
public class RedisObjUtil {
// 存对象
public static void setObject(Jedis jedis, String key, Object obj) throws Exception {
ObjectOutputStream oos = null; //对象输出流
ByteArrayOutputStream bos = null; //内存缓冲流
bos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
byte[] byt = bos.toByteArray();
jedis.set(key.getBytes(), byt);
bos.close();
oos.close();
}
// 取对象
public static Object getObject(Jedis jedis, String key) throws Exception {
byte[] byt = jedis.get(key.getBytes());
ObjectInputStream ois = null; //对象输入流
ByteArrayInputStream bis = null; //内存缓冲流
Object obj = null;
bis = new ByteArrayInputStream(byt);
ois = new ObjectInputStream(bis);
obj = ois.readObject();
bis.close();
ois.close();
return obj;
}
}
测试类
=================TestObjectOpt.java===============
package com.wx.test;
import com.wx.entitys.UserEntity;
import com.wx.utils.RedisObjUtil;
import com.wx.utils.RedisUtil;
import redis.clients.jedis.Jedis;
public class TestObjectOpt {
public static void main(String[] args) {
Jedis jedis=RedisUtil.getConn();
try {
UserEntity user1=new UserEntity(110, "麻子", "123", "mazi@qq.com");
RedisObjUtil.setObject(jedis, "user1", user1);
UserEntity theUser=(UserEntity)RedisObjUtil.getObject(jedis, "user1");
System.out.println(theUser.getUserName()+","+theUser.getEmail());
} catch (Exception e) {
e.printStackTrace();
}finally{
RedisUtil.closeConn();
}
}
}
执行完毕后,发现对象已经正确的存到了服务器上