Search
Duplicate

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
볡사