在mybatis的configure文件中增加typehandler:
<configuration>
<typeHandlers>
<typeHandler handler="com.test.framework.utils.MyStringTypeHandler" javaType="java.lang.String" jdbcType="CHAR"/>
<typeHandler handler="com.test.framework.utils.MyStringTypeHandler" javaType="java.lang.String" jdbcType="VARCHAR"/>
</typeHandlers>
…….
</configuration>
public class MyStringTypeHandler extends NStringTypeHandler {
public String getResult(ResultSet rs, String columnName) throws SQLException {
String result = getNullableResult(rs, columnName);
if (rs.wasNull()) {
return null;
} else {
return result.trim();
}
}
public String getResult(ResultSet rs, int columnIndex) throws SQLException {
String result = getNullableResult(rs, columnIndex);
if (rs.wasNull()) {
return null;
} else {
return result.trim();
}
}
public String getResult(CallableStatement cs, int columnIndex) throws SQLException {
String result = getNullableResult(cs, columnIndex);
if (cs.wasNull()) {
return null;
} else {
return result.trim();
}
}
}