Java – mybatis custom typehandler is not executed when tags are placed
I have a < resultmap > that uses a custom typehandler as one of the result properties:
<resultMap id="foo" type="hashmap"> ... <result property="SERVICES_XML" javaType="string" jdbcType="CLOB" typeHandler="com.foo.bar.OracleClobTypeHandler" /> ... </resultMap>
No matter which attribute I attach to my handler (I mean, this is not a CLOB specific problem, and I try to use varchar), the handler will not be called when I get the result from the database
I set breakpoints in all methods of the custom handler:
public class OracleClobTypeHandler implements TypeHandler<String> { @Override public void setParameter(PreparedStatement ps,int i,String parameter,JdbcType jdbcType) throws sqlException { log.debug("setParameter called"); <================ BREAKPOINT HERE } @Override public String getResult(ResultSet rs,String columnName) throws sqlException { log.debug("getResult 2 called"); <================ BREAKPOINT HERE return ""; } @Override public String getResult(ResultSet rs,int columnIndex) throws sqlException { log.debug("getResult 2 called"); <================ BREAKPOINT HERE return ""; } @Override public String getResult(CallableStatement cs,int columnIndex) throws sqlException { log.debug("getResult 3 called"); <================ BREAKPOINT HERE return ""; } }
Obviously, the above method was not implemented
I tried to put < typehandler javatype = "Java. Lang. string" JDBC type = "CLOB" handler = "com. Foo. Bar. Oracleclobtypehandler" / > in mybatis < configuration >, but it didn't work I didn't do anything else, including extending typehandler < < Object >, etc
What on earth did I do wrong?
Solution
After a long time of digging, I finally found the answer
This seems to be a mistake in mybatis
In order for your handler to apply to the < result > element, you must explicitly specify the column attribute even if the attribute already matches the column name and field name in the bean For me, it looks like this:
<result property="SERVICES_XML" column="SERVICES_XML" javaType="string" jdbcType="CLOB" typeHandler="com.foo.bar.OracleClobTypeHandler" />
Note that the above changes will also result in handlers defined in < configuration > Tags work, so inline typehandler may no longer be needed - this is my case I'm done:
<configuration> <typeHandlers> <typeHandler javaType="java.lang.String" jdbcType="CLOB" handler="com.foo.bar.OracleClobTypeHandler"/> </typeHandlers> </configuration>
and
<result property="SERVICES_XML" column="SERVICES_XML" javaType="string" jdbcType="CLOB" />