将java对象存入redis 序列化、反序列化
首先时我们需要一个序列化工具类package com.aw.redisdemo.util;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;/*** 序列化工具*/pub
·
首先时我们需要一个序列化工具类
package com.aw.redisdemo.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* 序列化工具
*/
public class SerializeUtil {
/**
* 将java对象转换为byte数组 序列化过程
*/
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
baos=new ByteArrayOutputStream();
oos=new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 将byte数组转化为java对象
* @param bytes
* @return
*/
public static Object unserialize(byte[] bytes){
if (bytes==null) return null;
ByteArrayInputStream bais=null;
try{
bais =new ByteArrayInputStream(bytes);
ObjectInputStream objectInputStream = new ObjectInputStream(bais);
return objectInputStream.readObject();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
实体类
package com.aw.redisdemo.pojo;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID= 9148937431079191022L;
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
package com.aw.redisdemo;
import com.aw.redisdemo.pojo.User;
import com.aw.redisdemo.util.SerializeUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.params.SetParams;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@SpringBootTest
@RunWith(SpringRunner.class)
class RedisDemoApplicationTests {
@Autowired
private JedisPool jedisPool;
private Jedis jedis=null;
//初始化jedis实例对象
@BeforeEach
public void initConnt(){
jedis = jedisPool.getResource();
}
@Test
public void testByte(){
User user = new User();
user.setId(2);
user.setUsername("阿威");
user.setPassword("123445");
//序列化为byte数组
byte[] userKey = SerializeUtil.serialize("user:"+user.getId());
byte[] userValue = SerializeUtil.serialize(user);
//存入redis
jedis.set(userKey,userValue);
//取出数据
byte[] bytes = jedis.get(userKey);
User unserialize = (User) SerializeUtil.unserialize(bytes);
System.out.println(unserialize);
}
//释放资源
@AfterEach
public void closeConnt(){
if (jedis!=null){
jedis.close();
}
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)