SpringBoot with JPA프로젝트(gradle) 1.시작하기

mingki·2022년 1월 29일
0

SpringBoot & JPA

목록 보기
5/26


📚 공부한 책 : 코드로배우는 스프링 부트 웹프로젝트
❤️ github 주소 : https://github.com/qkralswl689/LearnFromCode/tree/main/guestbook2022

1.프로젝트를 통한 학습내용

  • 프로젝트의 계층별 구조와 객체들의 구성
  • Querydsl을 이용해 동적으로 검색 조건을 처리하는 방법
  • Entity 객체와 DTO의 구분
  • 페이징 처리

2.프로젝트 의존성 추가

build.gradle에 의존성을 아래와 같이 추가한다

dependencies {
	// JPA
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
       // Thymeleaf
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    	// web
	implementation 'org.springframework.boot:spring-boot-starter-web'
    	// lombok
	compileOnly 'org.projectlombok:lombok'
    	// devTools
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
    	//MySql
	runtimeOnly 'mysql:mysql-connector-java'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

3.DB 정보추가

application.properties에 아래와 같이 DB 정보 추가

# MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# DB Source URL
spring.datasource.url=jdbc:mysql://localhost:3306/bootex
# DB username
spring.datasource.username=BOOTUSER
# DB password
spring.datasource.password=1234
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
# 코드 변경후에 만들어진 결과를 보관(캐싱)하지 않도록 설정해 두는것
spring.thymeleaf.cache=false

4.컨트롤러 클래스 생성

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/guestbook")
public class GuestbookController {

    @GetMapping({"/","/list"})
    public String list(){
        return "/guestbook/list";
    }
}

5.자동으로 처리되는 날짜/시간 설정

5-1.추상클래스를 생성한다
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;

@MappedSuperclass // 테이블로 생성되지 않도록 해주는 어노테이션
@EntityListeners(value = {AuditingEntityListener.class}) // AuditingEntityListener : JPA 내부에서 엔티티 객체가 생성/변경 되는것을 감지하는 역할
@Getter
abstract class BaseEntity {
    @CreatedDate
    @Column(name = "regdate",updatable = false) // updatable = false : 객체를 DB에 반영할 때 regdate 컬럼값은 변경되지 않는다
    private LocalDateTime regDate;

    @LastModifiedDate
    @Column(name = "moddate")
    private LocalDateTime modDate;
}
5-2.main 클래스에 @EnableJpaAuditing 어노테이션을 추가한다
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@SpringBootApplication
@EnableJpaAuditing // BaseEntity클래스의 AuditingEntityListener를 활성화 시키기 위해 추가한다
public class Guestbook2022Application {

	public static void main(String[] args) {
		SpringApplication.run(Guestbook2022Application.class, args);
	}

}
profile
비전공초보개발자

0개의 댓글