Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
Tags
- 포함관계
- Thread
- 메서드 오버로딩
- IntelliJ IDEA
- 상수
- 생성자
- 컴파일
- Java
- java변수
- this예약어
- function
- continue문
- break문
- 인텔리제이 한글 깨짐 해결법
- 집합관계
- 자바 멀티스레딩
- multi-threading
- Java데이터 타입
- JAVA객체지향
- 시스템 환경 변수 편집
- OPP개념
- 접근제어지시자
- JAVA기초
- 반복문
- While
- 인텔리제이 기초 설정
- 형 변환
- 연관관계
- for문
- 메서드
Archives
- Today
- Total
최원종의 개발 블로그
V3-3 게시글 목록보기(연산관계로 작성자 정보 표시) 본문

BoardPersistRepository 코드 ( 게시글 조회 JPQL 문법으로 그대로 사용)
// JPQL을 사용한 게시글 목록 조회
public List<Board> findAll() {
String jpqlStr = "SELECT b FROM Board b ORDER BY b.id DESC";
List<Board> boardList = em.createQuery(jpqlStr, Board.class).getResultList();
return boardList;
}
Board 에서 연관관계 설정(
@JoinColumn(name = "user_id") // 외래키 컬럼명 표시 됨
private User user;)
으로 인해 자동으로 User에 대한 정보도 가지고 올 수 있음.
BoardController 코드
@GetMapping({"/", "index"})
public String list(Model model) {
List<Board> boardList = boardPersistRepository.findAll();
model.addAttribute("boardList", boardList);
return "board/list";
}
핵심 로직 흐름 정리
1. 데이터 조회: boardPersistRepository.findAll()을 호출하여
DB(board_tb)에 있는 모든 게시글 데이터를 List<Board> 형태로 가져옴.
2. 데이터 전달: model.addAttribute("boardList", boardList)를 통해
조회한 게시글 목록을 "boardList"라는 이름으로 모델에 담음
이제 뷰 파일에서 이 이름을 통해 데이터에 접근 가능.
3. 뷰 리턴: "board/list"를 리턴함
이는 src/main/resources/templates/board/list.mustache(또는 .html)
파일을 찾아 사용자에게 보여주라는 명령.
중요 개념 요약
작동 원리 클라이언트 요청 → Controller 데이터 조회 → Model에 저장 → View(Mustache) 렌더링
핵심 객체 Model: 비즈니스 로직과 화면 사이의 데이터 징검다리 역할을 수행함
데이터 흐름 DB(board_tb) → Entity(Board) → List → Model → View(list.mustache)
EAGER 전략
더보기
2026-04-29T11:09:26.876+09:00 INFO 14708 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2026-04-29T11:09:26.878+09:00 INFO 14708 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2026-04-29T11:09:26.878+09:00 INFO 14708 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms
Hibernate:
select
b1_0.id,
b1_0.content,
b1_0.created_at,
b1_0.title,
b1_0.user_id
from
board_tb b1_0
order by
b1_0.id desc
Hibernate:
select
u1_0.id,
u1_0.created_at,
u1_0.email,
u1_0.password,
u1_0.username
from
user_tb u1_0
where
u1_0.id=?
Hibernate:
select
u1_0.id,
u1_0.created_at,
u1_0.email,
u1_0.password,
u1_0.username
from
user_tb u1_0
where
u1_0.id=?
Hibernate:
select
u1_0.id,
u1_0.created_at,
u1_0.email,
u1_0.password,
u1_0.username
from
user_tb u1_0
where
u1_0.id=?
Hibernate:
select
u1_0.id,
u1_0.created_at,
u1_0.email,
u1_0.password,
u1_0.username
from
user_tb u1_0
where
u1_0.id=?
Hibernate:
select
u1_0.id,
u1_0.created_at,
u1_0.email,
u1_0.password,
u1_0.username
from
user_tb u1_0
where
u1_0.id=?
kim
hong
cos
cos
ssar
ssar
ssar
admin
admin
admin
LAZY 전략
더보기
Hibernate:
select
b1_0.id,
b1_0.content,
b1_0.created_at,
b1_0.title,
b1_0.user_id
from
board_tb b1_0
order by
b1_0.id desc
Hibernate:
select
u1_0.id,
u1_0.created_at,
u1_0.email,
u1_0.password,
u1_0.username
from
user_tb u1_0
where
u1_0.id=?
kim
Hibernate:
select
u1_0.id,
u1_0.created_at,
u1_0.email,
u1_0.password,
u1_0.username
from
user_tb u1_0
where
u1_0.id=?
hong
Hibernate:
select
u1_0.id,
u1_0.created_at,
u1_0.email,
u1_0.password,
u1_0.username
from
user_tb u1_0
where
u1_0.id=?
cos
cos
Hibernate:
select
u1_0.id,
u1_0.created_at,
u1_0.email,
u1_0.password,
u1_0.username
from
user_tb u1_0
where
u1_0.id=?
ssar
ssar
ssar
Hibernate:
select
u1_0.id,
u1_0.created_at,
u1_0.email,
u1_0.password,
u1_0.username
from
user_tb u1_0
where
u1_0.id=?
admin
admin
admin
머스태치 파일 코드 추가(list.mustache)
{{> layout/header}}
<div class="container p-5 flex-grow-1">
{{#boardList}}
<div class="card mb-3">
<div class="card-body">
<h4 class="card-title mb-3">{{title}}</h4>
<!-- 머스태치 문법은 getter 메서드를 가지고 오고 get은 탈락 가능 -->
<div> 작성자 : {{user.username}} | 작성일 : {{time}}</div>
<a href="/board/{{id}}" class="btn btn-primary">상세보기</a>
</div>
</div>
{{/boardList}}
<ul class="pagination d-flex justify-content-center">
<li class="page-item"><a href="" class="page-link">Previous</a></li>
<li class="page-item"><a href="" class="page-link"> Next</a></li>
</ul>
</div>
{{> layout/footer}}
게시글 목록에서 연관관계 사용 시 핵심 설정
LAZY 로딩이 중요한 이유
@ManyToOne(fetch = FetchType.LAZY) // 중요한 설정
private User user;
**EAGER 사용 시 문제점:**
- 목록 조회할 때마다 모든 User 정보를 무조건 가져옴
- 불필요한 JOIN으로 성능 저하
- 메모리 낭비 발생
**LAZY 사용 시 장점:**
- 필요할 때만 User 정보 조회
- 메모리 효율성
- 선택적 데이터 로딩 가능'Spring boot 입문' 카테고리의 다른 글
| V3-5 로그인& 로그아웃(세션 기반 사용자 인증) (1) | 2026.05.12 |
|---|---|
| V3-4 회원가입(사용자 등록과 JPA 영속성 활용) (0) | 2026.05.12 |
| V3-2 게시글 상세보기 (연관관계 기본 활용) (0) | 2026.05.11 |
| V3 (사용자 관리 및 연관 관계 설정) -1 연관관계 설정하기 (0) | 2026.05.11 |
| V2(PersistentContext) 게시글 수정하기 - Persistence Context와 Dirty Checking 활용 (0) | 2026.05.11 |
