| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 호이스팅
- 스프링 부트
- 레디스
- 캐시
- 깃허브
- spring security
- 정보처리기사
- document database
- NoSQL
- Redis
- github
- 스프링부트
- 영속성 컨텍스트
- 가상 면접 사례로 배우는 대규모 시스템 설계 기초
- JPA
- 다이나믹프로그래밍
- SQL
- Spring Boot
- 동적계획법
- VMware
- 분할정복
- 이벤트루프
- 게시판
- MongoDB
- 정처기
- 자바의 정석
- 스프링 시큐리티
- sqld
- in-memory
- 실행 컨텍스트
- Today
- Total
FreeHand
[Spring Boot] 게시판 프로젝트 - 01. 개발환경 및 엔티티 작성 본문

목차
1. 개발환경
2. 프로젝트 생성
1. 개발환경
개발환경은 다음과 같다.
- Java 17
- Spring Boot 3
- Spring Security 5
- OAuth
- JPA
- MySQL
- JSP
- Bootstrap
- JQuery
2. 프로젝트 생성
1. 의존성 설정
먼저 사용할 의존성을 설정한다.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.security:spring-security-taglibs'
implementation 'jakarta.servlet:jakarta.servlet-api' //스프링부트 3.0 이상
implementation 'jakarta.servlet.jsp.jstl:jakarta.servlet.jsp.jstl-api' //스프링부트 3.0 이상
implementation 'org.glassfish.web:jakarta.servlet.jsp.jstl' //스프링부트 3.0 이상
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
스프링부트의 기본 뷰 템플릿이 Thymeleaf라서 JSP를 사용하려면 의존성을 추가해줘야 한다.
또한 JSTL은 스프링부트 3 버전과 이전 버전의 의존성 추가 코드가 다르다.
2. 엔티티 클래스 작성
JPA를 사용하므로 엔티티 클래스를 작성해야 한다.
@AllArgsConstructor
@NoArgsConstructor
@Getter @Setter
@Builder
@Entity
@Table(name = "users")
// @DynamicInsert 객체 생성 시 null인 컬럼은 insert에서 제외함
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true, length = 30)
private String username;
@Column(nullable = false, length = 100)
private String password;
@Column(nullable = false, length = 50)
private String email;
@Enumerated(EnumType.STRING)
private RoleType role = RoleType.USER;
@CreationTimestamp
private Timestamp createdAt;
}
public enum RoleType {
USER,
ADMIN
}
추후 권한 부여를 위해 role을 작성하는데, String 타입보다 Enum을 사용하면 더 정확하게 사용할 수 있어 좋다.
@DynamicInsert를 사용해서 User 객체 생성 시에 자동으로 role을 "USER"로 지정할 수 있지만 해당 어노테이션은 여러 필드에 다 적용될 수 있어서 개인적으로 사용이 꺼려져서 사용하지 않았다.
생성 시간인 createdAt 필드는 @CreationTimestamp를 사용해서 객체 생성 시에 자동으로 초기화된다.
@AllArgsConstructor
@NoArgsConstructor
@Getter @Setter
@Builder
@Entity
@Table(name = "boards")
public class Board {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 100)
private String title;
@Column(columnDefinition = "longblob")
private String content;
@Column
private int viewCount = 0;
@ManyToOne
@JoinColumn(name = "userId")
private User user;
@OneToMany(mappedBy = "board", fetch = FetchType.EAGER)
private List<Comment> comments = new ArrayList<>();
@CreationTimestamp
private Timestamp createdAt;
}
본문에 해당한는 content 필드는 html 태그 및 이미지 주소 등이 포함될 수 있어서 큰 데이터를 저장할 수 있도록 타입을 지정했다.
@AllArgsConstructor
@NoArgsConstructor
@Getter @Setter
@Builder
@Entity
@Table(name = "comments")
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 200)
private String content;
@ManyToOne
@JoinColumn(name = "boardId")
private Board board;
@ManyToOne
@JoinColumn(name = "userId")
private User user;
@CreationTimestamp
private Timestamp createdAt;
}
다음 글
[Spring Boot] 게시판 프로젝트 - 02. 회원가입 및 로그인
목차1. 회원가입 작성2. 로그인 작성 1. 회원가입 작성 [joinForm.jsp] Username: Password: Email address: 회원가입[user.js]let index = { init: function() { $("#btn-save").on("click", () => { this.save(); }); }, save: function() { let d
pressky99.tistory.com
'Web > Spring' 카테고리의 다른 글
| [Spring Boot] 게시판 프로젝트 - 05. 댓글 기능 (0) | 2023.10.04 |
|---|---|
| [Spring Boot] 게시판 프로젝트 - 04. 회원정보 수정 (0) | 2023.10.01 |
| [Spring Boot] 게시판 프로젝트 - 03. 게시판 글 CRUD (0) | 2023.09.30 |
| [Spring Boot] 게시판 프로젝트 - 02. 회원가입 및 로그인 (0) | 2023.09.25 |
| [스프링 Core] 스프링 핵심 원리 - 기본편1 (0) | 2023.09.14 |