인생 디벨로퍼

[Bank App] 9강 계좌목록보기 본문

Project/개인 Project - Bank App

[Bank App] 9강 계좌목록보기

뫄뫙뫄 2023. 6. 16. 16:08
728x90

쿼리 만들기

유저 id 로, 계좌 목록을 조회할 수 있는 쿼리를 만듦

select * from account_tb where user_id = 1;


Account xml 파일 mapper

 <select id="findUserById" resultType="shop.mtcoding.bankapp.model.account.Account">
     select * from account_tb where user_id = #{principalId}
 </select>

Repository

public List<Account> findUserById(Integer principalId);

Controller

기존 코드에, list 보기 추가

 @GetMapping({ "/", "/account" })
    public String main(Model model) {

        User principal = (User) session.getAttribute("principal");
        if (principal == null) {
            return "redirect:/loginForm";
        }

        List<Account> accountList = accountRepository.findUserById(principal.getId());
        model.addAttribute("accountList", accountList);

        return "account/main";

    }

main.jsp 수정

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="../layout/header.jsp" %>

 <div class="m-4">
        <p class="text-center fs-4" style="font-weight: bolder;">메인페이지</p>
        <div class="my_form">
            <hr />
            <table class="table">
                <thead class="text-center">
                    <tr>
                        <th scope="col" style="font-weight: bolder;">계좌번호</th>
                        <th scope="col" style="font-weight: bolder;">잔액</th>
                    </tr>

                <tbody class="table-group-divider text-center">
                    <c:forEach items="${accountList}" var="accountList">
                        <tr>
                            <td scope="row">${accountList.number}</td>
                            <td>${accountList.balance}원</td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </div>
</body>

</html>

 


결과

728x90