Spring Boot 예제: 데이터베이스 입출력(CRUD)이 가능한 RestController 제작
프로젝트 구조
1. Spring Boot 프로젝트 생성
2. application.properties에 데이터베이스 연결 (mariadb)
3. VO(Value Object; DTO) 작성 (Todo.java)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.springboot.simpletodo.vo;
public class Todo {
private int id;
private String icon, title, detail, regDate, modDate;
public Todo() {
super();
}
public Todo(int id, String icon, String title, String detail, String regDate, String modDate) {
super();
this.id = id;
this.icon = icon;
this.title = title;
this.detail = detail;
this.regDate = regDate;
this.modDate = modDate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getRegDate() {
return regDate;
}
public void setRegDate(String regDate) {
this.regDate = regDate;
}
public String getModDate() {
return modDate;
}
public void setModDate(String modDate) {
this.modDate = modDate;
}
@Override
public String toString() {
return "Todo [id=" + id + ", icon=" + icon + ", title=" + title + ", detail=" + detail + ", regDate=" + regDate
+ ", modDate=" + modDate + "]";
}
}
4. DAO 작성 (SimpleTodoDAO.java)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.springboot.simpletodo.dao;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.springboot.simpletodo.vo.Todo;
@Repository
public class SimpleTodoDAO {
@Autowired JdbcTemplate jt;
public List<Todo> selectAll() {
return jt.query("select * from simple_todo", (rs, rowNum) -> {
Todo aTodo = new Todo();
aTodo.setId(rs.getInt("id"));
aTodo.setIcon(rs.getString("icon"));
aTodo.setTitle(rs.getString("title"));
aTodo.setDetail(rs.getString("detail"));
aTodo.setRegDate(rs.getString("reg_date"));
aTodo.setModDate(rs.getString("mod_date"));
return aTodo;
});
}
public int insertTodo(Todo aTodo) {
return jt.update("insert into simple_todo(id, icon, title, detail, reg_date, mod_date) "
+ "values (0, ?, ?, ?, sysdate(), sysdate())", aTodo.getIcon(), aTodo.getTitle(), aTodo.getDetail());
}
public int updateTodo(Todo aTodo) {
return jt.update("update simple_todo set icon = ?, title = ?, detail = ?, mod_date = sysdate() where id = ?",
aTodo.getIcon(), aTodo.getTitle(), aTodo.getDetail(), aTodo.getId());
}
public int deleteTodo(Todo aTodo) {
return jt.update("delete from simple_todo where id = ?", aTodo.getId());
}
}
5. 서비스 인터페이스 작성 (SimpleTodoService.java)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.springboot.simpletodo.service;
import java.util.List;
import com.springboot.simpletodo.vo.Todo;
public interface SimpleTodoService {
public List<Todo> selectTodo();
public int insertTodo(Todo aTodo);
public int updateTodo(Todo aTodo);
public int deleteTodo(Todo aTodo);
}
6. 서비스 인터페이스 구현 (SimpleTodoServiceImpl.java)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.springboot.simpletodo.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springboot.simpletodo.dao.SimpleTodoDAO;
import com.springboot.simpletodo.service.SimpleTodoService;
import com.springboot.simpletodo.vo.Todo;
@Service
public class SimpleTodoServiceImpl implements SimpleTodoService{
@Autowired SimpleTodoDAO std;
@Override
public List<Todo> selectTodo() {
return std.selectAll();
}
@Override
public int insertTodo(Todo aTodo) {
return std.insertTodo(aTodo);
}
@Override
public int updateTodo(Todo aTodo) {
return std.updateTodo(aTodo);
}
@Override
public int deleteTodo(Todo aTodo) {
return std.deleteTodo(aTodo);
}
}
7. RestController 작성 (SimpleTodoController.java)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springboot.simpletodo.service.SimpleTodoService;
import com.springboot.simpletodo.vo.Todo;
@RestController
public class SimpleTodoController {
@Autowired SimpleTodoService stc;
@GetMapping("/get-test")
public String getTest() {
return "get test";
}
@GetMapping("/todo/get")
public List<Todo> getList() {
return stc.selectTodo();
}
@PostMapping("/todo/insert")
public int insertTodo(Todo aTodo) {
return stc.insertTodo(aTodo);
}
@PostMapping("/todo/update")
public int updateTodo(Todo aTodo) {
return stc.updateTodo(aTodo);
}
@PostMapping("/todo/delete")
public int deleteTodo(Todo aTodo) {
return stc.deleteTodo(aTodo);
}
}
8. RestController의 정상 동작 여부를 점검하는 테스트 페이지 작성
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.springboot.simpletodo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class WebController {
@RequestMapping("/")
public String mainPage() {
return "main-web";
}
}
This post is licensed under
CC BY 4.0
by the author.

