모아이티

[Springboot] 코드로 배우는 스프링부트 웹 프로젝트 Part2 - #2 본문

카테고리 없음

[Springboot] 코드로 배우는 스프링부트 웹 프로젝트 Part2 - #2

Yun's kitchen 2021. 6. 14. 15:57

Entity 패키지 생성 - BaseEntity.java

 

 

@EnableJpaAuditing 추가

-> 데이터베이스에서 누가, 언제 하였는지 기록을 잘 남겨놓아야 합니다.

 

그렇기 때문에 생성일, 수정일 칼럼은 대단히 중요한 데이터입니다.

 

그래서 JPA에서는 Audit이라는 기능을 제공하고 있습니다. Audit은 감시하다, 감사하다는 뜻으로 Spring Data JPA에서 시간에 대해서 자동으로 값을 넣어주는 기능입니다.

 

BaseEntity.java

package org.joe.guestbook.entity;

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})
@Getter
abstract class BaseEntity {

    @CreatedDate
    @Column(name = "regdate", updatable = false)
    private LocalDateTime regDate;

    @LastModifiedDate
    @Column(name = "modDate")
    private LocalDateTime modDate;
}

Guestbook.java

package org.joe.guestbook.entity;

import lombok.*;

import javax.persistence.*;

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Guestbook  extends BaseEntity{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long gno;

    @Column(length = 100, nullable = false)
    private String title;

    @Column(length = 1500, nullable = false)
    private String content;

    @Column(length = 50, nullable = false)
    private String writer;

}

 

MariaDB에서 GuestEntity 생성 확인

 

GuestbookRepository 인터페이스 생성 - JpaRepository GuestbookRepository에 있는 Long type 상속

package org.joe.guestbook.repository;

import org.joe.guestbook.entity.Guestbook;
import org.springframework.data.jpa.repository.JpaRepository;

public interface GuestbookRepository extends JpaRepository<Guestbook, Long> {
}

Querydsl

Querydsl dependency 추가

plugins {
    id 'org.springframework.boot' version '2.5.1'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'war'
    id 'com.ewerk.gradle.plugins.querydsl' version '1.0.10'  //추가된 항목
}

group = 'org.joe'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

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'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
    annotationProcessor 'org.projectlombok:lombok'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    implementation 'org.thymeleaf.extras:thymeleaf-extras-java8time:3.0.4.RELEASE' //추가된 항목
    implementation 'com.querydsl:querydsl-jpa' //추가된 항목
}

test {
    useJUnitPlatform()
}

//여기서부터 추가
def querydslDir = "$buildDir/generated/querydsl"

querydsl {
    jpa = true
    querydslSourcesDir = querydslDir}

sourceSets {
    main.java.srcDir querydslDir}

configurations {
    querydsl.extendsFrom compileClasspath}

compileQuerydsl {
    options.annotationProcessorPath = configurations.querydsl}

 

QuerydslPredicateExecutor 상속받기

package org.joe.guestbook.repository;

import org.joe.guestbook.entity.Guestbook;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

public interface GuestbookRepository extends JpaRepository<Guestbook, Long>,
        QuerydslPredicateExecutor<Guestbook> {

}
Comments