- Spring Boot 2.3.4
- Spring Data JPA 2.3.4 (JPA)
- Hibernate 5.4.21 (JPA)
- Lombok (Support)
- JUnit 4 (Test)
- LogBack 1.2.3 (Log)
- MockMvc (Web Test)
- HikariCP 3.4.5 (DB connection pool)
- mysql 8.0
HOST : http://localhost:8080
기능 httpMethod uri C POST /v1/board/create R GET /v1/board/read R GET /v1/board/read/{id} U PUT / POST /v1/board/update D DELETE / POST /v1/board/delete
DDL
use bone CREATE TABLE `board` ( `id` int(11) NOT NULL AUTO_INCREMENT, `contents` varchar(100) NOT NULL, `create_timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `name` varchar(50) NOT NULL, `title` varchar(10) NOT NULL, `update_timestamp` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
BoardController.java @GetMapping("/read/{id}") public ResponseEntity read(@PathVariable("id") long id) throws Exception { Optional boardOptional = service.read(id); Board board = boardOptional.orElseThrow(NotFoundException::new); return new ResponseEntity<>(board, HttpStatus.OK); }
BoardService.java @Modifying @Transactional public Board update(Board request) throws Exception { Optional boardOptional = this.read(request.getId()); Board board = boardOptional.orElseThrow(NotFoundException::new); board.setContents(request.getContents()); board.setTitle(request.getTitle()); return boardRepository.save(board); }
BoardRespository.java @Repository public interface BoardRepository extends JpaRepository { }
BoardControllerTest.java 샘플
@Test @JunitDocument("Board - 게시물 생성") void create() throws Exception { MultiValueMap info = new LinkedMultiValueMap<>(); info.add("title", "title"); info.add("name", "summer"); info.add("contents", "contents"); mockMvc.perform(post("/v1/board/create") .params(info)) .andExpect(status().isOk()) .andDo(print()); }