

Artifact와 Name을 board-service라고 지어주자. Package name을 Java 컨벤션에 맞게 boardservice라고 지어주자.Spring Boot DevTools, Spring Web, MySQL Driver, Spring Data JPA를 선택하자. 이 프로젝트에서는application.properties를 지우고application.yml을 생성했다.
server: port: 8081 spring: datasource: url: jdbc:mysql://localhost:3307/board-db # DB 주소 username: root # DB 계정 password: password # DB 비밀번호 driver-class-name: com.mysql.cj.jdbc.Driver jpa: hibernate: ddl-auto: update show-sql: true

@Entity @Table(name = "boards") public class Board { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long boardId; private String title; private String content; private Long userId; // FK 설정 안하고 그냥 컬럼으로 선언 public Board() { } public Board(String title, String content, Long userId) { this.title = title; this.content = content; this.userId = userId; } public Long getBoardId() { return boardId; } public String getTitle() { return title; } public String getContent() { return content; } public Long getUserId() { return userId; } }
public interface BoardRepository extends JpaRepository<Board, Long> { }
@RestController @RequestMapping("/boards") public class BoardController { private final BoardService boardService; public BoardController(BoardService boardService) { this.boardService = boardService; } @PostMapping public ResponseEntity<Void> create( @RequestBody CreateBoardRequestDto createBoardRequestDto ) { boardService.create(createBoardRequestDto); return ResponseEntity.noContent().build(); } }
public class CreateBoardRequestDto { private String title; private String content; private Long userId; public String getTitle() { return title; } public String getContent() { return content; } public Long getUserId() { return userId; } }
@Service public class BoardService { private final BoardRepository boardRepository; public BoardService(BoardRepository boardRepository) { this.boardRepository = boardRepository; } @Transactional public void create(CreateBoardRequestDto createBoardRequestDto) { Board board = new Board( createBoardRequestDto.getTitle(), createBoardRequestDto.getContent(), createBoardRequestDto.getUserId() ); this.boardRepository.save(board); } }


