mybatis sql语句怎么实现只更新有值的数据,不更新为0的数据

新手上路,请多包涵

尝试使用mybatis动态sql出错,参考的网上的写法

需要实现效果就是当传递属性值为0时,数据库不更新相关值,!=0才更新

如下图,只更新tuesD

clipboard.png

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)
<update id="updateClassTable" parameterType="com.jkb.entity.ClassTable">

    update ClassTable

    <trim prefix="set" suffixOverrides=",">>
        <if test="monD != 0">
            monD=#{monD}
        </if>
        <if test="tuesD != 0">
            tuesD=#{tuesD}
        </if>
        <if test="wedD != 0">
            wedD=#{wedD}
        </if>
        <if test="thursD != 0">
            thursD=#{thursD}
        </if>
        <if test="friD != 0">
            friD=#{friD}
        </if>
        <if test="satD != 0">
            satD=#{satD}
        </if>
        <if test="sunD != 0">
            sunD=#{sunD}
        </if>
    </trim>
   /* set monD=#{monD},tuesD=#{tuesD},wedD=#{wedD},thursD=#{thursD},friD=#{friD},satD=#{satD},sunD=#{sunD}
    */
    where Sno=#{Sno} and term=#{term} and classTime=#{classTime}
</update>

错误信息

clipboard.png

阅读 3.6k
1 个回答
✓ 已被采纳新手上路,请多包涵

解决了,太傻了,。。。多写了一个>,代码写晕了
<trim prefix="set" suffixOverrides=",">

        <if test="monD != 0">
            monD=#{monD},
        </if>
        <if test="tuesD != 0">
            tuesD=#{tuesD},
        </if>
        <if test="wedD != 0">
            wedD=#{wedD},
        </if>
        <if test="thursD != 0">
            thursD=#{thursD},
        </if>
        <if test="friD != 0">
            friD=#{friD},
        </if>
        <if test="satD != 0">
            satD=#{satD},
        </if>
        <if test="sunD != 0">
            sunD=#{sunD},
        </if>
    </trim>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题