System.out.println("부부개발단님의 강의를 참고하였습니다.");
1. 프로젝트 생성
https://start.spring.io
2. docker 에 mysql 설치
아래 url 참고해주세용
MySQL 설치 (docker 사용, m1 Macbook Air) (tistory.com)
MySQL 설치 (docker 사용, m1 Macbook Air)
System.out.println("부부개발단님의 강의를 참고하였습니다."); 1. docker-desktop 다운로드 (저는 2020 m1 Macbook Air를 사용중입니다. -=> Download for Mac - Apple Chip) 2. database 디렉토리 생성 3-1. docker-compose.yml 파
dev-tatooine.tistory.com
3. application.yml 파일 생성 후 db 관련 정보 넣어주기
4. 웹에서 입력 받아 DB 에 저장하기
@PostMapping 사용
TodoController.java
package com.example.todoapp.controller;
import com.example.todoapp.domain.Todo;
import com.example.todoapp.repository.ToDoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller
@RequiredArgsConstructor
public class TodoController {
private final ToDoRepository toDoRepository;
@PostMapping("/addTodo")
public String addTodo(@RequestParam("todo") String todo) {
// System.out.println(todo);
//db에 저장하자
Todo toDo = new Todo();
toDo.setTodo(todo);
toDoRepository.save(toDo);
return "redirect:/";
}
}
todos.html
<html lang="ko"
xmlns:th=":http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<h1>Todo App</h1>
<form method="post" action="/addTodo">
<input type="text" name="todo" />
<input type="submit" value="Add Todo" />
</form>
<hr>
</html>
Todo.java
import javax.persistence.*;
@Entity(name = "ToDo")
@Table(name = "todo")
@AllArgsConstructor //모든 파라미터를 갖는 생성자 구현... from lombok
@NoArgsConstructor //기본 생성자 생성 구현... from lombok
@Getter
@Setter
public class Todo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String todo;
}
ToDoRepository.java
package com.example.todoapp.repository;
import com.example.todoapp.domain.Todo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import javax.persistence.Entity;
@Repository
public interface ToDoRepository extends JpaRepository<Todo, Long> {
}
/*
@Repository 애노테이션이 자동으로 구현해준다.
*/
5. 정보 웹에 출력하기
@GepMapping 으로 가져오면 됨.
package com.example.todoapp.controller;
import com.example.todoapp.domain.Todo;
import com.example.todoapp.repository.ToDoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@Controller
@RequiredArgsConstructor
public class TodoController {
private final ToDoRepository toDoRepository;
@GetMapping("/")
public String index(Model model) {
List<Todo> todos = toDoRepository.findAll();
model.addAttribute("todos", todos);
return "todos";
}
@PostMapping("/addTodo")
public String addTodo(@RequestParam("todo") String todo) {
// System.out.println(todo);
//db에 저장하자
Todo toDo = new Todo();
toDo.setTodo(todo);
toDoRepository.save(toDo);
return "redirect:/";
}
}
[출처]
1시간안에 SpringBoot, JPA를 이용해 ToDo App만들기 - YouTube
'spring' 카테고리의 다른 글
[Spring] mvc 패턴 (0) | 2023.09.23 |
---|---|
[spring] http 요청 메시지 (0) | 2023.09.05 |
[spring] 컴포넌트 스캔 (스프링 핵심 원리-기본편 from 김영한 강사님) (0) | 2023.08.29 |
[spring] spring 조회 (스프링 핵심 원리-기본편 from 김영한 강사님) (0) | 2023.08.23 |
[spring] DI(의존 관계 주입) (스프링 핵심 원리-기본편 from 김영한 강사님) (0) | 2023.08.23 |