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
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 //final이 붙은 필드(boardRepository)를 생성자로 자동 주입해줌
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
27 changes: 13 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에 생성될 테이블 이름 지정
@NoArgsConstructor(access = AccessLevel.PROTECTED) //파라미터 없는 기본 생성자를 만들어줌
@AllArgsConstructor //모든 필드를 파라미터로 받는 생성자를 자동 생성
public class Board {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long ;
@Id //이 필드가 테이블의 기본키(Primary Key)임을 나타냄
@GeneratedValue(strategy = GenerationType.IDENTITY) //id 값을 DB가 자동으로 1, 2, 3 순서대로 증가시켜 직접 1, 2, 3을 입력할 필요 없음
private Long id;

// 게시글 제목
@Column(nullable = false, length = 100)
private String ;
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,12 @@ public Board(String title, String content, String writer) {
this.writer = writer;
}

@PrePersist
@PrePersist //createdAt(생성 시간)을 현재 시간으로 자동으로 채워줌
public void prePersist() {
this.createdAt = LocalDateTime.now();
this.updatedAt = LocalDateTime.now();
}

@PreUpdate
@PreUpdate //updatedAt(수정 시간)을 갱신.
public void preUpdate() {
this.updatedAt = LocalDateTime.now();
}
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/likelion/session/dto/BoardCreateRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Getter //getter 메서드를 자동 생성
@Setter
@NoArgsConstructor
@AllArgsConstructor
@NoArgsConstructor //파라미터 없는 기본 생성자를 만들어줌
@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 @@ -7,7 +7,7 @@
import java.time.LocalDateTime;

@Getter
@AllArgsConstructor
@AllArgsConstructor //모든 필드를 파라미터로 받는 생성자를 자동 생성
@Builder
public class BoardResponse {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Getter
@Setter
@NoArgsConstructor
@NoArgsConstructor //파라미터 없는 기본 생성자를 만들어줌
public class BoardUpdateRequest {
@NotBlank(message = "제목은 필수입니다.")
@Size(max = 100, message = "제목은 100자 이하로 입력해주세요.")
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/likelion/session/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

import java.util.List;

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

private final ;
private final BoardRepository boardRepository;

/*
게시글 생성
Expand Down Expand Up @@ -51,7 +51,7 @@ public BoardResponse create(BoardCreateRequest request) {
- DB에 있는 모든 게시글을 가져옴
- Entity 리스트를 Response DTO 리스트로 변환
*/
@Transactional(readOnly = true)
@Transactional(readOnly = true) //조회 전용 설정 > 성능 최적화
public List<BoardResponse> findAll() {
return boardRepository.findAll()
.stream()
Expand Down
27 changes: 27 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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: 1234
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