시행착오

Mybatis 동적 쿼리 시 null처리 때문에 난감할 때

쿵도리 2019. 1. 23. 16:34


문제 : 

update TB_RECIPE

set 

<if test="recipeAmt != null and recipeAmt != ''">

recipe_amt = #{recipeAmt}

</if>

 

<if test="note == null or note ==''">

note = ''

</if>


이거 너무 짜증나고 곤란했는데

jdbcType을 명시해주면 손쉽게 목표를 이룰 수 있다.

note에 값이 있으면 그 값을 사용하고, 공백이거나 null이면 자동으로 null로 넣어준다. 



/*  RecipeDAO.updateRecipe 레시피 정보 수정  */

update TB_RECIPE

set note = #{note, jdbcType=VARCHAR}  

<if test="recipeAmt != null and recipeAmt != ''">

, recipe_amt = #{recipeAmt}

</if>

where recipe_seq = #{id}

limit 1