Reputation: 47
In 3.2.8 official documentation,in 'typeHandlers' section,i have a test that,and then found it can't override the default org.apache.ibatis.type.StringTypeHandler. I debug it,it was stop in StringTypeHandler,not ExampleTypeHandler. here is the mybatis-config.xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="com.solverpeng.mybatis.beans"/>
</typeAliases>
<typeHandlers>
<typeHandler handler="com.solverpeng.mybatis.typeHandlers.ExampleTypeHandler"/>
</typeHandlers>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/solverpeng/mybatis/beans/mapper/CustomerMapper.xml"/>
</mappers>
</configuration>
and here is the ExampleTypeHandler.java
@MappedJdbcTypes(JdbcType.VARCHAR)
public class ExampleTypeHandler extends BaseTypeHandler<String> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
ps.setString(i, parameter);
}
@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
return rs.getString(columnName);
}
@Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return rs.getString(columnIndex);
}
@Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return cs.getString(columnIndex);
}
}
Upvotes: 2
Views: 7280
Reputation: 2044
I assume you are trying to override the Varchar => String default type handler But the documentation you refers to specifies:
You can override the type handlers or create your own to deal with unsupported or non-standard types.
Varchar <=> String is one of most common cases of supported/standard type handling.
It seems Mybatis does not allow implicit overriding (defined in configuration, then global for the application) of type handler defined in the lib by user defined type handler.
Then you may have to explicitly reference the custom result handler in the resultMap:
<result property="name" column="Name" typeHandler="com.example.mybatis.type.handler.ExampleTypeHandler" />
or when setting parameter:
#{name, typeHandler=com.example.mybatis.type.handler.ExampleTypeHandler}
EDIT: after further testing, it turns out that the ExampleTypeHandler can be called without explicit declaration in the resulMap. The culprit would be the annotation that should be removed:
@MappedJdbcTypes(JdbcType.VARCHAR)
comment the annotation & run / uncomment & run, you will see the difference.
Upvotes: 5