[✈️Travel Mate] 프로젝트 셋업

Dreamer·2024년 5월 28일

🛫 개발 환경 만들기

1. Gradle 의존성 체크

프로젝트 생성 후 내가 선택한 의존성이 잘 들어가 있는지 확인한다!
나는 헷갈려서 주석으로 어떤 의존성인지 분류했다.

dependencies {
	// Spring Boot
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-security'
	implementation 'org.springframework.boot:spring-boot-starter-mail'

	// DB
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'com.mysql:mysql-connector-j'

	implementation 'org.springframework.boot:spring-boot-starter-data-redis'
	implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'

	// Lombok
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'

	// Development
	developmentOnly 'org.springframework.boot:spring-boot-devtools'

	// Validation
	implementation 'org.springframework.boot:spring-boot-starter-validation'

	// Test
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'org.springframework.security:spring-security-test'
	testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

2. 패키지 구조 생각

Controller / Service / Repository / Entity = Domain / Dto
Config / Util / Security / Exception

3. MySQL과 DB연동 & Entity 생성

데이터베이스 연동

  • jdbc 드라이버 Gradle에 추가
implementation 'com.mysql:mysql-connector-j'
  • application.yml 파일에 db정보 입력 -> github에 올라가지 않도록 주의!!
spring:
  # MySQL
  datasource:
    url: jdbc:mysql://localhost:3306/{나의 DB명}
    username: {내가 설정한 이름}
    password: {내가 설정한 비밀번호}
    driver-class-name: com.mysql.cj.jdbc.Driver

DB Table참조하며 Entity생성 ⭐

4. 전반적인 뼈대 작성

Entity작성을 하고 데이터베이스와 가까운 순서로 클래스(인터페이스) 생성

Repository 생성

  • Repository interface 생성
    Spring Data JPA의 JpaRepository를 상속하여 구현
@Repository
public interface Repository extends JpaRepository<엔티티 이름, PK type> {

}
  1. @Repository 어노테이션
    : 해당 클래스가 Repository 역할을 한다는 것을 나타냅니다.
    @Repository 어노테이션이 붙은 클래스를 자동으로 스캔하여 빈(Bean)으로 등록합니다.

  2. extends JpaRepository <엔티티 이름, PK type> 으로 JpaRepository 상속
    : JpaRepository는 Spring Data JPA에서 제공하는 인터페이스
    Ex. User 엔티티의 기본 키가 Long 타입이라면, JpaRepository<User, Long>과 같이 작성합니다!

Service 생성

  • Service class 생성
@Service
@Slf4j
@RequiredArgsConstructor
public class Service {

}
  1. @Service 어노테이션
    : 해당 클래스가 Service 역할을 한다는 것을 나타냅니다.
    @Repository 어노테이션이 붙은 클래스를 자동으로 스캔하여 빈(Bean)으로 등록합니다.

  2. @Slf4j 어노테이션
    : Lombok 라이브러리에서 제공하는 어노테이션입니다.
    이 어노테이션을 사용하면 logger 객체를 자동으로 생성해줍니다.
    Ex. log.info(), log.error() 등의 로깅 메서드를 사용할 수 있습니다.

  3. @RequiredArgsConstructor 어노테이션
    : Lombok 라이브러리에서 제공하는 어노테이션입니다.
    클래스에 final 필드가 있는 경우, 해당 필드들을 파라미터로 받는 생성자를 자동으로 생성해줍니다.
    👍 개발자가 직접 생성자를 작성할 필요가 없습니다.

Controller 생성

  • Controller class 생성
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/공통 URL")
public class Controller {

}
  1. @RestController 어노테이션
    : 해당 클래스가 Controller 역할을 한다는 것을 나타냅니다.
    @Controller + @ResponseBody 의 기능을 모두 포함하고 있습니다.

  2. @RequestMapping("/공통 URL") 어노테이션
    : 해당 컨트롤러 클래스의 모든 핸들러 메서드에 "/공통 URL" 경로를 공통적으로 접두사로 추가합니다.

profile
Moving forward based on records

0개의 댓글