java序列化和反序列化对象到mysql 的实现
How to save a complex java object in a MySQL table
If you want to save complex java objects to MySQL you can serialize and save them as BLOB in a MySQL table.
For example you have an object “complexObject” from class “ComplexObject” and you want to save it in database.
The ComplexObject class must implements Serializable interface and you can serialize the objects like this:
ByteArrayOutputStream baos;
ObjectOutputStream out;
baos = new ByteArrayOutputStream();
try {
out = new ObjectOutputStream(baos);
out.writeObject(complexObject);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
byte[] byteObject = baos.toByteArray();
to deserialize the object :
ByteArrayInputStream bais;
ObjectInputStream in;
try {
bais = new ByteArrayInputStream(byteObject);
in = new ObjectInputStream(bais);
complexObject = (ComplexObject) in.readObject();
in.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
The MySQL table looks like this:
CREATE TABLE myTable(
…
complexObject BLOB,…
);
If you use Hibernate and Annotation you declare the complexObject transient, and a byte[] byteObject that will be persisted:
@Entity
@Table(name = “myTable”)
SomeClass{
private byte[] byteObject;
private ComplexObject complexObject;
…
@Transient
public ComplexObject getComplexObject() {
return complexObject;
}
public void setComplexObject(ComplexObject complexObject) {
this.complexObject = complexObject;
ByteArrayOutputStream baos;
ObjectOutputStream out;
baos = new ByteArrayOutputStream();
try {
out = new ObjectOutputStream(baos);
out.writeObject(complexObject);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
this.byteObject = baos.toByteArray();
}
@Column(columnDefinition = “blob”)
public byte[] getByteObject() {
return byteObject;
}
public void setByteObject(byte[] byteObject) {
ByteArrayInputStream bais;
ObjectInputStream in;
try {
bais = new ByteArrayInputStream(byteObject);
in = new ObjectInputStream(bais);
complexObject = (ComplexObject) in.readObject();
in.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
this.byteObject= byteObject;
}
}
Attention the table column must be BLOB, not varchar.
Because the ComplexObject is transient it will not be save in database, but the byteObject will be save.
-------------------------------------------------------------
毕业后头五年决定你的一生 10类最急需IT人才:Java开发者居首
海量Android教程、开发资料和源码 给将成为“Android高手”的10个建议
成为Java高手的25个学习目标--非常经典 Android 4.1果冻豆新特性详解
Java侵权诉讼Google获胜,Android厚积薄发 面试必备:Android笔试总结
Android高手必须掌握的28大内容和10个建议 Android平台研发人才缺口30万
Android开发环境安装和配置步骤详细图解 2012国内移动App开发者大调查结果
Windows 7下搭建android开发环境步骤图解 Android 4.0的30个突出的新特性
Android高手要经过的6个阶段和6个境界 linux下搭建Android开发环境步骤
从IT菜鸟变为“IT骨干开发者”的11个建议 程序员编程技术迅速提高的终极攻略
2012世界各国人均GDP排名,中国超泰国 2012年全国各省平均工资排行
2012年中国大学高校排行榜(580强排名) 中国各省市面积和人口数量排名
中国百万开发者大调查:程序员的薪水不错 Java高手需要越过的10座高山
周立功谈嵌入式:我的25年嵌入式生涯 Android和Java语言的异同和关系
华为中国区手机销量达千万,80%为智能机 谷歌Android碎片化严重
2012年中国各省GDP和人均GDP排名 90后就业“钱景”:IT仍是最佳选择
2012全球城市竞争力500强,69个中国城市上榜 不要做浮躁的软件工程师
2012年世界500强,79家大陆香港台湾公司上榜名单 给IT新兵的15个建议
美国知名科技公司入门级软件工程师的薪水排名 回顾Java经过的风风雨雨
71道经典Android面试题和答案--重要知识点都涉及到了
芯片巨头海思和展讯:给中国芯片业带来信心 海量经典Java教程、学习资料和源码