自定义个TypeHandler 然后把setParameter的setString改成setObject
@Component
@MappedTypes(String.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class StringTypeCustomizeHandler implements TypeHandler<String> {
@Override
public void setParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {
preparedStatement.setObject(i, s);
}
@Override
public String getResult(ResultSet resultSet, String s) throws SQLException {
return resultSet.getString(s);
}
@Override
public String getResult(ResultSet resultSet, int i) throws SQLException {
return resultSet.getString(i);
}
@Override
public String getResult(CallableStatement callableStatement, int i) throws SQLException {
return callableStatement.getString(i);
}
}
springboot中指定TypeHandler的路径
#用到了mybatis-plus
mybatis-plus.type-handlers-package=com.xiaobu.handlers
SqlSessionFactory也需要指定
@Autowired
StringTypeCustomizeHandler stringTypeCustomizeHandler;
TypeHandler<?>[] typeHandlers=new TypeHandler[]{stringTypeCustomizeHandler};
sqlSessionFactory.setTypeHandlers(typeHandlers);
然后mapper.xml里面指定类型
<result column="name" typeHandler="com.xiaobu.handlers.StringTypeCustomizeHandler" property="name" />
#{name,typeHandler=com.xiaobu.handlers.StringTypeCustomizeHandler}