八月【sql】

今天遇到一個bug

原代碼

    <update id="updateUserInfo" parameterType="java.util.Map">
        UPDATE t_fsans_record T
        SET T.bind_status = #{binding},
        T.bind_time = sysdate(),
        T.phone = #{phone},
        T.bind_form = #{bind_form},
        <if test="referee_phone !=null referee_phone and !=''">
            T.referee_phone = #{referee_phone},
        </if>
        <if test="county !=null and county !=''">
            T.county = #{county}
        </if>
        WHERE T.openid=#{openid} and T.city=#{cityCode}
        and ( T.bind_status <![CDATA[<>]]> 'binding' or T.bind_status is null)
    </update>

如果 if 標籤爲空,就不會插入,所以

T.bind_form = #{bind_form},

就會多出一個逗號,所以sql執行不通過

修改

    <update id="updateUserInfo" parameterType="java.util.Map">
        UPDATE t_fsans_record T
        SET T.bind_status = #{binding},
        T.bind_time = sysdate(),
        T.phone = #{phone},
        T.bind_form = #{bind_form},
        T.referee_phone = #{referee_phone},
        T.county = #{county}
        
        WHERE T.openid=#{openid} and T.city=#{cityCode}
        and ( T.bind_status <![CDATA[<>]]> 'binding' or T.bind_status is null)
    </update>

有 標籤就可以

  • 標籤能夠自動去掉最後一個逗號

    <update id="updateAuthor" parameterType="com.xwtec.sucai.bean.AuthorBean">
		update t_author
		<set>
			<if test="password!=null">
				password=#{password},
			</if>
			<if test="sex!=null">
				sex=#{sex},
			</if>
			<if test="professionalName!=null">
				professionalName=#{professionalName},
			</if>
		</set>
		<where>
			authorId= #{authorId}
		</where>
    </update>

  • 或者
 <update id="updateUserInfo" parameterType="java.util.Map">
        UPDATE t_fsans_record T
        SET T.bind_status = #{binding},
        T.bind_time = sysdate(),
        T.phone = #{phone},
        T.bind_form = #{bind_form}
        <if test="referee_phone !=null referee_phone and !=''">
            ,T.referee_phone = #{referee_phone}
        </if>
        <if test="county !=null and county !=''">
            ,T.county = #{county}
        </if>
        WHERE T.openid=#{openid} and T.city=#{cityCode}
        and ( T.bind_status <![CDATA[<>]]> 'binding' or T.bind_status is null)
    </update>

T.bind_form = #{bind_form}

後面的逗號去掉,放到標籤下。

好TM坑啊!!!!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章