Hibernate, Ibatis with blob

xiaoxiao2021-03-06  19

When storing binary information such as a picture, executable file (of course directly on the file system), the blob data is sent to the field. There is no too much depth of this article, and you can provide you with a reference feet in the development process.

Hibernate and SQL Server Blob

BLOB data is mainly reflected by the Image type in the SQL Server database, the maximum capacity is 2GB. Its storage mode is different from a normal data type, and for normal types of data systems store data values ​​directly on user-defined fields, for Image type data, the system opens up new storage pages to store these data, the iMage type data field Allows only a 16-byte pointer that points to the page of the image data stored in the record. If you still don't work about Hibernate, please see here.

A table named "blob_test", the fields are Int type "ID" and "myblob" of the Image type, respectively. Read "Sample.jpg" from the file system and convert into the byte array and put it in the blobtest object instance. The write procedure is as follows:

Import java.io. *; import net.sf.hibernate. *; import net.sf.hibernate.cfg. *; import bo. *; public class tester {public void dotest () {inputstream in = null; blobtest blobtest = null; Configuration cfg = null; SessionFactory sessions = null; Session session = null; Transaction tx = null; try {// begin InputStream in = new FileInputStream ( "d: /sample.jpg"); byte [] b = new byte [in.available ()]; in .read (b); in .close (); // begin blobtest blobtest = new blobtest (); blobtest.setmyblob (b); // begin hibernate session cfg = new configuration () .configure (); sessions = cfg.buildsessionFactory (); session = sessions.opensession (); tx = session.begintransaction (); session.save (blobtest); tx.commit ();} catch (Exception E) {e) .printstacktrace ();} finally {type {session.close ();} catch (exception e1) {e1.printstacktrace ();}}}}

Import java.io. *; import net.sf.hibernate. *; import net.sf.hibernate.cfg. *; import bo. *; public class tester {public void dotest () {inputstream in = null; blobtest blobtest = null; Configuration cfg = null; SessionFactory sessions = null; Session session = null; Transaction tx = null; try {// begin InputStream in = new FileInputStream ( "d: /sample.jpg"); byte [] b = new byte [in.available ()]; in .read (b); in .close (); // begin blobtest blobtest = new blobtest (); blobtest.setmyblob (b); // begin hibernate session cfg = new configuration () .configure (); sessions = cfg.buildsessionFactory (); session = sessions.opensession (); tx = session.begintransaction (); session.save (blobtest); tx.commit ();} catch (Exception E) {e) .printstacktrace ();} finally {type ();} catch (Exception E1) {E1.PrintStackTrace ();}}}} Take the procedure as follows:

Import java.io. *; import net.sf.hib.Hibernate. *; import.sf.hibernate.cfg. *; import bo. *; public class tester {public void dotest () {OutputStream out = null; blobtest blobtest = null; Configuration cfg = null; SessionFactory sessions = null; Session session = null; try {// begin Hibernate Session cfg = new Configuration () configure ();. sessions = cfg.buildSessionFactory (); session = sessions.openSession () // begin blobtest blobtest = new blobtest (); blobtest = (blobtest) session.load (blobtest.class, new integer (23)); // begin outstream out = new fileoutputstream ("d: /sample.jpg") Out.write (blobtest.getMyblob ()); out.flush (); out.close ();} catch (exception e) {E.PrintStackTrace ();} finally {type {session.close ();} catch (EXCETION E1) {E1.PrintStackTrace ();}}}}

Import java.io. *; import net.sf.hib.Hibernate. *; import.sf.hibernate.cfg. *; import bo. *; public class tester {public void dotest () {OutputStream out = null; blobtest blobtest = null; Configuration cfg = null; SessionFactory sessions = null; Session session = null; try {// begin Hibernate Session cfg = new Configuration () configure ();. sessions = cfg.buildSessionFactory (); session = sessions.openSession () // begin blobtest blobtest = new blobtest (); blobtest = (blobtest) session.load (blobtest.class, new integer (23)); // begin outstream out = new fileoutputstream ("d: /sample.jpg") Out.write (blobtest.getMyblob ()); out.flush (); out.close ();} catch (exception e) {E.PrintStackTrace ();} finally {type {session.close ();} catch (Exception E1) {E1.PrintStackTrace ();}}}} Hibernate with MySQL BLOB

The BLOB data in mysql is reflected in four types, which are tinyblob its capacity of 256 bytes. The BLOB has 64KB, and the Mediumblob has 16MB, and the length of Longblob is 4GB.

A table named "blob_test", the fields are "ID" and "myblob" of the Integer type and MediumBlob type, respectively. Read "Sample.jpg" from the file system and convert into the byte array and put it in the blobtest object instance. Write, remove the program, and the SQL Server above.

Hibernate with Oracle Blob

In order not to use "for Update" to lock the database, intends to save the Oracle Long Raw type to save large objects, maximum capacity 2GB. After the test, I found that the JDBC code can be saved directly, but Hibernate can only save 4K size content, and it is not possible to successfully be successfully replaced by Hibernate 3.0 Beta3. The chance of accidental opportunities is discovered on the mailing list, which is a problem with the JDBC Driver, replaced by Oracle 10G, the problem is resolved.

A table named "blob_test" is new, and the fields are Number type "ID" and "myblob" for long Raw types. Read "Sample.jpg" from the file system and convert into the byte array and put it in the blobtest object instance. Write, remove the program, and SQL Server.

If you must use the Oracle Blob type, then look down:

Hibernate handles the Oracle BLOB type, read from the file system "Sample.jpg" to the blobtest object instance is a java.sql.blob type, not byte arrays.

Import java.io. *; import net.sf.hib.hibernate. *; import.sf.hibernate.cfg. *; import oracle.sql. *; import bo. *; public class tester {public void dotest () {blob blob = null; InputStream in = null; OutputStream out = null; BlobTest blobTest = null; Configuration cfg = null; SessionFactory sessions = null; Session session = null; Transaction tx = null; try {// begin InputStream in = new FileInputStream ( "d: /sample.jpg"); byte [] b = new byte [in.available ()]; in.read (b); in.close (); // begin blobtest blobtest = new blobtest (); blobtest () .SETMYBLOB (BLOB.EMPTY_LOB ()); // Begin Hibernate session cfg = new configuration (). configure (); sessions = cfg.buildsessionFactory (); session = sessions.opensession (); tx = session.begintrance (); Session.save (blobtest); session.flush (); session.refresh (blobtest, lockmode.upgrade); blob = (blob) blobtest.getMyblob (); out = blob.getbinaryoutputstream (); out.write (b); Out.close (); session.flush (); tx.commit (); (Exception E) {E.PrintStackTrace ();} finally {type {session.close ();} catch (Exception E1) {E1.PrintStackTrace ();}}}}

Import java.io. *; import net.sf.hib.hibernate. *; import.sf.hibernate.cfg. *; import oracle.sql. *; import bo. *; public class tester {public void dotest () {blob blob = null; InputStream in = null; OutputStream out = null; BlobTest blobTest = null; Configuration cfg = null; SessionFactory sessions = null; Session session = null; Transaction tx = null; try {// begin InputStream in = new FileInputStream ( "d: /sample.jpg"); byte [] b = new byte [in.available ()]; in.read (b); in.close (); // begin blobtest blobtest = new blobtest (); blobtest () .SETMYBLOB (BLOB.EMPTY_LOB ()); // Begin Hibernate session cfg = new configuration (). configure (); sessions = cfg.buildsessionFactory (); session = sessions.opensession (); tx = session.begintrance (); Session.save (blobtest); session.flush (); session.refresh (blobtest, lockmode.upgrade); blob = (blob) blobtest.getMyblob (); out = blob.getbinaryoutputstream (); out.write (b); Out.close (); session.flush (); tx.commit ();} Catch (exception e) {E.PrintStackTrace ();} finally {type {session.close ();} catch (exception E1) {e1.printStackTrace ();}}}} Removal program and other two Database operation is almost the same.

Ibatis SQL MAPS with SQL Server Blob

The formation table process is the same as Hibernate, if you still don't work about Ibatis SQL Maps, please see here.

The mapping file is as follows:

< SelectKey Resultclass = "java.lang.integer" keyproperty = "id"> The write program is as follows:

Import java.io. *; import com.ibatis.sqlmap.client. *; import com.ibatis.common.Resources. *; import bo. *; public class tester {public void dotest () {byte [] b = null ; Reader reader = null; InputStream in = null; BlobTest blobTest = null; SqlMapClient sqlMap = null; String resource = "SqlMapConfig.xml"; try {// begin InputStream in = new FileInputStream ( "d: /sample.jpg") ; B = new byte [in.available ()]; in.read (b); in.close (); // begin blobtest blobtest = new blobtest (); blobtest.setMyblob (b); // begin sqlmapclient ready = Resources.getResourceAsReader (resource); sqlMap = SqlMapClientBuilder.buildSqlMapClient (reader); sqlMap.startTransaction (); sqlMap.insert ( "insertBlob", blobTest); sqlMap.commitTransaction ();} catch (Exception e) {e.printStackTrace ( (} Finally {try {sqlmap.endtransaction ();} catch (Exception E1) {E1.PrintStackTrace ();}}}}

Import java.io. *; import com.ibatis.sqlmap.client. *; import com.ibatis.common.Resources. *; import bo. *; public class tester {public void dotest () {byte [] b = null ; Reader reader = null; InputStream in = null; BlobTest blobTest = null; SqlMapClient sqlMap = null; String resource = "SqlMapConfig.xml"; try {// begin InputStream in = new FileInputStream ( "d: /sample.jpg") ; B = new byte [in.available ()]; in.read (b); in.close (); // begin blobtest blobtest = new blobtest (); blobtest.setMyblob (b); // begin sqlmapclient ready = Resources.getResourceAsReader (resource); sqlMap = SqlMapClientBuilder.buildSqlMapClient (reader); sqlMap.startTransaction (); sqlMap.insert ( "insertBlob", blobTest); sqlMap.commitTransaction ();} catch (Exception e) {e.printStackTrace ( (} Finally {try {sqlmap.endtransaction ();} catch (exception E1) {e1.printStackTrace ();}}}} The take-out program is as follows:

Import java.io. *; import com.ibatis.sqlmap.client. *; import com.ibatis.common.Resources. *; import bo. *; public class tester {public void dotest () {reader reader = null; OutputStream out = null; BlobTest blobTest = null; SqlMapClient sqlMap = null; String resource = "SqlMapConfig.xml"; try {// begin BlobTest blobTest = new BlobTest (); blobTest.setId (new Integer (21)); // begin SqlMapClient reader = Resources.getResourceAsReader (resource); sqlMap = SqlMapClientBuilder.buildSqlMapClient (reader); blobTest = (BlobTest) sqlMap.queryForObject ( "getBlob", blobTest); // begin OutputStream out = new FileOutputStream ( "d: / sample. "); out.write (blobtest.getMyblob ()); out.close ();} catch (exception e) {E.PrintStackTrace ();} finally {try {sqlmap.endtransaction ();} catch (Exception E1 ) {E1.PrintStackTrace ();}}}}}

Import java.io. *; import com.ibatis.sqlmap.client. *; import com.ibatis.common.Resources. *; import bo. *; public class tester {public void dotest () {reader reader = null; OutputStream out = null; BlobTest blobTest = null; SqlMapClient sqlMap = null; String resource = "SqlMapConfig.xml"; try {// begin BlobTest blobTest = new BlobTest (); blobTest.setId (new Integer (21)); // begin SqlMapClient reader = Resources.getResourceAsReader (resource); sqlMap = SqlMapClientBuilder.buildSqlMapClient (reader); blobTest = (BlobTest) sqlMap.queryForObject ( "getBlob", blobTest); // begin OutputStream out = new FileOutputStream ( "d: / sample. "); out.write (blobtest.getMyblob ()); out.close ();} catch (exception e) {E.PrintStackTrace ();} finally {try {sqlmap.endtransaction ();} catch (Exception E1 ) {E1.PrintStackTrace ();}}}}} SQL MAPS with MySQL BLOB

This topic is simple, you need to pay attention to the mapping file INSERT element primary key generation mode, write, remove the program, and SQL Server above:

iBatis SQL MAPS with Oracle Blob

Use Oracle Long Raw type, note mapping file INSERT element primary key generation mode, write, remove the program, and the above SQL Server:

If you must use the Oracle Blob type, then look down:

Before IBATIS 2.0.9, the Oracle BLOB type is quite troublesome, and you must implement the TypeHandlerCallback interface. IBATIS 2.0.9 provides the blobTypeHandlerCallback implementation class, write, remove the program, and SQL Server above. Just the mapping file Resultmap element needs to be modified:

(Please note that the reference should indicate the original author posted this article:! Rosen Jiang and source: http: //blog.9cbs.net/rosen)

(Note that the reference should indicate the original author posted this article:! Rosen Jiang and Source: http: //blog.9cbs.net/rosen)

转载请注明原文地址:https://www.9cbs.com/read-56661.html

New Post(0)