mybatis动态sql更新失效的问题

场景

更新表中某条数据时,把其他数据也更新了!

还原

调接口:根据id更新订单状态,入参

{
    "id": 2,
    "orderStatus": 2
}

实体:

private Long id;
private
int orderType; private int price;//单位:分 private int status;

动态sql(其中trim标签可换为set)

    <update id="updateOrderById" parameterType="com.data.Entity.Order">
        UPDATE order
        <trim prefix="set" suffixOverrides=",">
            <if test="orderType != null">
                order_type = #{orderType,jdbcType=TINYINT},
            </if>
            <if test="price != null">
                price = #{price ,jdbcType=INTEGER},
            </if>
            <if test="orderStatus != null">
                order_status = #{orderStatus,jdbcType=TINYINT},
            </if>
        </trim>
        WHERE id=#{id}
    </update>

问题

这里的实体,由于用的基本类型,而入参没有传orderType和price,导致数据初始化为0,更新的动态sql失效,orderType != null,不管用
最终:导致数据更新为 orderType=0,price=0,orderStatus=2

解决

实体请用,包装类型,即int 换为Integer即可避免此类情况

 

posted @ 2021-12-09 17:12  白玉神驹  阅读(539)  评论(0)    收藏  举报