I searched a lot of articles on the Internet and found that many things need to be configured. In addition, I also reported errors when configuring them. I found that a method without special configuration is easy to operate. I don't need to talk much about it. Read the Code:
Package COM. CZP. test; public class blobtest {private int ID; private object OBJ; // This can be any object private byte [] bytes; Public byte [] getbytes () {return bytes ;} public void setbytes (byte [] bytes) {This. bytes = bytes;} public int GETID () {return ID;} public void setid (int id) {This. id = ID;} public object getobj () {return OBJ;} public void setobj (Object OBJ) {This. OBJ = OBJ ;}}
Package COM. CZP. test; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. objectinputstream; import Java. io. objectoutputstream; import Java. SQL. blob; import Java. util. arraylist; import Java. util. list; import Org. hibernate. hibernate; import Org. springframework. orm. hibernate3.support. hibernatedaosupport; public class blobdao extends hibernatedaosupport {/*** Save Like * @ Param blobtest * @ throws exception */Public void add (blobtest) throws exception {file = file. createtempfile (blobtest. hashcode () + "", null); If (file! = NULL) {objectoutputstream Ob = new objectoutputstream (New fileoutputstream (File); ob. writeobject (blobtest. getobj (); ob. flush (); ob. close (); Ob = NULL; fileinputstream FCM = new fileinputstream (File); byte [] B = new byte [FCM. available ()]; FCM. read (B); FCM. close (); blobtest. setbytes (B); this. gethibernatetemplate (). save (blobtest) ;}}/*** get object * @ return * @ throws exception */@ suppresswarnings ("unchecked") public list <blobtest> getblob () throws exception {list <blobtest> listtmp = new arraylist <blobtest> (); List <blobtest> List = This. gethibernatetemplate (). loadall (blobtest. class); For (blobtest BLT: List) {blob B = hibernate. createblob (BLT. getbytes (); objectinputstream OIS = new objectinputstream (B. getbinarystream (); object OBJ = Ois. readobject (); ois. close (); ois = NULL; BLT. setobj (OBJ); listtmp. add (BLT) ;}return listtmp ;}}
Hibernate spring configuration is omitted. You only need to map the bytes attribute of blobtest to the following in the hibernate ing file: <property name = "bytes" type = "binary" not-null = "true" column = "'xxxx'"> </property>