MyBatis 设置column的默认值

需求

ibatis改造为mybatis,因旧线存在nullValue="xxx"等默认值,而mybatis的resultMap没有这个属性

实现

寻找了很多方式
方法一(排除):编写工具,批量替换查询语句的字段结果值,根据nullValue判断替换sql语句。(已实现普通的sql语句,但是存在很多特殊的sql增加很多替换风险,如 *号,函数NVL,length等)
方法二:根据原文档找到Mybatis的一个特殊属性typeHandler="XXX",可以自行创建继承org.apache.ibatis.type.TypeHandler的类,将其getResult中判断null赋于默认值
如下举例三种:

mybatis.xml
<resultMap id="xxx" type="autoTrade">
    <result column="endperiod" property="endPeriod" jdbcType="DECIMAL" typeHandler="com.erayt.xpad.tools.MybatisEmptyIntegerToZero"/>
    <result column="endperiod" property="endPeriod" jdbcType="DECIMAL" typeHandler="com.erayt.xpad.tools.MybatisEmptyBigDecimalToZero"/>
    <result column="endperiod" property="endPeriod" jdbcType="DECIMAL" typeHandler="com.erayt.xpad.tools.MybatisEmptyIntegerToOne"/>
</<resultMap>
MybatisEmptyIntegerToZero.java
public class MybatisEmptyIntegerToZero implements TypeHandler<Integer>{

    @Override
    public void setParameter(PreparedStatement ps, int i, Integer parameter, JdbcType jdbcType) throws SQLException {
        // TODO Auto-generated method stub
    }
    
    @Override
    public Integer getResult(ResultSet rs, String columnName) throws SQLException {
        return (rs.getInt(columnName)==0) ? 0 : rs.getInt(columnName);
    }

    @Override
    public Integer getResult(ResultSet rs, int columnIndex) throws SQLException {
        return (rs.getInt(columnIndex) ==0) ? 0 : rs.getInt(columnIndex);
    }

    @Override
    public Integer getResult(CallableStatement cs, int columnIndex) throws SQLException {
        return (cs.getInt(columnIndex) ==0) ? 0 : cs.getInt(columnIndex);
    }
}
MybatisEmptyBigDecimalToZero.java
public class MybatisEmptyBigDecimalToZero implements TypeHandler<BigDecimal>{

    @Override
    public void setParameter(PreparedStatement ps, int i, BigDecimal parameter, JdbcType jdbcType) throws SQLException {
        // TODO Auto-generated method stub
    }
    
    @Override
    public BigDecimal getResult(ResultSet rs, String columnName) throws SQLException {
        return (rs.getBigDecimal(columnName)==null) ? new BigDecimal(0) : rs.getBigDecimal(columnName);
    }

    @Override
    public BigDecimal getResult(ResultSet rs, int columnIndex) throws SQLException {
        return (rs.getBigDecimal(columnIndex) ==null) ? new BigDecimal(0) : rs.getBigDecimal(columnIndex);
    }

    @Override
    public BigDecimal getResult(CallableStatement cs, int columnIndex) throws SQLException {
        return (cs.getBigDecimal(columnIndex) ==null) ? new BigDecimal(0) : cs.getBigDecimal(columnIndex);
    }
}
MybatisEmptyIntegerToOne.java
public class MybatisEmptyIntegerToOne implements TypeHandler<Integer>{

    @Override
    public void setParameter(PreparedStatement ps, int i, Integer parameter, JdbcType jdbcType) throws SQLException {
        // TODO Auto-generated method stub
    }
    
    @Override
    public Integer getResult(ResultSet rs, String columnName) throws SQLException {
        return (rs.getString(columnName)==null) ? 1 : rs.getInt(columnName);
    }

    @Override
    public Integer getResult(ResultSet rs, int columnIndex) throws SQLException {
        return (rs.getString(columnIndex) ==null) ? 1 : rs.getInt(columnIndex);
    }

    @Override
    public Integer getResult(CallableStatement cs, int columnIndex) throws SQLException {
        return (cs.getString(columnIndex) ==null) ? 1 : cs.getInt(columnIndex);
    }
}

为此不创建过多的类不利于管理,一些特殊的默认值如数值200、字符串‘xxx’等,调整查询语句sql的值,用IFNULL赋予默认值。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容