MyBatis implement a Typehandler method for a custom type converter _java

Source: Internet
Author: User

Let's give you a brief introduction to the MyBatis.

MyBatis is an excellent persistence layer framework that supports common SQL queries, stored procedures, and advanced mappings. MyBatis eliminates the manual setting of almost all JDBC code and parameters and the retrieval encapsulation of the result set. MyBatis can use simple XML or annotations for configuration and raw mappings, mapping interfaces and Java Pojo (Plain old Java Objects, normal Java objects) to records in the database.

This article is actually a Java operation Oracle type XmlType Summary II: A summary of the use of MyBatis.

MyBatis implements a custom converter, very simple, its main steps are divided into three steps, here to operate XmlType type for example.

First step

Creates a new transformation class, implements the Typehandler interface, specifies the parameter type for the interface's generic type, and does not specify an object:

public class Xmltypetypehandler implements Typehandler<string>

The interface has the following 4 main methods:

public void Setparameter (preparedstatement ps, int i, String parameter, Jdbctype jdbctype) public String GetResult (ResultS Et rs, string columnName) throws SQLException public
string GetResult (ResultSet rs, int columnindex) throws Sqlexcepti On the public
String GetResult (callablestatement cs, int columnindex) throws SQLException

The role of the method to see the name should be able to understand, Setparameter is the operation when the parameter is passed in, the code that needs to do processing before the parameter passes to the database, can write in this method, the other three are to obtain the query result method, after obtaining the JDBC query result can convert to any type you want.

Second Step

In Mapper-config, register your implementation of the converter class, Where jdbctype can specify a type that is explicitly defined in the MyBatis enumeration class Org.apache.ibatis.type.JdbcType, it cannot be a value other than the enumeration, or an error occurs. This is specified as undefined because there is no xmltype type we need in the enumeration. (You can also specify a specific class with Typehandler when you use it, without specifying a specific type):

<typeHandlers>
<typehandler javatype= "string" jdbctype= "UNDEFINED" handler= " Com.tyyd.dw.context.XmltypeTypeHandler "/>
</typeHandlers>

Third Step

Use a type converter in your mapper mapping file:

Insert INTO T_content (
<include refid= "Fullcolumns"/>
) VALUES (
#{controlid,jdbctype=bigint},
#{xmlfile,javatype=string,jdbctype=undefined,typehandler=com.tyyd.dw.context.xmltypetypehandler},
#{ DRMFILE,JDBCTYPE=BLOB}
)

Note the incoming xmlfile parameters, specifying Javatype, Jdbctype, and Typehandler, indicating which type of processor we want to use, and of course you can specify only one of them, but the item must exist only if there are multiple and unspecified words, MyBatis can cause errors because they cannot be distinguished.

At this point, a MyBatis custom type converter is complete, and it should be noted that the type processor specified above works only when inserting data, and wants to use a custom type processor in the query, which needs to be specified in the label of the attribute within the Resultmap. The label Javatype, Jdbctype, and Typehandler names and usage are the same, and there is no more to repeat.

Enclose the complete type converter code, because the XmlType type can be queried by using the database's Xmltype.getclobval () to return directly as a string, so the returned method has no special handling. (You can also use the Xmltype.getstringval () function to return a string, but it is found that when the field is null, Getstringval () appears ora-06502:numeric or value error when the field is actually used: Character string buffer too small error, getclobval () does not appear error, so the Getclobval () function is recommended:

/** * Oracle SYS. XmlType Type Custom Processor * * User:liyd * date:13-12-27 * Time: PM 4:53/public class Xmltypetypehandler implements <String> {@Override public void Setparameter (preparedstatement ps, int i, String parameter, Jdbctype jdbctype) thro WS SQLException {//Prevent null, create XmlType error if (Stringutils.isnotblank (parameter)) {Delegatingconnection connection = (
delegatingconnection) ps.getconnection (). GetMetaData (). getconnection ();
XmlType XmlType = Xmltype.createxml (Connection.getdelegate (), parameter);
Ps.setobject (i, xmltype);
else {ps.setstring (i, NULL);}} @Override public String GetResult (ResultSet rs, String columnName) throws SQLException {//
The XmlType field uses Xmltype.getclobval () to return back to Rs.getstring (columnName) when the database SQL query is used. 
@Override public String GetResult (ResultSet rs, int columnindex) throws SQLException {return rs.getstring (columnindex); @Override public String GetResult (callablestatement cs, int columnindex) throws SQLException {return cs.getstring (colUmnindex); }
}

The above is a small series to introduce the MyBatis implementation of the custom type converter Typehandler method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.