💡JPA를 활용한 프로젝트! 전반적인 작업 과정!
<프로젝트 준비>
1. 프로젝트 생성(Spring Boot)
- Create Git repository
- Java
- Gradle-Groovy
- JDK : 17.xx.xx
- Java : 17
- Jar
2. 의존성(Dependency) 추가
*Spring Boot 3.x.x 버전
- Spring Web
- Lombok
- MySQL Driver
- Spring Data JPA
- (H2 Database)
3. 편의 기능 설정
*macOS: command + ,
*Windows: Ctrl + Alt + S
- (Auto Import)Editor - General - Auto Import : Always, Add ~, Optimize~
- (Editor Zoom) Editor - General : Mouse Control - Change ~ Mouse Wheel
- (Lombok) Build~ - Compiler - Annotation Processors : Enable annotation~
4. build.gradle 확인
- spring-boot-starter-data-jpa : Spring Data JPA 의존성
- spring-boot-starter-web : Spring Web 관련 의존성
- org.projectlombok:lombok : Lombok 의존성
- com.mysql:mysql-connector-j : MySQL 연결
5. Intellij Database 연동
- Database 탭 클릭 후 + 버튼
- Data Source > MySQL
- User, Password 정보 추가 후 Test Connection 클릭
- Test Connection 성공 시 OK
- 생성된 DataSource(@localhost) 우클릭 → New → Schema
- Name 설정 후 Schema 생성 OK
- 생성 완료 후 새로고침
6. JPA 관련 설정하기(application.properties)
▶ DataSource 설정
spring.datasource.url=jdbc:mysql://localhost:3306/Schema명(수정)
spring.datasource.username=계정(수정)
spring.datasource.password=비밀번호(수정)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
▶ Hibernate 설정
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
<spring.jpa.hibernate.ddl-auto 설정 옵션>
옵션 | 데이터 유지 여부 | 스키마 변경 여부 | 사용 환경 |
none | O 유지됨 | X 변경 없음 | 운영 환경(DB 직접 관리) |
create | X 초기화됨 | O 변경됨 | 개발/테스트 |
create-drop | X 종료 시 삭제 | O 변경됨 | 일시적인 테스트 |
update | O 유지됨 | 일부 변경됨 | 개발 환경 |
validate | O 유지됨 | X 변경 없음 | 운영 환경 |
+) 원격저장소 GitHub 연결(터미널 입력)
git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin github주소
git push -u origin main
+) 개인 정보 보호를 위해 .gitignore 추가
### ignore properties ###
application.properties
(터미널 입력)
git rm --cached src/main/resources/application.properties
<설계>
1. Package 생성, 구조 잡기
- controller
- dto
- entity
- repository
- service
2. Entity 클래스 생성
- @Getter
- @Entity
- @NoArgsConstructor
- @Id
- @GeneratatedValue(strategy = GenerationType.IDENTITY)
- update 메서드(9번 CRUD 구현 시 작성)
*필드 작성 ex) private Long id; / private String name;
*생성자 작성 : id는 DB에서 자동으로 생성해 주는 값이므로, id를 제외한 필드 중 원하는 필드를 선택해서 ‘생성자’ 만들기
3. Dto 클래스 생성
- @Getter
- RequestDto(final&생성자, id 불필요 : only 필드만 )
- ResponseDto(final&생성자, id 필요)
4. Repository 클래스 생성
- interface로 생성, JpaRepository 상속
5. Service 클래스 생성
- @Service
- @RequiredArgsConstructor
- 의존성 주입 : Repository repository (final)
6. Controller 클래스 생성
- @RestController
- @RequiredArgsConstructor
- 의존성 주입 : Service service (final)
7. CRUD 개발(Top-Down 방식 추천)
- Controller(POST, GET, PUT, PATCH, DELETE)
- Service(@Transactional)
마무리는 Postman으로 테스트 진행 :)
끝!
'개발 > 부트캠프' 카테고리의 다른 글
본캠프 : 특강 (0) | 2025.02.11 |
---|---|
본캠프 : 특강_협업 관점에서의 Git (0) | 2025.02.10 |
본캠프 : Spring 숙련_3주차 실습 (0) | 2025.02.06 |
본캠프 : Spring 숙련_2주차 (0) | 2025.02.05 |
본캠프 : Spring 숙련_1주차 (1) | 2025.02.04 |