MyBatis
mybatis update 컴마
xemaker
2020. 5. 14. 17:25
▶ mybatis update 사용시 모두 <if test="">를 사용해야할 경우
<update id="updateExam" parameterType="VO">
UPDATE examTable
<trim prefix="SET" suffixOverrides=",">
<if test="exam1 != null">exam1 = #{exam1},</if>
<if test="exam2 != null">exam2= #{exam2},</if>
</trim>
WHERE
idx = #{idx}
</update>
mybatis에서 update 쿼리문을 사용하다보면 위처럼 조건문만 사용하게 되는 경우가 생긴다.
그때 콤마(,) 를 어떻게 사용해야 sql문 error가 발생하지 않을지 고민하게 된다.
그럴때는 trim 이라는것을 사용한다.
이것을 사용하면 맨 앞에 SET을 붙혀주고, if문 안의 콤마(,)를 지워준다.
출처: https://devfunny.tistory.com/11