mybatis动态sql去掉,

动态sql

 <sql id="finalExamAttribute">
        <if test="course!=null">
            courseId = #{course.id}
        </if>
        <if test="tclass!=null">
            , classId = #{tclass.id}
        </if>
        <if test="teacher!=null">
            , tid=#{teacher.id}
        </if>
        <if test="score!=null">
            , score=#{score}
        </if>
        <if test="signer!=null">
            , signer=#{signer}
        </if>
    </sql>

修改语句


 <update id="updateFinalExamList">
        UPDATE finalExamList SET
        <include refid="finalExamAttribute" />
        WHERE id=#{id}
    </update>

执行的sql
图片说明
这是什么情况?
会不会是关联查询逗号就去不掉??
怎么解决???

mybatis的动态sql语句中的逗号(,)是放在语句后面,而不是前面的,像这样:OPTIME = #{optime,jdbcType=TIMESTAMP},
这样mybatis在执行更新语句时会自动将末尾的逗号去掉,同时保证即使该对象的属性值全都为空也不会出错;
当然如果你非要把逗号放在前面就需要在finalExamAttribute的最前面加上 id=#{id},不需要条件判断


, signer=#{signer}

原因在于你的条件里面本来就有都好,最好的是在finalExamAttribute里面加上 id=#{id}

一般如果前面属性为空,他应该会把头部的逗号去掉;不过你这个处理方法也不错

你where的位置问题,你抽取的条件都没放到where下面。所以报错。




and id = #{id}


and courseId = #{course.id}


and classId = #{tclass.id}


and tid=#{teacher.id}


and score=#{score}


and signer=#{signer}


图片说明手写代码显示不全 给你贴个图片

mybatis本身就有属性可以解决这个问题,不需要每个都加逗号,mybatis会自行进行判断。

我是这样解决的!!!自己保存!

<sql id="finalExamAttribute">
        <trim suffixOverrides=",">
        <if test="course!=null">
            courseId = #{course.id} ,
        </if>
        <if test="tclass!=null">
            classId = #{tclass.id} ,
        </if>
        <if test="teacher!=null">
            tid=#{teacher.id} ,
        </if>
        <if test="score!=null">
            score=#{score} ,
        </if>
        <if test="signer!=null">
            signer=#{signer}
        </if>
        </trim>
    </sql>