DB Story

[ Mybatis ] foreach

WhiteDuck 2016. 9. 8. 12:55

foreach 문은 list나 array만 된다.


* ArrayList의 예제


in java

ArrayList<String> myList=  new ArrayList<String>();



in xml


<select id = "id" parameterType="String", resultType="com.whiteduck.test.vo.TextVO">

select * from test.Text

where id in

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

#{item.value}

</foreach>

</select>


* 설명 

collection = 전달받은 인자값
item   = 전달받은 인자값을 다른이름으로 대체 
open 해당 구문이 시작할 때
close 해당구문이 끝날 때
separator  한번 이상 반복할때 반복되는 사이에  해당 문을 넣어줌


참고 : http://pcdate.blogspot.kr/2013/05/mybatis-foreach.html

반응형