Use spring's hibernatetemplate to operate MySQL blob

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.