[mybatis] collection 사용법

Yuni·2023년 3월 8일
0

mybatis

목록 보기
3/8

< join을 mybatis에서 어케 불러옴...? >

DB에서 join을 한 리스트를 mybatis를 통해서 불러와야하는데
할 줄 몰라서 구글 신을 뒤지다가 좋은 참고 글을 발견해서 성공!
나중에 또 찾아볼 나를 위해 기록해본다


일단 나는 많은 vo클래스들을 가지고 있는데
이 중에서 UserVO랑 PartVO를 함께 가지고 와야했다

join를 작성을 했다

xml

	<select id="userData" parameterType="String" resultMap="UserResult">
		<![CDATA[
		SELECT 
		 u.user_id
		, u.user_name
		, u.user_phone
		, u.user_email
		, u.user_signdate
		, p.part_name
		FROM 
		tbl_user u
		LEFT JOIN 
		tbl_part p 
		ON 
		p.part_id = (
		SELECT part_id
		FROM tbl_part AS p2
		WHERE p2.part_id = u.user_part
		)
		where user_authority > 1
		ORDER BY u.user_name desc
		]]>
	</select>

mybaris에 xml 파일에 이렇게 작성을 해두겠다

< 권한이 1 이하일때는 가입신청을 한 상태라 보이면 안되고 이름 내림차순으로 >
오름차순(ASC) : 작은 값부터 큰 값 쪽으로의 순서 ex)1, 2, 3, 4, n, n+1...
내림차순(DESC) : 큰 값부터 작은 값 쪽으로의 순서 ex)5, 4, 3, 2, 1

UserVO

package com.sck.alice.domain;

public class UserVO {
	private String id; // 아이디
	private String pwd; // 비밀번호
	private String name; // 이름
	private int part; // 부서
	private String phone; // 전화번호
	private String email; // 이메일
	private int autority; // 권한
	private String createddate; // 가입신청일
	private String signdate; // 가입일
	private int locked; // 5회 이상 오류
	
	private PartVO partvo;
	
	public String getId() 							{return id;}
	public void setId(String id)					{this.id = id;}
	
	public String getPwd()							{return pwd;}
	public void setPwd(String pwd)					{this.pwd = pwd;}
	
	public String getName()							{return name;}
	public void setName(String name)				{this.name = name;}
	
	public int getPart()							{return part;}
	public void setPart(int part)					{this.part = part;}
	
	public String getPhone()						{return phone;}
	public void setPhone(String phone)				{this.phone = phone;}
	
	public String getEmail() 						{return email;}
	public void setEmail(String email) 				{this.email = email;}
	
	public int getAutority() 						{return autority;}
	public void setAutority(int autority) 			{this.autority = autority;}
	
	public String getCreateddate()					{return createddate;}
	public void setCreateddate(String createddate)	{this.createddate = createddate;}
	
	public String getSigndate() 					{return signdate;}
	public void setSigndate(String signdate) 		{this.signdate = signdate;}
	
	public int getLocked()							{return locked;}
	public void setLocked(int locked)				{this.locked = locked;}
	
	@Override
	public String toString() {
		return "UserVO [id=" + id + ", pwd=" + pwd + ", name=" + name + ", part=" + part + ", phone=" + phone
				+ ", email=" + email + ", autority=" + autority + ", createddate=" + createddate + ", signdate="
				+ signdate + ", locked=" + locked + ", partvo=" + partvo + "]";
	}
	public PartVO getPartvo()							{return partvo;}	
	public void setPartvo(PartVO partvo) 				{this.partvo = partvo;}
}

UserVO 안에 PartVO 객체참조변수를 넣어주고 getter, setter, toString 까지 만들어준다.

xml

<resultMap type="com.sck.alice.domain.PartVO" id="PartResult">
		<result column="part_id" property="part_id" />
		<result column="part_name" property="part_name" />
	</resultMap>

	<resultMap type="com.sck.alice.domain.UserVO" id="UserResult">
		<result column="user_id" property="id" />
		<result column="user_pwd" property="pwd" />
		<result column="user_name" property="name" />
		<result column="user_part" property="part" />
		<result column="user_phone" property="phone" />
		<result column="user_email" property="email" />
		<result column="user_authority" property="autority" />
		<result column="user_createddate" property="createddate" />
		<result column="user_signdate" property="signdate" />
		<result column="user_locked" property="locked" />
		<collection property="partvo" resultMap="PartResult"/> // 요기 작성!
	</resultMap>

collection 부분을 작성해준다
property = 아까 UserVO에서 썼던 객체참조변수 이름
resultMap = 내가 사용할 resultMap의 id 이름을 넣어준다
(나중에 헷갈릴 수도 있을꺼같아 이름을 바꿔가며 썼다)

jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<table class="table_01" style="text-align: center;text-align: center; margin: 50px auto 0 auto;">
<thead>
	<tr>
	<th><div class="th-text">No</div></th>
	<th><div class="th-text">부서</div></th>
	<th><div class="th-text">아이디</div></th>
	<th><div class="th-text">이름</div></th>
	<th><div class="th-text">전화번호</div></th>
	<th><div class="th-text">이메일</div></th>
	<th><div class="th-text">가입시기</div></th>

	</tr>
</thead>
<tbody>
	<c:forEach items="${list}" var="list"  varStatus="status" >
		<tr>
			<td><c:out value="${status.count}"/></td>
			<td>${list.partvo.part_name}</td>
			<td>${list.id}</td>
			<td>${list.name}</td>
			<td>${list.phone}</td>
			<td>${list.email}</td>
			<td>${list.signdate}</td>
		</tr>
	</c:forEach>
</tbody>
</table>

JSTL 반복문을 넣어 만들어준다

참고
https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=tkdguq9369&logNo=221734475155

profile
backend developers

0개의 댓글