Redis does not support the direct storage of Java objects to a database, so you need to serialize Java objects to a byte array, and then deposit the byte array into the Redis, and then take the byte array out of the Redis database when you need the data. Then deserialization converts its own array to object use (JDK serialization performance is worse than Google's Protobuf serialization performance, and the serialized byte length is also a bit longer, so it is recommended to use protobuf,protobuf how to serialize, see my other post)
entity Classes
User.java
[Java] View Plain copy import java.io.serializable; public class user implements serializable{ private static final long serialversionuid = 2724888087391664167L; private string id; private string username; private string password; public user () { } Public user (String id, string username, string password) { this.id = id; this.username = username; This.password = password; } Public string getid () { return id; } Public void setid (string 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; } public static long Getserialversionuid () { return serialversionuid; } @Override public string tostring () { return "user [id=" + id + ", username=" + username + ", password=" + password + "]"; } } serialization Deserialization tool class
Serializeutils.java
[Java]View plain copy import Java.io.ByteArrayInputStream; Import Java.io.ByteArrayOutputStream; Import java.io.IOException; Import Java.io.ObjectInputStream; Import Java.io.ObjectOutputStream; public class Serializeutils {