解决mybatis映射对象中null值赋默认值的问题

mybatis-conf.xml配置文件中添加如下配置


    
    



对应的handler实体类

@MappedJdbcTypes(value = JdbcType.VARCHAR)
public class EmptyStringIfNull implements TypeHandler {
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {

    }

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

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

    @Override
    public Object getResult(CallableStatement cs, int i) throws SQLException {
        return (cs.getString(i) == null) ? "" : cs.getString(i);
    }
}

如果是null给赋值上空串。当然也可以进行一些别的操作。

你可能感兴趣的:(日常工作,mybatis,java,spring)