개발
Day 41 - Spring Boot pt.1
·1분 읽기
이 글은 2026년 04월 28일 작성된 글입니다.
오늘은 Spring Boot + JPA 기본 흐름과 엔티티 관계 매핑, 리포지터리 사용법까지 정리했다.
1. Spring Boot 기본 구조
Spring Initializr로 프로젝트를 생성하고 Controller를 통해 요청을 처리한다.
@Controller
public class MainController {
@GetMapping("/")
@ResponseBody
public String index() {
return "SBB";
}
}- @Controller → 요청 처리 클래스
- @GetMapping → URL 매핑
- @ResponseBody → 반환값을 바로 응답으로 사용
2. JPA 도입
implementation("org.springframework.boot:spring-boot-starter-data-jpa")- 객체 기반 DB 처리
3. 엔티티 관계 (@ManyToOne)
@ManyToOne
private Question question;- 외래키 생성
4. mappedBy (핵심)
@OneToMany(mappedBy = "question")- 중간 테이블 방지
5. Repository
questionRepository.findAll();
questionRepository.findById(1);6. JPA 메서드 규칙
findBySubject(String subject)
findBySubjectAndContent(String subject, String content)7. 트랜잭션
@Transactional✅ 정리
- JPA는 객체 중심 DB 처리 방식이다.
- mappedBy 설정이 매우 중요하다.
- Repository로 CRUD를 간단하게 처리할 수 있다.