Search

To Do List ํ”„๋กœ์ ํŠธ (Back)

To Do List ํ”„๋กœ์ ํŠธ

โ€œTodo List - ํ•  ์ผ ๋ชฉ๋ก UI ๋งŒ๋“ค๊ธฐโ€

์Šคํƒ€ ์ข€ ๋ˆŒ๋Ÿฌ์ฃผ์„ธ์š”

๋ฐฑ์—”๋“œ (SpringBoot)

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

todo ํ…Œ์ด๋ธ”
CREATE TABLE `todo` ( `no` int NOT NULL AUTO_INCREMENT, `name` text NOT NULL, `status` int DEFAULT '0', `reg_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `upd_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`no`) ) COMMENT='ํ• ์ผ';
SQL
๋ณต์‚ฌ

ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

โ€ข
๐Ÿ–ง Server
โ—ฆ
java
โ–ช
controller
โ€ข
TodoController.java
โ–ช
dto
โ€ข
Todo.java
โ–ช
service
โ€ข
TodoService.java
โ€ข
TodoServiceImpl.java
โ–ช
mapper
โ€ข
TodoMapper.java
โ—ฆ
resources
โ–ช
main-package/mapper
โ€ข
TodoMapper.xml
โ–ช
application.properties
โ–ช
mybatis-config.xml
โ—ฆ
build.gradle

์ฝ”๋“œ ์ž‘์—…

1.
build.gradle
2.
application.properties
3.
mybatis-config.xml
4.
main-package/mapper
โ€ข
TodoMapper.xml
5.
mapper
โ€ข
TodoMapper.java
6.
dto
โ€ข
Todo.java
7.
service
โ€ข
TodoService.java
โ€ข
TodoServiceImpl.java
8.
controller
โ€ข
TodoController.java

build.gradle

plugins { id 'java' id 'war' id 'org.springframework.boot' version '3.1.6' id 'io.spring.dependency-management' version '1.1.4' } group = 'com.joeun' version = '0.0.1-SNAPSHOT' java { sourceCompatibility = '17' } configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.3' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.3' } tasks.named('bootBuildImage') { builder = 'paketobuildpacks/builder-jammy-base:latest' } tasks.named('test') { useJUnitPlatform() }
Plain Text
๋ณต์‚ฌ

application.properties

# ๋ฐ์ดํ„ฐ ์†Œ์Šค - MySQL spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/joeun?serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true&useSSL=false&autoReconnection=true&autoReconnection=true spring.datasource.username=joeun spring.datasource.password=123456 # Mybatis ์„ค์ • # Mybatis ์„ค์ • ๊ฒฝ๋กœ : ~/resources/mybatis-config.xml mybatis.config-location=classpath:mybatis-config.xml # Mybatis ๋งคํผ ํŒŒ์ผ ๊ฒฝ๋กœ : ~/๋ฉ”์ธํŒจํ‚ค์ง€/mapper/**Mapper.xml mybatis.mapper-locations=classpath:mybatis/mapper/**/**.xml
Plain Text
๋ณต์‚ฌ

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- ์„ค์ • --> <settings> <!-- ์–ธ๋”์Šค์ฝ”์–ด ์ผ€์ด์Šค์ธ ์ปฌ๋Ÿผ์„ ์นด๋ฉœ ์ผ€์ด์Šค๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์„ค์ • --> <!-- board_no - boardNo --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- ํƒ€์ž… ๋ณ„์นญ ์„ค์ • --> <typeAliases> <!-- ํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘ํ•  DTO๊ฐ€ ์žˆ๋Š” ํŒจํ‚ค์ง€ ๊ฒฝ๋กœ ์ง€์ • --> <package name="com.joeun.todo.dto"/> </typeAliases> </configuration>
XML
๋ณต์‚ฌ

main-package/mapper

โ€ข
TodoMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace="๋งคํผ ์ธํ„ฐํŽ˜์ด์Šค ๊ฒฝ๋กœ" --> <mapper namespace="com.joeun.todo.mapper.TodoMapper"> <!-- ํ• ์ผ ๋ชฉ๋ก --> <select id="list" resultType="Todo"> SELECT * FROM todo ORDER BY status ASC, no DESC </select> <!-- ํ• ์ผ ์กฐํšŒ --> <select id="select" resultType="Todo"> SELECT * FROM todo WHERE no = #{no} </select> <!-- ํ• ์ผ ๋“ฑ๋ก --> <insert id="insert"> INSERT INTO todo( name ) VALUES ( #{name} ) </insert> <!-- ํ• ์ผ ์ˆ˜์ • --> <update id="update"> UPDATE todo SET name = #{name} ,status = #{status} ,upd_date = now() WHERE no = #{no} </update> <!-- ํ• ์ผ ์‚ญ์ œ --> <delete id="delete"> DELETE FROM todo WHERE no = #{no} </delete> <!-- last id --> <select id="lastId" resultType="int"> select last_insert_id() id </select> <!-- ์ „์ฒด ํ• ์ผ ์™„๋ฃŒ --> <update id="completeAll"> UPDATE todo SET status = 1 ,upd_date = now() </update> <!-- ์ „์ฒด ํ• ์ผ ์‚ญ์ œ --> <delete id="deleteAll"> DELETE FROM todo </delete> </mapper>
XML
๋ณต์‚ฌ

mapper

