💡 학습목표
1. 계좌 목록 쿼리 확인 및 생성
2. AccountRepository 코드 추가
<select id="findByUserId" resultType="com.tencoding.bank.repository.model.Account">
select * from account_tb where user_id = #{userId}
</select>
package com.tencoding.bank.repository.interfaces;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.tencoding.bank.repository.model.Account;
@Mapper
public interface AccountRepository {
public int insert(Account acount);
public int updateById(Account acount);
public int deleteById(Integer id);
public List<Account> findAll();
public Account findById(Integer id);
public List<Account> findByUserId(Integer principalId);
public Account findByNumber(String number);
}
package com.tencoding.bank.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.tencoding.bank.dto.SaveFormDto;
import com.tencoding.bank.handler.exception.CustomRestfulException;
import com.tencoding.bank.repository.interfaces.AccountRepository;
import com.tencoding.bank.repository.model.Account;
@Service
public class AccountService {
@Autowired
private AccountRepository accountRepository;
@Transactional
public void creatAccount(SaveFormDto saveFormDto, Integer principalId) {
Account account = new Account();
account.setNumber(saveFormDto.getNumber());
account.setPassword(saveFormDto.getPassword());
account.setBalance(saveFormDto.getBalance());
account.setUserId(principalId);
int resultRowCount = accountRepository.insert(account);
if(resultRowCount != 1) {
throw new CustomRestfulException("계좌 생성 실패", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Transactional
public List<Account> readAccountList(Integer userId) {
List<Account> list = accountRepository.findByUserId(userId);
return list;
}
}
package com.tencoding.bank.controller;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.tencoding.bank.dto.SaveFormDto;
import com.tencoding.bank.handler.exception.CustomRestfulException;
import com.tencoding.bank.handler.exception.UnAuthorizedException;
import com.tencoding.bank.repository.model.Account;
import com.tencoding.bank.repository.model.User;
import com.tencoding.bank.service.AccountService;
import com.tencoding.bank.util.Define;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private HttpSession session;
@Autowired
private AccountService accountService;
@GetMapping("/list")
public String list(Model model) {
User user = (User)session.getAttribute(Define.PRINCIPAL);;
if(user == null) {
throw new UnAuthorizedException("로그인을 먼저 해주세요.", HttpStatus.UNAUTHORIZED);
}
List<Account> accountList = accountService.readAccountList(user.getId());
if(accountList.isEmpty()) {
model.addAttribute("accountList", null);
} else {
model.addAttribute("accountList", accountList);
}
return "account/list";
}
@GetMapping("/save")
public String save() {
User user = (User)session.getAttribute(Define.PRINCIPAL);;
if(user == null) {
throw new UnAuthorizedException("로그인을 먼저 해주세요.", HttpStatus.UNAUTHORIZED);
}
return "account/save";
}
@PostMapping("/save")
public String saveProc(SaveFormDto saveFormDto) {
User user = (User)session.getAttribute(Define.PRINCIPAL);;
if(user == null) {
throw new UnAuthorizedException("로그인을 먼저 해주세요.", HttpStatus.UNAUTHORIZED);
}
if(saveFormDto.getNumber() == null
|| saveFormDto.getNumber().isEmpty()) {
throw new CustomRestfulException("계좌번호를 입력해주세요.", HttpStatus.BAD_REQUEST);
}
if(saveFormDto.getPassword() == null
|| saveFormDto.getPassword().isEmpty()) {
throw new CustomRestfulException("비밀번호를 입력해주세요.", HttpStatus.BAD_REQUEST);
}
if(saveFormDto.getBalance() == null
|| saveFormDto.getBalance() < 0) {
throw new CustomRestfulException("잘못된 입력입니다..", HttpStatus.BAD_REQUEST);
}
accountService.creatAccount(saveFormDto, user.getId());
return "redirect:/account/list";
}
@GetMapping("/withdraw")
public String withdraw() {
return "account/withdraw";
}
@GetMapping("/deposit")
public String deposit() {
return "account/deposit";
}
@GetMapping("/transfer")
public String transfer() {
return "account/transfer";
}
@GetMapping("/detail")
public String detail() {
return "account/detail";
}
}
&<%@ 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>
<td>계좌번호</td>
<td>잔액</td>
</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>
</div>
</div>
</div>
<%@ include file="/WEB-INF/view/layout/footer.jsp"%>