Java 串行化技术可以使你将一个对象的状态写入一个Byte 流里,并且可以从其它地方把该Byte 流里的数据读出来,重新构造一个相同的对象。这种机制允许你将对象通过网络进行传播,并可以随时把对象持久化到数据库、文件等系统里。Java的串行化机制是RMI、EJB等技术的技术基础。用途:利用对象的串行化实现保存应用程序的当前工作状态,下次再启动的时候将自动地恢复到上次执行的状态。序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化。可以对流化后的对象进行读写操作,也可将流化后的对象传输于网络之间。序列化是为了解决在对对象流进行读写操作时所引发的问题。
序列化的实现:将需要被序列化的类实现Serializable接口,然后使用一个输出流(如:FileOutputStream)来构造一个ObjectOutputStream(对象流)对象,接着,使用ObjectOutputStream对象的writeObject(Object obj)方法就可以将参数为obj的对象写出(即保存其状态),要恢复的话则用输入流。
使用hibernate跟数据库打交道
hibernate配置文件如下
com.mysql.jdbc.Driverjdbc:mysql://localhost:3306/testtrueutf-8rootyou_passwordorg.hibernate.connection.C3P0ConnectionProvider20118001005002trueselect 1 1800025000trueorg.hibernate.dialect.MySQLInnoDBDialectupdatetruetrue
hibernate实体类
package test.java;
import java.sql.Blob;
public class Test {
private int id;
private Blob content;
public Blob getContent() {
return content;
}
public void setContent(Blob content) {
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
待序列化的类
package test.java;
import java.io.Serializable;
public class User implements Serializable{
private int id;
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name;
private int age;
}
测试类
package test.java;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Blob;
import java.sql.SQLException;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class MySerialization {
public static SessionFactory sessionFactory=null;
public static SessionFactory getSessionFactory(){
if(sessionFactory==null)
sessionFactory=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
return sessionFactory;
}
public static Blob getBlob(Object object) throws IOException{
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(object);
ByteArrayInputStream bis=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
return Hibernate.createBlob(bis);
}
public static void writeObjectToDB(Object object) throws IOException{
Test test=new Test();
test.setContent(getBlob(object));
Session session=getSessionFactory().openSession();
Transaction tx =session.beginTransaction();
session.save(test);
tx.commit();
session.close();
}
public static User readObjectFromDB(int id) throws IOException, SQLException, ClassNotFoundException{
Session session=getSessionFactory().openSession();
Test test=(Test) session.get(Test.class,id);
ObjectInputStream objectInputStream=new ObjectInputStream(test.getContent().getBinaryStream());
return (User)objectInputStream.readObject();
}
public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
User user=new User();
user.setId(1);
user.setAge(22);
user.setName("zhangsan");
writeObjectToDB(user);
User user1=readObjectFromDB(2);
System.out.println(user1.getId());
System.out.println(user1.getName());
System.out.println(user1.getAge());
}
}