위로 아래

forEach문 태그

  1. collection : 전달 받은 인자
  2. item : 전달 받은 인자의 Alias
  3. open : 구문 시작 시 들어갈 문자열
  4. close : 구문 종료 시 들어갈 문자열
  5. separator : 매 반복 회차 종료 시 들어갈 문자열 (반복 구분자)
  6. index : 반복 횟수

 

예시

insert 문

<insert id="insertOrder" parameterType="list">
	
		<selectKey keyProperty="o_no" resultType="int" order="BEFORE">
			SELECT NVL(MAX(o_no),0)+1 AS o_no FROM ordert
		</selectKey>
		
		<foreach collection="list" item="odto" index="index" open="INSERT ALL " close="SELECT * FROM DUAL" separator=" ">
			INTO ordert(
				o_no
				,o_price
				,o_quantity
				,o_regdate
				,o_state
				,o_amount
				,m_id
				,d_no
			)
			VALUES(
				 #{o_no}
				,#{odto.o_price}
				,#{odto.o_quantity}
				,SYSDATE
				,'배송 준비'
				,#{odto.o_price}*#{odto.o_quantity}
				,#{odto.m_id}
				,#{odto.d_no}
			)
		</foreach>

 

 

update 문

<update id="updateStocks" parameterType="list">
	<foreach collection="list" item="ovo" index="index" separator=";" open="DECLARE BEGIN" close="; END;">
		UPDATE product
		SET stock = stock-#{ovo.quantity}
		<where>
			p_no = #{ovo.p_no}
		</where>
	</foreach>
</update>