How does ibatis support clob and blob?

Source: Internet
Author: User

I have carefully read the ibatis document over the past few days and found that after 2.2, the ibatis change is quite big. The support for custom types is also good, so that the processing of blob and clob data is much simpler.
However, Spring provides good implementation, which saves a lot of effort. Next, let's look at how ibatis supports clob and blob.

Ibatis provides the typehandler interface for processing data types. The basic implementation class is basetypehandler.
In spring, abstractlobtypehandler is provided as the basic class, and corresponding template methods are provided. All the work is processed by lobhandler.
Blobbytearraytypehandler is mainly used to process BLOB data. byte [] is used to map the corresponding blob
Clobstringtypehandler is used to process clob-type data and uses strings to map clob data.
One thing to note is that transaction support is implemented in abstractlobtypehandler and needs to be used to release corresponding resources, so it must be performed in the transaction environment.

The following is a simple example:

Public class food {
Private string content;

Private string ID;

Private byte [] image;

Private string name;
...
}

XML: Description: In resultmap, you can use typehandler to specify a specific handler. In the inline variable, you can use handler to define the corresponding typehandler.

<Sqlmap namespace = "food">

<Typealias alias = "food" type = "org. esoft. HDB. Bo. Food"/>
<Resultmap id = "foodresult" class = "food">
<Result property = "ID" column = "c_id"/>
<Result property = "name" column = "c_name"/>
<Result property = "content" column = "c_content"
Typehandler = "org. springframework. Orm. ibatis. Support. clobstringtypehandler"/>
<Result property = "image" column = "c_image"
Typehandler = "org. springframework. Orm. ibatis. Support. blobbytearraytypehandler"/>
</Resultmap>
<SQL id = "foodfragment"> select c_id, c_name, c_content, c_image from t_food </SQL>
<Select id = "getall" resultmap = "foodresult">
<Include refID = "foodfragment"/>
</SELECT>
<Select id = "selectbyid" parameterclass = "string" resultmap = "foodresult">
<Include refID = "foodfragment"/> where c_id = # ID # </SELECT>

<Insert id = "insert" parameterclass = "food"> insert into t_food (c_id,
C_name, c_content, c_image) values (# ID #,
# Name #, # Content, Handler = org. springframework. Orm. ibatis. Support. clobstringtypehandler #,
# Image, Handler = org. springframework. Orm. ibatis. Support. blobbytearraytypehandler #)
</Insert>

<Update id = "Update" parameterclass = "food"> Update t_food set c_name = # name #,
C_content =
# Content, Handler = org. springframework. Orm. ibatis. Support. clobstringtypehandler #,
C_image =
# Image, Handler = org. springframework. Orm. ibatis. Support. blobbytearraytypehandler #
Where c_id = # ID # </update>

<Delete id = "deletebyid" parameterclass = "string"> Delete from t_food where c_id = # ID #
</Delete>

</Sqlmap>

Public interface foodservice {


Void save (Food food );
Food get (string ID );
/**
* @ Param food
*/
Void Update (Food food );
}

Public class foodserviceimpl implements foodservice {
Private fooddao;

Private daocreator creator;

Public void setcreator (daocreator creator ){
This. creator = creator;
}

Protected fooddao getfooddao (){
If (fooddao = NULL ){
Fooddao = (fooddao) creator. createdao (fooddao. Class, food. Class );
}
Return fooddao;
}

Public food get (string ID ){
Return getfooddao (). Get (ID );
}
Public void save (Food food ){
Getfooddao (). Save (food );
}
Public void Update (Food food ){
Getfooddao (). Update (food );
}

}

Spring xml configuration:
...
<Bean id = "lobhandler"
Class = "org. springframework. JDBC. Support. lob. defaultlobhandler"/>

<Bean id = "transactionmanager"
Class = "org. springframework. JDBC. datasource. datasourcetransactionmanager">
<Property name = "datasource" ref = "datasource"/>
</Bean>

<Bean id = "sqlmapclient"
Class = "org. springframework. Orm. ibatis. sqlmapclientfactorybean">
<Property name = "datasource" ref = "datasource"/>
<Property name = "configlocation">
<Value> sqlmapconfig. xml </value>
</Property>
<Property name = "lobhandler" ref = "lobhandler"/>
</Bean>

<Bean id = "daocreate" class = "org. esoft. HDB. ibatis. ibatisdaocreator">
<Property name = "sqlmapclient" ref = "sqlmapclient"/>
</Bean>

<Bean id = "foodservice" class = "org. esoft. HDB. Service. foodserviceimpl">
<Property name = "creator" ref = "daocreate"/>
</Bean>


<AOP: config>
<AOP: pointcut id = "foodservicemethods"
Expression = "execution (* org. esoft. HDB. Service. foodservice. * (...)"/>
<AOP: Advisor advice-ref = "txadvice" pointcut-ref = "foodservicemethods"/>
</AOP: config>
<TX: Advice id = "txadvice" transaction-Manager = "transactionmanager">
<TX: Attributes>
<TX: method name = "*" propagation = "required"/>
</TX: Attributes>
</TX: Advice>

Simple test:
Save:
Food food = new food ();
Food. setpk ("1 ");
Food. setname ("food1 ");
Bufferedinputstream in = new bufferedinputstream (getclass ()
. Getresourceasstream ("/1.gif "));
Byte [] B = filecopyutils. copytobytearray (in );
Food. setimage (B );
In = new bufferedinputstream (getclass (). getresourceasstream (
"/Hibernate. cfg. xml "));
B = filecopyutils. copytobytearray (in );
Food. setcontent (new string (B ));
Foodservice. Save (food );
Update:
Food food = foodservice. Get ("1 ");
Bufferedinputstream in = new bufferedinputstream (getclass ()
. Getresourceasstream ("/jdbc. properties "));
Byte [] B = filecopyutils. copytobytearray (in );
Food. setcontent (new string (B ));
Foodservice. Update (food );
Food = foodservice. Get ("1 ");
Assertnotnull (food. getimage ());

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.