1. 계좌 목록 쿼리 확인 및 생성  
2. AccountRepository 코드 추가 
3. AccountService 코드 추가 
4. AccountController 코드 추가 
5. account/list.jsp 코드 수정

account.xml 파일 쿼리 확인 및 추가

<select id="findByUserId" resultType="com.tenco.bank.repository.model.Account">
	select * from account_tb where user_id = #{userId} 
</select>

AccountRepository 코드 추가

package com.tenco.bank.repository.interfaces;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.tenco.bank.repository.model.Account;

@Mapper // mybatis 연결 처리 
public interface AccountRepository {
	
	public int insert(Account account); 
	public int updateById(Account account); 
	public int deleteById(int id); 
	
	public List<Account> findAll();   
	public Account findById(int id);
	// 코드 추가 
	public List<Account> findByUserId(Integer userId);
}

AccountService 코드 추가

// 계좌 목록 보기 기능
@Transactional
public List<Account> readAccountList(Integer userId) {
	List<Account> list = accountRepository.findByUserId(userId);
	return list; 
}

AccountController 코드 추가

/**
 * 계좌 목록 페이지 
 * @return 목록 페이지 이동
 */
@GetMapping({"/list", "/"})
public String list(Model model) {
	
	//인증검사 처리
	User principal = (User)session.getAttribute(Define.PRINCIPAL);
	if(principal == null) {
		throw new UnAuthorizedException("로그인 먼저 해주세요", HttpStatus.UNAUTHORIZED);
	}
	
	// View 화면으로 데이터를 내려 주는 기술 
	// Model 과 ModelAndView 
	List<Account> accountList = accountService.readAccountList(principal.getId());
	if(accountList.isEmpty()) {
		model.addAttribute("accountList", null);
	} else {
		model.addAttribute("accountList", accountList);
	}
	
	return "/account/list";
}

account/list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/view/layout/header.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<div class="col-sm-8">
	<h2>나의 계좌 목록 </h2>
	<h5>어서오세요 환영합니다</h5>
	<div class="bg-light p-md-5 h-75">
	<c:choose>
		<c:when test="${accountList != null}">
			<table class="table">
			<thead>
				<tr>
					<th>계좌 번호</th>
					<th>잔액</th>
				</tr>
			</thead>
			<tbody>
				<c:forEach var="account" items="${accountList}">
				<tr>
					<td>${account.number}</td>
					<td>${account.balance}</td>
				</tr>
				</c:forEach>
			</tbody>
		</table>
		</c:when>
		<c:otherwise>
			<p>아직 생성된 계좌가 없습니다</p>
		</c:otherwise>
	</c:choose>
	</div>
	<br>
</div>

<%@ include file="/WEB-INF/view/layout/footer.jsp"%>

결과 화면

+ Recent posts