1. Test 테이블에 데이터를 insert 해보자
TestController.java
- Post방식으로 데이터를 insert 하기위해 아래의 코드를 작성
- Service를 호출한다
@PostMapping(value = "/")
public String insertTest(@RequestBody TestDTO testDTO) throws Exception {
log.debug("insertTest");
return testService.insertTest(testDTO);
}
TestDAO
- DB에 접속하여 비즈니스 로직 실행에 필요한 쿼리를 호출
int insertTest(TestDTO testDTO);
TestService
- 비즈니스 로직 수행
public String insertTest(TestDTO testDTO) throws Exception {
// testDTO db에 insert
int result = testDAO.insertTest(testDTO);
return result + " rows inserted";
}
TestMapper.xml
<insert id="insertTest" parameterType="kr.ac.daegu.springbootapi.test.model.TestDTO">
INSERT INTO Test (id, name) values (#{num}, #{name})
</insert>
postman에서 확인
- DBEAVER에서 id 값은 auto_increment를 사용해 자동 증가하도록 설정했기 때문에 name 값만 전송해도 데이터가 삽입되는 것을 확인할 수 있다
- name에 "name9"라는 값을 전송 성공하면 1 rows inserted라는 문구가 뜬다
'Springboot' 카테고리의 다른 글
2021-10-01 (Springboot - board 테이블 read) (0) | 2021.10.01 |
---|---|
2021-09-30 (Springboot - board 테이블 insert) (0) | 2021.09.30 |
2021-09-28 (Springboot - board 테이블 연결) (0) | 2021.09.29 |
2021-09-28 (Springboot - mybatis) (0) | 2021.09.28 |
2021-09-28 (Springboot - service layer) (0) | 2021.09.28 |