Let me give you a brief introduction to mybatis
MyBatis is an excellent persistence layer framework that supports plain SQL queries, stored procedures and advanced mapping. MyBatis eliminates manual settings of almost all JDBC code and parameters and search encapsulation of the result set. MyBatis can use simple XML or annotations for configuration and original mapping, mapping interfaces and Java's POJOs (Plain Old Java Objects) into records in the database.
This article is actually a summary of the Java operation Oracle type XMLType summary 2: Using Mybatis.
Mybatis implements a custom converter, which is very simple. Its main steps are divided into three steps. Here we take operating XMLType types as an example.
first step
Create a new conversion class to implement the TypeHandler interface. The generics of the interface specify the parameter type, and if not specified, it is Object:
public class XmltypeTypeHandler implements TypeHandler<String>
This interface has the following 4 methods:
public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) public String getResult(ResultSet rs, String columnName) throws SQLExceptionpublic String getResult(ResultSet rs, int columnIndex) throws SQLExceptionpublic String getResult(CallableStatement cs, int columnIndex) throws SQLException
The function of the method should be understood by the name. SetParameter is an operation when passing in parameters. The code that needs to be processed before the parameters are passed into the database can be written in this method. The other three are methods of obtaining the query result. After obtaining the query result of jdbc, it can be converted to any type you want.
Step 2
In mapper-config, register the converter class you implemented, where the type that jdbcType can specify is clearly defined in Mybatis' enumeration class org.apache.ibatis.type.JdbcType. It cannot be a value other than the enum, otherwise an error will occur. Here, because the enum does not have the XMLType type we need, it is specified as UNDEFINED. (You can also not specify specific types. You can use typeHandler to specify specific classes when using):
<typeHandlers><typeHandler javaType="string" jdbcType="UNDEFINED" handler="com.tyyd.dw.context.XmltypeTypeHandler"/></typeHandlers>
Step 3
Use 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})Pay attention to the incoming xmlFile parameters, specifying javaType, jdbcType and typeHandler, indicating which type of processor we want to use. Of course, you can only specify one of them, but the existence of this item must be unique. If multiple are exactly the same and the specification is not clear, Mybatis will cause an error because it cannot distinguish.
At this point, a custom type converter for mybatis has been implemented. It should be noted that the type processor specified above only works when inserting data. If you want to use a custom type processor during query, you need to specify it in the tags of the attributes in the resultMap. The names and usage methods of the tags javaType, jdbcType and typeHandler are the same, so I will not repeat them here.
Attach the complete type converter code. Because when XMLType type is to be queryed, you can use the database's xmltype.getclobval() to directly return as string to operate, so the returned methods have not been processed specially. (You can also use the xmltype.getstringval() function to return string, but during the actual use, it is found that when the field is null, getstringval() will have an error of ORA-06502:numeric or value error:character string buffer too small, getclobval() will not have an error, so it is recommended to use getclobval() function):
/*** oracle SYS.XMLTYPE Type Custom Processor* * User: liyd* Date: 13-12-27* Time: 4:53 pm*/public class XmltypeTypeHandler implements TypeHandler<String> {@Overridepublic void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)throws SQLException {//Prevent errors from creating XMLType when null is needed 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);}}@Overridepublic String getResult(ResultSet rs, String columnName) throws SQLException {//The xmltype field uses xmltype.getclobval() to return return when querying database sql rs.getString(columnName);}@Overridepublic String getResult(ResultSet rs, int columnIndex) throws SQLException {return rs.getString(columnIndex);}@Overridepublic String getResult(CallableStatement cs, int columnIndex) throws SQLException {return cs.getString(columnIndex);}}The above is the method of implementing the TypeHandler for Mybatis to implement the customized type converter by TypeHandler. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!