@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);
}
}
}
BoardEntity를 아래와 같이 작성하였습니다.
@Getter
@Setter
public class BoardEntity {
int id;
String userName;
String title;
String content;
LocalDate nowDay;
int viewCount;
}
## 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)
개요
코드 부분
BoardController를 아래와 같이 작성하였습니다.
BoardMapper를 아래와 같이 작성하였습니다.
}
@Service @RequiredArgsConstructor public class BoardService {
}
<?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">
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'