โ€ข
TodoMapper.java
package com.joeun.todo.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.joeun.todo.dto.Todo; @Mapper public interface TodoMapper { public List<Todo> list() throws Exception; public Todo select(int no) throws Exception; public int insert(Todo todo) throws Exception; public int update(Todo todo) throws Exception; public int delete(int no) throws Exception; public int lastId() throws Exception; public int completeAll() throws Exception; public int deleteAll() throws Exception; }
Java
๋ณต์‚ฌ

dto

โ€ข
Todo.java
package com.joeun.todo.service; import java.util.List; import com.joeun.todo.dto.Todo; public interface TodoService { public List<Todo> list() throws Exception; public Todo select(int no) throws Exception; public int insert(Todo todo) throws Exception; public int update(Todo todo) throws Exception; public int delete(int no) throws Exception; public int lastId() throws Exception; public int completeAll() throws Exception; public int deleteAll() throws Exception; }
Java
๋ณต์‚ฌ

service

โ€ข
TodoService.java
package com.joeun.todo.service; import java.util.List; import com.joeun.todo.dto.Todo; public interface TodoService { public List<Todo> list() throws Exception; public Todo select(int no) throws Exception; public int insert(Todo todo) throws Exception; public int update(Todo todo) throws Exception; public int delete(int no) throws Exception; public int lastId() throws Exception; public int completeAll() throws Exception; public int deleteAll() throws Exception; }
Java
๋ณต์‚ฌ
โ€ข
TodoServiceImpl.java
package com.joeun.todo.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.joeun.todo.dto.Todo; import com.joeun.todo.mapper.TodoMapper; @Service public class TodoServiceImpl implements TodoService { @Autowired private TodoMapper todoMapper; @Override public List<Todo> list() throws Exception { return todoMapper.list(); } @Override public Todo select(int no) throws Exception { return todoMapper.select(no); } @Override public int insert(Todo todo) throws Exception { int result = todoMapper.insert(todo); if( result > 0 ) result = todoMapper.lastId(); return result; } @Override public int update(Todo todo) throws Exception { return todoMapper.update(todo); } @Override public int delete(int no) throws Exception { return todoMapper.delete(no); } @Override public int lastId() throws Exception { return todoMapper.lastId(); } @Override public int completeAll() throws Exception { return todoMapper.completeAll(); } @Override public int deleteAll() throws Exception { return todoMapper.deleteAll(); } }
Java
๋ณต์‚ฌ

controller

โ€ข
TodoController.java

[Extension] Spring Code Generator

๊ฟ€ํŒ : sp-crud
package com.joeun.todo.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.joeun.todo.dto.Todo; import com.joeun.todo.service.TodoService; import lombok.extern.slf4j.Slf4j; @Slf4j @RestController @CrossOrigin(origins = "*") // cors ํ—ˆ์šฉ @RequestMapping("/todos") public class TodoController { @Autowired private TodoService todoService; @GetMapping() public ResponseEntity<?> getAll() { log.info("list..."); try { List<Todo> todoList = todoService.list(); log.info("ํ•  ์ผ ๊ฐœ์ˆ˜ : " + todoList.size()); return new ResponseEntity<>(todoList, HttpStatus.OK); } catch (Exception e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @GetMapping("/{id}") public ResponseEntity<?> getOne(@PathVariable Integer id) { log.info("select..."); log.info("id : " + id); try { Todo todo = todoService.select(id); log.info("todo : " + todo); return new ResponseEntity<>(todo, HttpStatus.OK); } catch (Exception e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @PostMapping() public ResponseEntity<?> create(@RequestBody Todo todo) { log.info("insert..."); try { int result = todoService.insert(todo); // ์ƒˆ๋กœ ์ƒ์„ฑ๋œ no ๋ฅผ ์‘๋‹ต todo.setNo(result); log.info("result : " + result); if( result > 0 ) return new ResponseEntity<>(todo, HttpStatus.CREATED); else return new ResponseEntity<>(todo, HttpStatus.OK); } catch (Exception e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @PutMapping() public ResponseEntity<?> update(@RequestBody Todo todo) { log.info("update..."); log.info(todo.toString()); try { int result = 0; // ์ „์ฒด ์™„๋ฃŒ if( todo.getNo() == -1 ) { result = todoService.completeAll(); } else { result = todoService.update(todo); } if( result > 0 ) return new ResponseEntity<>("Update Result", HttpStatus.OK); else return new ResponseEntity<>("No Result", HttpStatus.OK); } catch (Exception e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } @DeleteMapping("/{id}") public ResponseEntity<?> destroy(@PathVariable Integer id) { log.info("delete..."); log.info("id : " + id); try { int result = 0; // ์ „์ฒด ์‚ญ์ œ if( id == -1 ) { result = todoService.deleteAll(); } else { result = todoService.delete(id); } if( result > 0 ) return new ResponseEntity<>("Destroy Result", HttpStatus.OK); else return new ResponseEntity<>("No Result", HttpStatus.OK); } catch (Exception e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } } }
Java
๋ณต์‚ฌ

ํ…Œ์ŠคํŠธ

๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก [GET]
http://localhost:8080/boards
Plain Text
๋ณต์‚ฌ
๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ [GET]
http://localhost:8080/boards/1
Plain Text
๋ณต์‚ฌ
๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก [POST]
http://localhost:8080/boards
Plain Text
๋ณต์‚ฌ
๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • [PUT]
http://localhost:8080/boards
Plain Text
๋ณต์‚ฌ
๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ [DELETE]
http://localhost:8080/boards/1
Plain Text
๋ณต์‚ฌ