Mybatis语法错误的一个坑

来源:互联网 发布:mac开盖自动开机 编辑:程序博客网 时间:2024/06/13 19:56

前言

        为什么说是一个坑呢?是因为这个错误实在是比较难出现,只有在特定的场合才会出现,是关于Mybatis语法错误的一个问题,说到底,其实就是一个小逗号的事情。

问题描述

        先来看一段sql:


<update id="updateByPrimaryKeySelective" parameterType="com.weimob.mengdian.promotion.dao.model.PromotionInfo">        update promotion_info set        <if test="title != null">            title = #{title,jdbcType=VARCHAR},        </if>        <if test="startTime != null">            start_time = #{startTime,jdbcType=TIMESTAMP},        </if>        <if test="endTime != null">            end_time = #{endTime,jdbcType=TIMESTAMP},        </if>        <if test="promotionType != null">            promotion_type = #{promotionType,jdbcType=INTEGER},        </if>        <if test="status != null">            status = #{status,jdbcType=INTEGER},        </if>        <if test="isAllGoods != null">            is_all_goods = #{isAllGoods,jdbcType=BIT},        </if>        <if test="version != null">            version = #{version,jdbcType=VARCHAR},        </if>        <if test="isDeleted != null">            is_deleted = #{isDeleted,jdbcType=BIT},        </if>        <if test="createUserId != null">            create_user_id = #{createUserId,jdbcType=BIGINT},        </if>        <if test="bannerImage != null">            banner_image = #{bannerImage,jdbcType=VARCHAR},        </if>        <if test="merchantId != null">            merchant_id = #{merchantId,jdbcType=BIGINT}        </if>        where id = #{id}        /*promotionInfo.updateByPrimaryKeySelective*/    </update>

        这一段sql乍一看是没有什么问题啊,但是,当一种特殊的情况就是当传入的最后一个字段为空的时候,就会出现一个问题,那就是前一个字段的逗号就会多出来。这时候自然而然就报出语法错误了。

解决方案

        解决的方案呢实际上也是非常简单,那就是保证最后一个字段一定不为空。

总结

        看来使用动态sql一定也要注意某些特殊的情况,否则害死自己了!还有排查错误必须要各种的推敲,我只能对Mybatis说一句,受教了!

0 0
原创粉丝点击