Use hibernate to save blob big data and ibatis to query blob Big Data

Source: Internet
Author: User
 

Requirement: serialize the object and save it to the database. The database design uses the BLOB data type, the Oracle database, and Hibernate to save the Blob big data. ibatis queries the Blob big data.

 

Some codes are presented as follows:

1. Storage:

1. class to be saved: Class savequerybo implements serializable

 

2. Convert the object to the Blob format: blob savequerybo = hibernate. createblob (searchutil. objecttobyte (savequerybo ));

3. hibernate Configuration:

<Property name = "savequerybo" column = "save_query_bo" type = "Java. SQL. Blob"
Not-null = "false"
/>

4. Save it and stop posting it.

 

Ii. Fetch

1. ibatis Configuration

<Result property = "savequerybo" column = "save_query_bo" jdbctype = "blob" javatype = "Java. Lang. Object"/>

Note:

Javatype = "Java. Lang. Object" must be the same. Otherwise, write a handler ~

2. Corresponding classes:

Private object savequerybo;

 

Although savequerybo is of the Blob type, it must be written as an object, as shown in the preceding figure;

3. query and convert

If (savequerybo! = NULL ){
Blob savequeryboblob = (BLOB) savequerybo;
Return (savequerybo) searchutil. bytetoobject (searchutil. blobtobyte (savequeryboblob ));
}

 

Complete code:

 

 

Import Java. SQL. blob; public class savequeryvo {private string tablekey; // primary key private string queryname; // query name private string usercode; // user ID private string custtype; // customer type private object savequerybo; // search for related classes private string SQL; // sqlpublic string getsql () {return SQL;} public void setsql (string SQL) {This. SQL = SQL;}/*** the returned BLOB Object must be forced to convert */public object getsavequerybo () {return savequerybo;}/*** the returned saveq Uerybo object * @ throws crmbaseexception */Public savequerybo getsavequerybo () throws crmbaseexception {try {If (savequerybo! = NULL) {blob savequeryboblob = (BLOB) savequerybo; Return (savequerybo) searchutil. bytetoobject (searchutil. blobtobyte (savequeryboblob);} catch (exception e) {e. printstacktrace (); // todo throw new crmbaseexception ("system error");} return NULL;} public void setsavequerybo (Object savequerybo) {This. savequerybo = savequerybo;} Public String getcusttype () {return custtype;} public void setcusttype (string custtype) {This. custtype = custtype;} Public String gettablekey () {return tablekey;} public void settablekey (string tablekey) {This. tablekey = tablekey;} Public String getqueryname () {return queryname;} public void setqueryname (string queryname) {This. queryname = queryname;} Public String getusercode () {return usercode;} public void setusercode (string usercode) {This. usercode = usercode ;}}

Import Java. io. bufferedinputstream; import Java. io. bytearrayinputstream; import Java. io. bytearrayoutputstream; import Java. io. objectinputstream; import Java. io. objectoutputstream; import Java. SQL. blob; public class searchutil {/*** '* Get the code * @ return */public static string getmaintablecode () {return searchconstant. main_table_code_pe_value;}/***** @ Param columntype * @ Param columnvalue * @ return * @ throws crmbaseexception */public static string getcolumvalue4sql (string columntype, string columnvalue) throws crmbaseexception {// If (searchconstant. columntype_number.equals (columntype) | searchconstant. columntype_double.equals (columntype) {If (stringutil. isempty (columnvalue) {return "0" ;}else {return columnvalue ;}} else if (searchconstant. columntype_date.equals (columntype) {// when the type is date if (stringutil. isempty (columnvalue) {return "'' ";} else {return" to_date ('"+ columnvalue +"', 'yyyy-mm-dd ')";}} else {// default if (stringutil. isempty (columnvalue) {return "'' ";} else {return" '"+ columnvalue + "'";}}} /*** convert an object to a byte stream * @ Param OBJ * @ return * @ throws exception */public static byte [] objecttobyte (Object object) throws crmbaseexception {try {If (Object = NULL) return NULL; bytearrayoutputstream Bos = new bytearrayoutputstream (); objectoutputstream OOS = new objectoutputstream (BOS); OOS. writeobject (object); byte [] bytes = Bos. tobytearray (); Bos. close (); OOS. close (); Return bytes;} catch (exception e) {e. printstacktrace (); throw new crmbaseexception (E. getmessage () ;}}/*** convert bytes into objects * @ Param bytes * @ return * @ throws exception */public static object bytetoobject (byte [] bytes) throws exception {If (Bytes = NULL) return NULL; bytearrayinputstream Bis = new bytearrayinputstream (bytes); objectinputstream OIS = new objectinputstream (bis); object = Ois. readobject (); bis. close (); ois. close (); return object;} public static byte [] blobtobyte (BLOB) {If (BLOB = NULL) return NULL; byte [] listbyte = NULL; try {bufferedinputstream is = new bufferedinputstream (blob. getbinarystream (); int Len = (INT) blob. length (); listbyte = new byte [Len]; int offset = 0; int READ = 0; while (offset <Len & (read = is. read (listbyte, offset, len-offset)> = 0) {Offset + = read;} catch (exception e) {e. printstacktrace ();} return listbyte ;}}

 

<resultMap id="saveQueryVO" class="SaveQueryVO"><result property="tableKey" column="table_Key" /><result property="queryName" column="query_Name" /><result property="userCode" column="user_Code" /><result property="custType" column="cust_Type" /><result property="saveQueryBo" column="save_Query_Bo" jdbcType="BLOB" javaType = "java.lang.Object"/><result property="sql" column="sql" /></resultMap>

 

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.