반응형
이중 foreach insert 작성 예제
자료구조
// getter, setter 생략
class User {
private int userNum;
private List<Order> orderList;
}
class Order {
private int orderNum;
private List<Item> itemList;
}
class Item {
private int itemNum;
private String itemName;
private String itemPrice;
}
Mapper.xml
<resultMap id="userMap" type=" {User class 위치 ex) com.example.vo.User} ">
<result property="userNum" column=" {데이터베이스 userNum 컬럼 ex) USER_NUM} " />
<collection property="orderList" javaType="java.util.ArrayList" resultMap="orderMap" />
<resultMap>
<resultMap id="orderMap" type=" {Order class 위치 ex) com.example.vo.Order} ">
<result property="orderNum" column=" {데이터베이스 orderNum 컬럼 ex) ORDER_NUM} " />
<collection property="itemList" javaType="java.util.ArrayList" resultMap="itemMap" />
<resultMap>
<resultMap id="itemMap" type=" {Item class 위치 ex) com.example.vo.Item} ">
<result property="itemNum" column=" {데이터베이스 itemNum 컬럼 ex) ITEM_NUM} " />
<result property="itemName" column= "ITEM_NAME" />
<result property="itemPrice" column= "ITEM_PRICE" />
<resultMap>
<insert id=" {Mapper 연결 메소드 이름 ex) insertUser} " parameterType=" {User class 위치 ex) com.example.vo.User}">
INSERT INTO
ITEM ( ITEM_NAME, ITEM_PRICE )
VALUES
<foreach collection="orderList" item="order" separator=",">
<foreach collection="itemList" item="item" separator=",">
( #{item.itemName}, #{item.itemPrice} )
</foreach>
</foreach>
</insert>
반응형