Ibatis-spring-mysql processes BLOB fields

Source: Internet
Author: User

1) Spring configuration file:

<Bean id = "lobhandler" class = "org. springframework. JDBC. Support. lob. defaultlobhandler"/>
 
<Bean id = "sqlmapclient" class = "org. springframework. Orm. ibatis. sqlmapclientfactorybean">
<Property name = "configlocation" value = "classpath: CONF/sqlmapconfig. xml"> </property>
<Property name = "datasource" ref = "performance2"> </property>
<Property name = "lobhandler" ref = "lobhandler"> </property>
</Bean>

2) ibatis configuration file:

Sqlmapconfig. xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype sqlmapconfig public "-// ibatis.com//dtd SQL map config 2.0/EN"
Http://www.ibatis.com/dtd/sql-map-config-2.dtd>

<Sqlmapconfig>
<Properties resource = "CONF/serverity. properties"/>
<Settings usestatementnamespaces = "true" cachemodelsenabled = "false" maxrequests = "256" maxsessions = "64" maxtransactions = "16"/>
<Typehandler jdbctype = "blob" javatype = "[B" Callback = "org. springframework. Orm. ibatis. Support. blobbytearraytypehandler"/>
<Sqlmap resource = "CONF/sqlmap/monitorsqlmap. xml"/>
</Sqlmapconfig>

Monitorsqlmap. xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype sqlmap
Public "-// ibatis.com//dtd SQL map 2.0 // en"
Http://www.ibatis.com/dtd/sql-map-2.dtd>

<Sqlmap namespace = "favmonitor">
<Select id = "queryvobytes" parameterclass = "Java. util. Map" resultclass = "[B">
Select volistbytes from t_favmonitor where userid = # userid # And reqname = # reqname #
</SELECT>
</Sqlmap>

3) MySQL database:

Table: t_favmonitor (userid int, reqname varchar, volistbytes BLOB)

4) Java access:

Private Static class byteobjectutil {

Static byte [] convertobj2bytearray (Object OBJ) throws ioexception {
Bytearrayoutputstream baos = NULL;
Objectoutputstream OOS = NULL;
Try {
Baos = new bytearrayoutputstream ();
Oos = new objectoutputstream (baos );
Oos. writeobject (OBJ );
Return baos. tobytearray ();
} Catch (ioexception e ){
Throw new ioexception (E );
} Finally {
Baos. Close ();
Oos. Close ();
}
}


Static object readobjfrombytearray (byte [] OB) throws illegalstateexception, ioexception, classnotfoundexception {
If (Ob = NULL | OB. Length = 0)
Throw new illegalstateexception ("parameter byte [] OB is empty! ");
Bytearrayinputstream BAIS = new bytearrayinputstream (OB );
Objectinputstream OIS = NULL;
Try {
Ois = new objectinputstream (BAIS );
Return Ois. readobject ();
} Catch (ioexception e ){
Throw new ioexception (E );
} Finally {
BAIS. Close ();
Ois. Close ();
}
}
}

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.