2swan

React DB 연결(Mybatis) 본문

Programming/React

React DB 연결(Mybatis)

2swan 2023. 9. 11. 15:46

BoardController

@RestController
public class BoardController {

	@Autowired
	private BoardService boardService;
	
	//추가
//	@PostMapping("insert")
//	public void insert(@RequestBody Board board) {
//		boardService.insert(board);
//	}
	
	//추가(입력 2 방법 때문에 이렇게 수정)
	@PostMapping("insert")
	public Board insert(@RequestBody Board board) {
		boardService.insert(board);
		return board;
	}
	
	
	//전체보기
	@GetMapping("list")
	public List<Board>list(){
		return boardService.list();
	}
	
	//삭제
	@DeleteMapping("delete/{num}")
	public void delete(@PathVariable Long num) {
		boardService.delete(num);
	}
	
}

 

Board

@Getter @Setter
public class Board {
	private Long num;
	private String title;
	private String content;
}

 

BoardMapper

@Mapper
public interface BoardMapper {

	//게시판 글쓰기
	@Insert("insert into board(title, content) values(#{title}, #{content})")
	@Options(useGeneratedKeys = true, keyProperty = "num")
	public void insert(Board board);
	

	//게시판 전체보기
	@Select("select * from board")
	public List<Board>lists();
	
	//삭제
	@Delete("delete from board where num=#{num}")
	public Long delete(Long num);
}

 

BoardService

@Service
public class BoardService {

	@Autowired
	private BoardMapper boardMapper;

	//추가
	public void insert(Board board) {
		boardMapper.insert(board);
	}
	
	
	//전체보기
	public List<Board>list(){
		return boardMapper.lists();
	}
	
	//삭제
	public void delete(Long num) {
		boardMapper.delete(num);
	}
}

 

'Programming > React' 카테고리의 다른 글

React Router 예제(함수 방식)  (0) 2023.09.12
React Router 예제(Class 방식)  (0) 2023.09.12
React 추가, 삭제(Mybatis)  (0) 2023.09.11
React DB 연결(JPA)  (0) 2023.09.11
React 추가, 삭제(JPA)  (0) 2023.09.11