Open Paransaik opened 9 months ago
BoardController를 아래와 같이 작성하였습니다.
@RestController
@RequestMapping("/api/boards")
@RequiredArgsConstructor
public class BoardController {
private final BoardService boardService;
// 전체 목록 조회
@GetMapping("/all")
public List<BoardEntity> getAllBoards() {
return boardService.getAllBoards();
}
// 글 하나 조회
@GetMapping("/{id}")
public BoardEntity getBoardById(@PathVariable int id) {
return boardService.getBoardById(id);
}
// 글 등록
@PostMapping
public BoardEntity createBoard(@RequestBody BoardEntity board) {
return boardService.createBoard(board); // Return the created board
}
@PutMapping("/{id}")
public ResponseEntity<String> updateBoard(@PathVariable int id, @RequestBody BoardEntity board) {
boolean isUpdated = boardService.updateBoard(id, board);
if (isUpdated) {
return ResponseEntity.ok("Board updated successfully");
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Board not found with id: " + id);
}
}
@DeleteMapping("/{id}")
public ResponseEntity<String> deleteBoard(@PathVariable int id) {
boolean isDeleted = boardService.deleteBoard(id);
if (isDeleted) {
return ResponseEntity.ok("Board deleted successfully");
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Board not found with id: " + id);
}
}
}
@Getter
@Setter
public class BoardEntity {
int id;
String userName;
String title;
String content;
LocalDate nowDay;
int viewCount;
}
BoardMapper를 아래와 같이 작성하였습니다.
@Mapper
public interface BoardMapper {
List<BoardEntity> getAllBoards();
BoardEntity getBoardById(int id);
void createBoard(BoardEntity board);
void updateBoard(BoardEntity board);
void deleteBoard(int id);
}
- BoardServiece를 아래와 같이 작성하였습니다.
@Service @RequiredArgsConstructor public class BoardService {
private final BoardMapper boardMapper;
public List<BoardEntity> getAllBoards() {
return boardMapper.getAllBoards();
}
public BoardEntity getBoardById(int id) {
return boardMapper.getBoardById(id);
}
public BoardEntity createBoard(BoardEntity board) {
try {
boardMapper.createBoard(board);
return board;
} catch (Exception e) {
// 실패 시 예외 처리
throw new RuntimeException("보드 생성에 실패하였습니다.", e);
}
}
public boolean updateBoard(int id, BoardEntity board) {
try {
BoardEntity existingBoard = boardMapper.getBoardById(id);
if (existingBoard != null) {
board.setId(id);
boardMapper.updateBoard(board);
return true;
} else {
// Board not found, update failed
return false;
}
} catch (Exception e) {
// 실패 시 예외 처리
throw new RuntimeException("보드 업데이트에 실패하였습니다.", e);
}
}
public boolean deleteBoard(int id) {
try {
BoardEntity existingBoard = boardMapper.getBoardById(id);
if (existingBoard != null) {
boardMapper.deleteBoard(id);
return true;
} else {
// Board not found, delete failed
return false;
}
} catch (Exception e) {
// 실패 시 예외 처리
throw new RuntimeException("보드 삭제에 실패하였습니다.", e);
}
}
}
- BoardMapper.xml를 아래와 같이 작성하였습니다.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- application.yml을 아래와 같이 작성하였습니다.
server: port: 8084
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/Board?allowPublicKeyRetrieval=true&useSSL=false username: root password: dbzoseh2rl driver-class-name: com.mysql.cj.jdbc.Driver
mybatis: type-aliases-package: com.ldkspringbase.entity mapper-locations: classpath:mapper/*.xml configuration: map-underscore-to-camel-case: 'true'
## u 구현 사진
![image](https://github.com/ParansaikStudy/LDK-SpringBase/assets/91397068/121faefe-9e30-4356-b2eb-390f267e6c41)
![image](https://github.com/ParansaikStudy/LDK-SpringBase/assets/91397068/af711ffb-e7f9-41ee-9124-327ba0cc6075)
## d 구현 사진
![image](https://github.com/ParansaikStudy/LDK-SpringBase/assets/91397068/3fadd9b4-4b10-4f51-9f2d-9b998263ba26)
![image](https://github.com/ParansaikStudy/LDK-SpringBase/assets/91397068/7c4d6207-0b0e-4a9b-bc96-390c32e351f0)
개요