본문 바로가기
Java, Spring

[Java, Mybatis] foreach 문법

by 혀나Lee 2017. 3. 20.

<foreach collection="전달받은 인자값" item="전달받은 인자값을 대체할 이름" open="(" close=")" separator=",">

...

</foreach>


  • collection: 전달받은 인자값
  • item: 전달받은 인자값을 대체할 이름
  • open: 해당 구문 시작 예, (
  • close: 해당 구문 종료 예, )
  • separator: 구분값

예시

URI

GET /test?testList=1,2,3,4,5

SQL

SELECT * FROM t_test WHERE id in (1,2,3,4,5)

in JAVA

...

String[] tmpList = param.get("testList").toString().split(",");

List<Integer> testList = new ArrayList<Integer>();

for (String i : tmpList) {

testList.add(Integer.parseInt(i));

}

param.put("testList", testList);

...

in mapper.xml

...

<if test="testList != null">

AND id in 

<foreach collection="testList" item="item" index="index" separator="," open="(" close=")">

#{item.value}

</foreach>

</if>

...


댓글