关于数据库中decimal类型数据没有默认值时的数据插入问题

当数据库中decimal类型字段无默认值,Mybatis在接收到0值时会将其解析为null导致插入错误。解决方案包括:1.为字段设置默认值;2.修改Mybatis映射文件,移除Integer和BigDecimal的空值判断条件。
<insert id="insertActivity" parameterType="TDeptActivityResultDto" useGeneratedKeys="true" keyProperty="id">
        insert into t_dept_activity(
        <if test="id != null and id != 0">ID,</if>
        <if test="deptId != null and deptId != 0">DEPT_ID,</if>
        <if test="activityName != null and activityName != ''">ACTIVITY_NAME,</if>
        <if test="activityDescription != null and activityDescription != ''">ACTIVITY_DESCRIPTION,</if>
        <if test="activityStartTime != null">ACTIVITY_START_TIME,</if>
        <if test="activityEndTime != null">ACTIVITY_END_TIME,</if>
        <if test="matchRuleType != null and matchRuleType != ''">MATCH_RULE_TYPE,</if>
        <if test="victoryCounts != null and victoryCounts != ''">VICTORY_COUNTS,</if>
        <if test="goodsCounts != null and goodsCounts != ''">GOODS_COUNTS,</if>
        <if test="goodsId != null and goodsId != ''">GOODS_ID,</if>
        <if test="exchangeDeadline != null">EXCHANGE_DEADLINE,</if>
        <if test="entryFee != null and entryFee != ''">ENTRY_FEE,</if>
        <if test="activityStatus != null and activityStatus != ''">ACTIVITY_STATUS,</if>
        <if test="createBy != null and createBy != ''">CREATE_BY,</if>
        <if test="remark != null and remark != ''">remark,</if>
        CREATE_TIME,
        DELETE_FLG
        )values(
        <if test="id != null and id != 0">#{id},</if>
        <if test="deptId != null and deptId != 0">#{deptId},</if>
        <if test="activityName != null and activityName != ''">#{activityName},</if>
        <if test="activityDescription != null and activityDescription != ''">#{activityDescription},</if>
        <if test="activityStartTime != null">#{activityStartTime},</if>
        <if test="activityEndTime != null">#{activityEndTime},</if>
        <if test="matchRuleType != null and matchRuleType != ''">#{matchRuleType},</if>
        <if test="victoryCounts != null and victoryCounts != ''">#{victoryCounts},</if>
        <if test="goodsCounts != null and goodsCounts != ''">#{goodsCounts},</if>
        <if test="goodsId != null and goodsId != ''">#{goodsId},</if>
        <if test="exchangeDeadline != null">#{exchangeDeadline},</if>
        <if test="entryFee != null and entryFee != ''">#{entryFee},</if>
        <if test="activityStatus != null and activityStatus != ''">#{activityStatus},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        sysdate(),0
        )
    </insert>

       先贴代码,此时因为数据库中 ENTRY_FEE 字段没有默认值,当页面传入0时,会报"entryFee  don't have a default value"的错误,而直接运行插入sql是可以正常插入的,这是因为mybatis在存储Integer、Bigdecimal等数据类型时,会将0解析成null,结果在存储的时候就报错了。

      有两种解决方法:

             1. 给数据库的integer和decimal数据类型的字段设置默认值。

             2. 去掉entryFee(integer/Bigdecimal)的判空条件,即<if test="entryFee != null and entryFee != ''">#{entryFee},</if>                     改为<if test="entryFee != null">#{entryFee},</if>即可。

其实仔细查看代码会发现,在Bigdecimal类型数据做插入操作时,页面是0,它的inval的值是null。做查询时,DB中的值为0时,java代码中的intval的值为null。 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值