mybatis:BigDecimal判定0值时问题

时间:2025-02-13 08:23:44

问题

在Mybatis中,遇到入参类型为BigDecimal时,不为0的情况下用!=''方法可以正常判断,

但值为0时使用 !=''判定会出现问题。

BigDecimal fine_cash=new BigDecimal("0");//违约金

String bill_;

("bill_id",bill_id);

("fine_cash",fine_cash);

(tmap);//执行mybatis的SQL语句

	<!-- 更新信息 -->
	<update  parameterType="">
	update t_month_bill
	    <set>
            less_cash=#{less_cash},
			<!-- 更新违约金 -->
			<if test="fine_cash !=null and fine_cash !=''">
				fine_cash=#{fine_cash},
				shou_cash=total_cash+#{fine_cash},
			</if>
		</set>
	where bill_id=#{bill_id};

 以上执行时,不会进入违约金字段的if代码段中。

解决方案

1.去除if代码段中的and fine_cash !=''判定,改为

<if test="fine_cash !=null ">

2.传入前先将fine_cash值转换为String类型

BigDecimal fine_cash=new BigDecimal("0");//违约金

("bill_id",bill_id);

("fine_cash",fine_cash.toString());

(tmap);//执行mybatis的SQL语句