Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-webmvc'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0'

compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
Expand Down
3 changes: 3 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '1.0.0'
}
rootProject.name = 'session'
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

import java.util.List;

@RestController
@RequestMapping("/boards")
@RequiredArgsConstructor
@RestController //JSON 형태로 응답을 내보내는 Controller
@RequestMapping("/boards") //이 Controller의 모든 URL에 /boards를 기본으로 붙여줌.
@RequiredArgsConstructor //필수 인자를 가진 생성자를 자동으로 생성
public class BoardController {

private final ;
private final BoardService boardService;

/*
게시글 생성
Expand All @@ -36,7 +36,7 @@ public class BoardController {
summary = "게시글 생성",
description = "새로운 게시글을 생성합니다."
)
@
@PostMapping //데이터 생성
public ResponseEntity<BoardResponse> create(@RequestBody BoardCreateRequest request) {
BoardResponse response = boardService.create(request);
return ResponseEntity.ok(response);
Expand All @@ -47,7 +47,7 @@ public ResponseEntity<BoardResponse> create(@RequestBody BoardCreateRequest requ
summary = "게시글 전체 조회",
description = "등록된 모든 게시글을 조회합니다."
)
@
@GetMapping //데이터 조회
public ResponseEntity<List<BoardResponse>> findAll() {
List<BoardResponse> response = boardService.findAll();
return ResponseEntity.ok(response);
Expand All @@ -69,7 +69,7 @@ public ResponseEntity<BoardResponse> findById(@PathVariable Long id) {
summary = "게시글 수정",
description = "id로 특정 게시글의 제목과 내용을 수정합니다."
)
@("/{id}")
@PutMapping("/{id}") //데이터 수정
public ResponseEntity<BoardResponse> update(@PathVariable Long id,
@RequestBody BoardUpdateRequest request) {
BoardResponse response = boardService.update(id, request);
Expand All @@ -81,7 +81,7 @@ public ResponseEntity<BoardResponse> update(@PathVariable Long id,
summary = "게시글 삭제",
description = "id로 특정 게시글을 삭제합니다."
)
@("/{id}")
@DeleteMapping("/{id}") //데이터 삭제
public ResponseEntity<Void> delete(@PathVariable Long id) {
boardService.delete(id);
return ResponseEntity.noContent().build();
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/likelion/session/domain/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,34 @@

@Getter // getter 메서드 자동 생성
@Entity // 해당 클래스 DB 테이블로 인식하고 관리
@Table(name = "boards")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Table(name = "boards") //실제 DB에 생성될 테이블 이름 지정. 없으면 클래스 이름(Board)이 테이블 이름이 됨.
@NoArgsConstructor(access = AccessLevel.PROTECTED) //파라미터 없는 기본 생성자를 만들어줌.
@AllArgsConstructor //파라미터 없는 기본 생성자를 만들어줌.
public class Board {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long ;
@Id //PK
@GeneratedValue(strategy = GenerationType.IDENTITY) //id 값을 DB가 자동으로 1, 2, 3... 순서대로 증가시켜 직접 1, 2, 3, ..을 입력할 필요 없음
private Long id;

// 게시글 제목
@Column(nullable = false, length = 100)
private String ;
@Column(nullable = false, length = 100) //해당 컬럼의 옵션 설정
private String title;

// 게시글 내용
@Column(nullable = false, columnDefinition = "TEXT")
private String ;
private String content;

// 작성자
@Column(nullable = false, length = 30)
private String ;
private String writer;

// 생성 시간
@Column(nullable = false)
private LocalDateTime ;
private LocalDateTime createdAt;

// 수정 시간
@Column(nullable = false)
private LocalDateTime ;
private LocalDateTime updatedAt;


public Board(String title, String content, String writer) {
Expand All @@ -46,13 +46,13 @@ public Board(String title, String content, String writer) {
this.writer = writer;
}

@PrePersist
@PrePersist //DB에 저장되기 직전에 자동으로 실행되는 메서드. createdAt(생성시간) 현재 시간으로 자동으로 채워줌.
public void prePersist() {
this.createdAt = LocalDateTime.now();
this.updatedAt = LocalDateTime.now();
}

@PreUpdate
@PreUpdate //DB에서 수정되기 직전에 자동으로 실행됨. updatedAt(수정 시간)을 갱신.
public void preUpdate() {
this.updatedAt = LocalDateTime.now();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@AllArgsConstructor
public class BoardCreateRequest {
// 넘겨주고 싶은 정보: 제목(title), 내용(content), 작성자(writer)
private String ;
private String ;
private String ;
private String title;
private String content;
private String writer;
}
2 changes: 1 addition & 1 deletion src/main/java/com/likelion/session/dto/BoardResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@Getter
@AllArgsConstructor
@Builder
@Builder //Service에서 .builder().id(xxx).title(xxx)...build() 식으로 필드를 선택적으로 세팅할 수 있음.
public class BoardResponse {

// 돌려주고 싶은 응답: id, title, content, writer, createdAt, updatedAt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

@Getter
@Setter
@Getter //getId(), getTitle() 같은 getter 메서드를 자동 생성.
@Setter //setTitle(), setContent() 같은 메서드 자동 생성
@NoArgsConstructor
public class BoardUpdateRequest {
@NotBlank(message = "제목은 필수입니다.")
Expand All @@ -18,4 +18,3 @@ public class BoardUpdateRequest {
@NotBlank(message = "내용은 필수입니다.")
private String content;
}

8 changes: 4 additions & 4 deletions src/main/java/com/likelion/session/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
@Service //이 클래스가 비즈니스 로직 담당임을 Spring에 알려줌.
@RequiredArgsConstructor // final이 붙은 필드(boardRepository)를 생성자로 자동 주입해줌
@Transactional //DB 작업 도중 에러가 나면 모든 작업을 이전으로 롤백함, 조회 전용 설정으로 성능 최적화에 씀.
public class BoardService {

private final ;
private final BoardRepository boardRepository;

/*
게시글 생성
Expand Down
28 changes: 28 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
server:
port: 8082

spring:
application:
name: session

datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/session?serverTimezone=Asia/Seoul&characterEncoding=UTF-8
username: root
password: abcd8907

jpa:
hibernate:
ddl-auto: update
show-sql: true
properties:
hibernate:
format_sql: true

jackson:
serialization:
fail-on-empty-beans: false

logging:
level:
org.hibernate.SQL: debug