[Spring boot] 프로젝트 기본 설정

Jeskey·2024년 6월 20일

스프링부트 3.0 이상부터는 자바 17 이상에서 동작하므로 새 jdk 17버전을 받아서 설치해야 한다.

또한 환경 변수 편집에 등록한 자바 버전도 새로 설치한 자바의 경로로 바꿔주어야 한다.


종속성(Dependencies)

  • Spring Boot DevTools
  • Lombok
  • Spring Web
  • Thymeleaf
  • Spring Data JPA
  • MariaDB Driver

선택 후 생성


📂 application.properties

//마리아db
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://localhost:3306/webdb
spring.datasource.username=webuser
spring.datasource.password=123123

//jpa
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto 설정을 create로 해놓으면 서버를 재시작할 때 마다 db 데이터가 전부 날라간다.

만일 @OnDelete 어노테이션을 사용할 예정이라면 create로 초기 설정을 해 놓은 뒤, 테이블이 만들어지면 update로 바꾸어야 한다. update로 테이블을 만들 시 자식 엔티티에 @OnDelete 어노테이션이 적용되지 않기 때문이다.

다만 이 프로젝트에서는 OnDelete 어노테이션을 사용하지 않으므로 처음부터 update로 설정했다.

📂 build.gradle

dependencies {

	//기본 생략

    //lombok 추가
    testCompileOnly 'org.projectlombok:lombok'
    testAnnotationProcessor 'org.projectlombok:lombok'

    //유효성 검증 추가
    implementation 'org.springframework.boot:spring-boot-starter-validation'

    //시큐리티 추가
    implementation 'org.springframework.boot:spring-boot-starter-security'

    //Querydsl 추가
    implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
    annotationProcessor(
            "jakarta.annotation:jakarta.annotation-api",
            "jakarta.persistence:jakarta.persistence-api",
            "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
    )

   //thymeleaf
    implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:3.1.0'
    implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'

    //swagger-ui
    implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'

    // json
    implementation 'org.json:json:20230227'
}

//Querydsl 시작
def generated='src/main/generated'

sourceSets {
    main.java.srcDirs += [ generated ]
}

tasks.withType(JavaCompile) { // compile시
    options.generatedSourceOutputDirectory = file(generated)
}

clean.doLast { // clean시 폴더 삭제
    file(generated).deleteDir()
}
//Querydsl 끝

테스트 코드를 실행해서 정상적으로 DB 연결이 되는 것을 확인한다.

📂 BookmarkApplicationTests

@SpringBootTest
@Log4j2
class BookmarkApplicationTests {

    @Autowired
    private DataSource dataSource;

    @Test
    void contextLoads() throws SQLException {

        @Cleanup
        Connection con = dataSource.getConnection();

        log.info(con);
        Assertions.assertNotNull(con);
    }
}

0개의 댓글