create project

Mina Park·2022년 9월 17일
0

1. spring boot initializr
https://start.spring.io/

  • 이후 IDE에서 import
    • 인텔리제이의 경우 import할 때 build.gradle 바로 선택

2. build.gradle

plugins {
	id 'org.springframework.boot' version '2.7.3'
	id 'io.spring.dependency-management' version '1.0.13.RELEASE'
	id 'java'
}

group = 'jpabook'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-validation'
	implementation 'org.springframework.boot:spring-boot-devtools'
	implementation("com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.8.1")
	compileOnly 'org.projectlombok:lombok'
	runtimeOnly 'com.h2database:h2'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	//JUnit4 추가
	testImplementation("org.junit.vintage:junit-vintage-engine") {
		exclude group: "org.hamcrest", module: "hamcrest-core"
	}
}

tasks.named('test') {
	useJUnitPlatform()
}

3. 동작 확인


4. lombok 적용 확인

  • Prefrences plugin lombok 검색 실행 (재시작)
  • Prefrences Annotation Processors 검색 Enable annotation processing 체크 (재시작)
  • 임의의 테스트 클래스를 만들고 @Getter, @Setter 확인

5. 자바로 바로 실행하도록 설정 변경

최근 IntelliJ 버전은 Gradle로 실행을 하는 것이 기본 설정인데 실행속도가 느리므로 바로 바로 실행하도록 설정


6. 라이브러리

  • gradle 의존관계 보는 방법
    • 프로젝트 경로에서 아래 명령어
./gradlew dependencies —configuration compileClasspath
  • 스프링 부트 라이브러리 살펴보기
    spring-boot-starter-web
    spring-boot-starter-tomcat: 톰캣 (웹서버)
    spring-webmvc: 스프링 웹 MVC
    spring-boot-starter-thymeleaf: 타임리프 템플릿 엔진(View)
    spring-boot-starter-data-jpa
    spring-boot-starter-aop
    spring-boot-starter-jdbc
    HikariCP 커넥션 풀 (부트 2.0 기본)
    hibernate + JPA: 하이버네이트 + JPA
    spring-data-jpa: 스프링 데이터 JPA
    spring-boot-starter(공통): 스프링 부트 + 스프링 코어 + 로깅
    spring-boot
    spring-core
    spring-boot-starter-logging
    logback, slf4j
  • 테스트 라이브러리
    spring-boot-starter-test
    junit: 테스트 프레임워크
    mockito: 목 라이브러리
    assertj: 테스트 코드를 좀 더 편하게 작성하게 도와주는 라이브러리
    spring-test: 스프링 통합 테스트 지원
  • 핵심 라이브러리
    스프링 MVC
    스프링 ORM
    JPA, 하이버네이트
    스프링 데이터 JPA
    기타 라이브러리
    H2 데이터베이스 클라이언트
    커넥션 풀: 부트 기본은 HikariCP
    WEB(thymeleaf)
    로깅 SLF4J & LogBack
    테스트

7. View 환경설정


  • 동적 템플릿
@Controller
public class HelloController {

    @GetMapping("hello")
    public String hello(Model model) {
        model.addAttribute("data", "hello!!!");
        return "hello"; //view name
    }
}
  • hello.html 생성하여 템플릿 엔진 동작 확인
    • 위치: resources/templates/hello.html
    • 자동으로 templates 폴더 하위 파일들을 읽어냄
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'안녕하세요. ' + ${data}" >안녕하세요. 손님</p>
</body>
</html>

  • 정적 템플릿
    • 위치: static/index.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Hello
<a href="/hello">hello</a>
</body>
</html>

[참고] spring-boot-devtools 라이브러리를 추가하면, html 파일을 컴파일만 해주면 서버 재시작 없이 View 파일 변경 가능


8. H2 데이터베이스 설치

  • https://www.h2database.com
  • 데이터베이스 파일 생성 방법
    • jdbc:h2:~/jpashop (최소 한번)
    • ~/jpashop.mv.db 파일 생성 확인
    • 이후에는 jdbc:h2:tcp://localhost/~/jpashop 이렇게 바로 접속

9. application.yml 생성

  • application.properties는 삭제하고 yml으로 생성
spring:
  datasource:
    url: jdbc:h2:tcp://localhost/~/jpashop
    username: sa
    password:
    driver-class-name: org.h2.Driver
  jpa:
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
#        show_sql: true //system.out보다는 아래 logger를 통해서 찍어야함
        format_sql: true
logging:
  level:
    org.hibernate.SQL: debug
    org.hibernate.type: trace

[참고] 모든 로그 출력은 가급적 로거를 통해 남겨야 함

  • show_sql : 옵션은 System.out 에 하이버네이트 실행 SQL을 남긴다.
  • org.hibernate.SQL : 옵션은 logger를 통해 하이버네이트 실행 SQL을 남긴다.

[주의] application.yml 같은 yml 파일은 띄어쓰기(스페이스) 2칸으로 계층을 만들기 때문에 띄어쓰기 2칸 필수!!

implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.5.6'

[참고] 쿼리 파라미터를 로그로 남기는 외부 라이브러리는 시스템 자원을 사용하므로, 개발 단계에서는 편하게 사용 but 운영시스템에 적용하려면 꼭 성능테스트를 하고 사용

0개의 댓글