프로젝트 생성 후 내가 선택한 의존성이 잘 들어가 있는지 확인한다!
나는 헷갈려서 주석으로 어떤 의존성인지 분류했다.
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'
}
Controller / Service / Repository / Entity = Domain / Dto
Config / Util / Security / Exception
implementation 'com.mysql:mysql-connector-j'
spring:
# MySQL
datasource:
url: jdbc:mysql://localhost:3306/{나의 DB명}
username: {내가 설정한 이름}
password: {내가 설정한 비밀번호}
driver-class-name: com.mysql.cj.jdbc.Driver
Entity작성을 하고 데이터베이스와 가까운 순서로 클래스(인터페이스) 생성
@Repository
public interface Repository extends JpaRepository<엔티티 이름, PK type> {
}
@Repository 어노테이션
: 해당 클래스가 Repository 역할을 한다는 것을 나타냅니다.
@Repository 어노테이션이 붙은 클래스를 자동으로 스캔하여 빈(Bean)으로 등록합니다.
extends JpaRepository <엔티티 이름, PK type> 으로 JpaRepository 상속
: JpaRepository는 Spring Data JPA에서 제공하는 인터페이스
Ex. User 엔티티의 기본 키가 Long 타입이라면, JpaRepository<User, Long>과 같이 작성합니다!
@Service
@Slf4j
@RequiredArgsConstructor
public class Service {
}
@Service 어노테이션
: 해당 클래스가 Service 역할을 한다는 것을 나타냅니다.
@Repository 어노테이션이 붙은 클래스를 자동으로 스캔하여 빈(Bean)으로 등록합니다.
@Slf4j 어노테이션
: Lombok 라이브러리에서 제공하는 어노테이션입니다.
이 어노테이션을 사용하면 logger 객체를 자동으로 생성해줍니다.
Ex. log.info(), log.error() 등의 로깅 메서드를 사용할 수 있습니다.
@RequiredArgsConstructor 어노테이션
: Lombok 라이브러리에서 제공하는 어노테이션입니다.
클래스에 final 필드가 있는 경우, 해당 필드들을 파라미터로 받는 생성자를 자동으로 생성해줍니다.
👍 개발자가 직접 생성자를 작성할 필요가 없습니다.
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/공통 URL")
public class Controller {
}
@RestController 어노테이션
: 해당 클래스가 Controller 역할을 한다는 것을 나타냅니다.
@Controller + @ResponseBody 의 기능을 모두 포함하고 있습니다.
@RequestMapping("/공통 URL") 어노테이션
: 해당 컨트롤러 클래스의 모든 핸들러 메서드에 "/공통 URL" 경로를 공통적으로 접두사로 추가합니다.