場景:想將數(shù)據(jù)庫查詢出的-1統(tǒng)一轉(zhuǎn)化成null
解決: 通過mybatis的BaseTypeHandler實(shí)現(xiàn)特殊字段映射
以long類型為例,其他類型同理
@Component
public class LongHandler extends BaseTypeHandler<Long> {
@Override
public void setNonNullParameter(PreparedStatement preparedStatement, int i, Long number, JdbcType jdbcType) throws SQLException {
preparedStatement.setLong(i, number);
}
@Override
public Long getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
Long value = resultSet.getLong(columnName);
if (value == -1) {
return null;
}
return value;
}
@Override
public Long getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {
Long value = resultSet.getLong(columnIndex);
if (value == -1) {
return null;
}
return value;
}
@Override
public Long getNullableResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
Long value = callableStatement.getLong(columnIndex);
if (value == -1) {
return null;
}
return value;
}
參考鏈接:
1友酱、MyBatis借助BaseTypeHandler實(shí)現(xiàn)特殊的字段(數(shù)組或json)映射