已解决:mybatis 查询默认过滤值为空的字段

实现Mybatis的TypeHandler接口,在result 中应用这个转换器:

<result column="introduction" property="introduction" jdbcType="VARCHAR" typeHandler="com.ssm.util.EmptyStringIfNull"/>

EmptyStringIfNull的代码如下:

public class EmptyStringIfNull implements TypeHandler<String> {

@Override
public String getResult(ResultSet rs, String columnName) throws SQLException {
return (rs.getString(columnName) == null) ? "" : rs.getString(columnName); 
}

@Override
public String getResult(ResultSet rs, int columnIndex) throws SQLException {
return (rs.getString(columnIndex) == null) ? "" : rs.getString(columnIndex);
}
@Override
public String getResult(CallableStatement cs, int columnIndex) throws SQLException {
return (cs.getString(columnIndex) == null) ? "" : cs.getString(columnIndex);
}
@Override
public void setParameter(PreparedStatement ps, int arg1, String str, JdbcType jdbcType) throws SQLException { }


}

猜你喜欢

转载自blog.csdn.net/x_xian_/article/details/77840306