Board.java
package jpa.board.controller.entity;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.time.LocalDateTime;
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@EntityListeners(AuditingEntityListener.class)
public class Board {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "board_id")
private Long id; //번호
private String title; //제목
private String content; //내용
@CreatedDate
private LocalDateTime regDate; //등록 날짜
@LastModifiedDate
private LocalDateTime uptDate; //수정 날짜
private Long viewCount; //조회수
private String delYn; //삭제여부
@ManyToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;
public Board update(String title, String content) {
this.title = title;
this.content = content;
return this;
}
public Board delete(String delYn) {
this.delYn = delYn;
return this;
}
public Board(String title, String content, Member member) {
this.title = title;
this.content = content;
this.viewCount = 0L;
this.delYn = "N";
this.member = member;
}
}
기존에 있던 Test클래스는 삭제하고 Board클래스를 만들어서 작성
Authority.java
package jpa.board.entity;
public enum Authority {
ROLE_USER, ROLE_ADMIN
}
사용자 한 명당 권한은 한 개만 가지게 설정하기 위해 사용자 권한을 지정한 enum 파일 생성
Member.java
package jpa.board.entity;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "member_id")
private Long id; //번호
private String username; //작성자
private String password; //비밀번호
private String phoneNo; //휴대폰번호
private int age; //나이
@Column(name = "user_role")
@Enumerated(EnumType.STRING)
private Authority authority; //권한 [ROLE_USER, ROLE_ADMIN]
@OneToMany(mappedBy = "member")
private List<Board> boards = new ArrayList<>();
@Builder
public Member(String username, String phoneNo, int age, Authority authority) {
this.username = username;
this.phoneNo = phoneNo;
this.age = age;
this.authority = authority;
}
}
MemberRepository.java
package jpa.board.repository;
import jpa.board.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MemberRepository extends JpaRepository<Member, Long> {
}
BoardRepository.java
package jpa.board.repository;
import jpa.board.entity.Board;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BoardRepository extends JpaRepository<Board, Long> {
}
인터페이스로 생성
InitDB.java
package jpa.board;
import jpa.board.entity.Member;
import jpa.board.repository.BoardRepository;
import jpa.board.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.transaction.Transactional;
import java.util.List;
import static jpa.board.entity.Authority.ROLE_ADMIN;
@Component
@RequiredArgsConstructor
public class InitDB {
private final InitService initService;
@PostConstruct
public void init() {
initService.userDBInit();
}
@Component
@Transactional
@RequiredArgsConstructor
static class InitService {
private final MemberRepository memberRepository;
private final BoardRepository boardRepository;
public void userDBInit() {
List<Member> memberList = memberRepository.findAll();
if(memberList.size() == 0) {
Member member = Member.builder()
.username("관리자")
.phoneNo("010-1234-5678")
.age(28)
.authority(ROLE_ADMIN)
.build();
//member 저장
memberRepository.save(member);
}
}
}
}
jpa.board패키지 밑에 initDB.java를 생성합니다.
build.gradle에 라인 추가
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.5.6'
한글이라 깨지지만 정상 작동