사용예시
- 한 사람이 여러 카드 가지고 있을 경우(1:N)
- 한 사람이 이력서 여러개 있을 경우 (1:N)
자바빈 클래스 생성
package kr.or.ddit.vo;
import java.util.ArrayList;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import lombok.Data;
//POHO가 약해짐
@Data
public class MemberVO {
private Address address; //1:1 관계
private List<Card> cardList; //MEMBER : CARD = 1: N 관계
}
Create class 'Address' 클릭
package kr.or.ddit.vo;
import lombok.Data;
@Data
public class Address {
private String postCode; //우편번호
private String location; // 주소
}
jsp
input 형식
<form action="/registerUserId" method="post">
<p>
postCode :<input type="text" name="postCode">
<br/>
location :<input type="text" name="location">
</p>
<p>
postCode :<input type="text" name="address.postCode">
<br/>
location :<input type="text" name="address.location">
</p>
<p><input type="submit" value="register01"/></p>
</form>
vo 값 넣기
association 1:1 관계
만약 기본키 null 일 경우
address{userId = null,postCode =54321,location = 대전서구 321}
1. memberVO 안에 address 프로퍼티 출력하기
Address address = member.getAddress();
2. address userId 값 넣기
address.setUserId(member.getUserId());
collection 1:N관계
1. memberVO 안에 CardList 출력
List<Card> cardList =member.getCardList();
List<Card> cardList | userId | no | validMonth | |
Card | null | 123456 | 05/24 | |
Card | null | 1111 | 06/24 |
2. for문 이용
cardList 있는 Card 하나씩 꺼내서 dao 보냄
for(Card card : cardList) {
card.setUserId(member.getUserId());
result +=this.memberDao.insertCard(card);
}
@Override
public int registerUserId(MemberVO member) {
int result = this.memberDao.registerUserId(member);
//2)Address
Address address = member.getAddress();
address.setUserId(member.getUserId());
result += this.memberDao.insertAddress(address);
// 3)CARD
List<Card> cardList =member.getCardList();
for(Card card : cardList) {
card.setUserId(member.getUserId());
result +=this.memberDao.insertCard(card);
}
return result;
}
member
association 1:1 관계
address=
Address(userId=null, postCode=080908, location=seoul)
값 접근 : 자바빈객체.중첩된자바빈객체.프로퍼티
예시)member.address.postCode
collection 1:N관계
cardList=[
Card(userId=leejj, no=1111-1111-1111-1111, validMonth=29/01/31),
Card(userId=leejj, no=2222-2222-2222-2222, validMonth=29/3/30)
]
값 접근 : 자바빈객체.중첩된자바빈객체[0].프로퍼티
예시)member.credList[0].no ⇒ 1111-1111-1111-1111
예시
read.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<h3>Result05</h3>
<p>${member}</p>
<!--
MEMBER: ADDRESS = 1:1 (association)
-->
<p>member.address.postCode
: ${member.address.postCode}
</p>
<p>member.address.location
: ${member.address.location}
</p>
<hr/>
<!-- List<Card> cardList -->
<h4>member.cardList</h4>
<c:forEach var="card" items="${member.cardList}" varStatus="stat">
<p>${card.no} : ${card.validMonth}</p>
</c:forEach>
controller.java
@GetMapping("/read05")
public String read05(Model model) {
MemberVO member = new MemberVO();
Address address = new Address();
address.setPostCode("080908");
address.setLocation("seoul");
member.setAddress(address);
List<Card> cardList = new ArrayList<Card>();
Card card1 = new Card();
card1.setNo("123456");
card1.setValidMonth("05/24");
cardList.add(card1);
Card card2 = new Card();
card2.setNo("1111");
card2.setValidMonth("06/24");
cardList.add(card2);
member.setCardList(cardList);
Calendar cal = Calendar.getInstance();
cal.set(2000,05,07);
member.setDateOfBirth(cal.getTime());
model.addAttribute("member",member);
return "read05";
}
중첩된 자바빈 사용한 쿼리문
쿼리문
--LPROD : PRODUCT = 1 : N
SELECT A.LPROD_ID, A.LPROD_GU, A.LPROD_NM
,B.PRODUCT_ID, B.PNAME, B.UNIT_PRICE, B.DESCRIPTION, B.MANUFACTURER, B.CATEGORY, B.UNITS_IN_STOCK, B.CONDITION, B.FILENAME, B.QUANTITY
FROM LPROD A LEFT OUTER JOIN PRODUCT B ON(A.LPROD_GU=SUBSTR(B.PRODUCT_ID,1,4))
WHERE A.LPROD_GU = 'P101'
;
⭐기본키 == 외래키 이므로 외래키 컬럼명 제외
만약 사용하면 컬럼명_1 생성
SQL.xml
join 사용 했기 때문에 resultType 대신 resultMap 사용
<select id="detail" parameterType="lprodVO" resultMap="lprodMap">
SELECT A.LPROD_ID, A.LPROD_GU, A.LPROD_NM
,B.PRODUCT_ID, B.PNAME, B.UNIT_PRICE, B.DESCRIPTION, B.MANUFACTURER, B.CATEGORY, B.UNITS_IN_STOCK, B.CONDITION, B.FILENAME, B.QUANTITY
FROM LPROD A LEFT OUTER JOIN PRODUCT B ON(A.LPROD_GU=SUBSTR(B.PRODUCT_ID,1,4))
WHERE A.LPROD_GU = #{lprodGu}
</select>
resultMap 설정
https://lavender1122.tistory.com/130
<!-- LPORD :1 -->
<resultMap type="lprodVO" id="lprodMap">
<result property="lprodId" column="LPROD_ID"/>
<result property="lprodGu" column="LPROD_GU"/>
<result property="lprodNm" column="LPROD_NM"/>
<collection property="productVOList" resultMap="productMap"></collection>
</resultMap>
<!-- PRODUCT : N -->
<resultMap type="productVO" id="productMap">
<result property="productId" column="PRODUCT_ID"/>
<result property="pname" column="PNAME"/>
<result property="unitPrice" column="UNIT_PRICE"/>
<result property="description" column="DESCRIPTION"/>
<result property="manufacturer" column="MANUFACTURER"/>
<result property="category" column="CATEGORY"/>
<result property="unitsInStock" column="UNITS_IN_STOCK"/>
<result property="condition" column="CONDITION"/>
<result property="filename" column="FILENAME"/>
<result property="quantity" column="QUANTITY"/>
</resultMap>
resultMap id ⇒ select resultMap 연결
resultMap type ⇒ 해당 vo 연결
<colloection> 1:N관계 일때 사용
만약, 1:1 일 경우 <association> 사용
< colloection property> ⇒ vo 변수 작성