본문 바로가기
JAVA

SQL 동적 쿼리 foreach문 문법 정리

by soy미니 2023. 9. 21.

 

List<InsertDataDetail> data = new ArrayList<>();
data.put('data', arr);
MainMapper.insertData(data);

 

<insert id="insertData">
 INSERT INTO ${session.tableName}.personDetail(P_ID, P_NAME, P_AGE) VALUES
     <foreach collection="data" item="person" separator="," index="index" open="(" close=")">
     #{person.p_id}, #{person.p_name}, #{person.p_age}
     </foreach>
</insert>

 

 

SQL foreach 문

태그 기능
collection 전달받은 인자 (List or Array)
item 전달받은 인자 alias
separator 반복되는 사이에 출력할 문자열
index 반복되는 구문 번호 (0부터 순차 증가)
open 구문 시작될 때 삽입할 문자열
close 구문 종료될 때 삽입할 문자열

 

댓글