LANGUAGE/SPRING 2017. 3. 27. 11:49

MyBatis (마이바티스)

MyBatis를 사용하다보면, "여기서 그 자바 함수를 사용하면 딱인데" 라고 생각이 드는 순간이 있다.

다행히도 static으로 선언된 함수(method)를 사용할 수 있게 할 수 있다.

static 함수사용하기

static 함수가 있는 Class의 classpath@이와 함께 적고, 사용할 함수명을 적는다.

예)

@foo.Bar@myMethod()
  • IF문에서 사용예
<if test="@com.myproject.Util@isNice(#{parameter})">       
  AND SONG_CODE = #{songCode}
</if>
  • Parameter로 사용예
<insert id="insertFooBar">
INSERT INTO FOO_TABLE(
  BAR_COL
)VALUE(
  ${@foo.Bar@myMethod()}
)
</insert>

참고

Calling a java static method as parameter in MyBatis 3.1 SQL Query: http://mybatis-user.963551.n3.nabble.com/Calling-a-java-static-method-as-parameter-in-MyBatis-3-1-SQL-Query-td4028393.html