-
Couldn't load subscription status.
- Fork 164
[Spring MVC] 한성재 미션 제출합니다. #358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: bingle625
Are you sure you want to change the base?
Changes from 16 commits
d5890c7
92c31db
f60ca1f
89c9355
bc14e15
aa53458
e9f1555
dfc3f82
156e220
8963ac0
df35ea5
13e16ce
aa59507
2e8d057
09f939f
4aec219
d7133d8
d41fef9
8b7ffcd
251ca70
e311d68
2662b29
cfc3c26
610e556
2318082
2c735cc
32c827a
d08d455
a7af10e
14bc007
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package jdbctest; | ||
|
|
||
| public class Customer { | ||
| private long id; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 자바엔 long과 Long이 있습니다. 둘은 무엇이 다르고 각각 어떤 장단점이 있나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrapper 클래스의 차이는 null값을 가질 수 있냐, 없냐 로 알고 있는데, 좀더 자세히 알아보고 정리해보겠습니다. long 과 Long 타입의 차이1. long은 원시(Primitive) 타입, Long 타입은 참조(Reference) 타입
추가 조사: heap과 스택자바 프로그램 실행시, JVM(자바가상머신)은 하드웨어로부터 전달받은 메모리를 특정 부분들로 나눈다. 메모리 공간(Runtime Data Area)
자바 변수의 종류
각 변수의 생성 시기 Method(static) 영역
Stack 영역
스택 프레임(stack frame) Heap 영역
Heap과 Stack의 차이점
출처: |
||
| private String firstName; | ||
| private String lastName; | ||
|
|
||
| public Customer(long id, String firstName, String lastName) { | ||
| this.id = id; | ||
| this.firstName = firstName; | ||
| this.lastName = lastName; | ||
| } | ||
|
|
||
| public long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getFirstName() { | ||
| return firstName; | ||
| } | ||
|
|
||
| public String getLastName() { | ||
| return lastName; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package jdbctest; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| public class CustomerController { | ||
| private JdbcTemplate jdbcTemplate; | ||
|
|
||
| public CustomerController(JdbcTemplate jdbcTemplate) { | ||
| this.jdbcTemplate = jdbcTemplate; | ||
| } | ||
|
|
||
| @PostMapping("/customers") | ||
| public ResponseEntity<Void> save(@RequestBody Customer customer) { | ||
| String sql = "INSERT INTO customers(first_name, last_name) VALUES (?,?)"; | ||
| jdbcTemplate.update(sql, customer.getFirstName(), customer.getLastName()); | ||
| return ResponseEntity.ok().build(); | ||
| } | ||
|
|
||
| @GetMapping("/customers") | ||
| public ResponseEntity<List<Customer>> list() { | ||
| String sql = "select id, first_name, last_name from customers"; | ||
| List<Customer> customers = jdbcTemplate.query( | ||
| sql, (resultSet, rowNum) -> { | ||
| Customer customer = new Customer( | ||
| resultSet.getLong("id"), | ||
| resultSet.getString("first_name"), | ||
| resultSet.getString("last_name") | ||
| ); | ||
| return customer; | ||
| }); | ||
| return ResponseEntity.ok().body(customers); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package jdbctest; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.CommandLineRunner; | ||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
|
|
||
| @SpringBootApplication | ||
| public class DemoApplication implements CommandLineRunner { | ||
|
|
||
| public static void main(String args[]) { | ||
| SpringApplication.run(DemoApplication.class, args); | ||
| } | ||
|
|
||
| @Autowired | ||
| JdbcTemplate jdbcTemplate; | ||
|
|
||
| @Override | ||
| public void run(String... strings) throws Exception { | ||
|
|
||
| jdbcTemplate.execute("DROP TABLE customers IF EXISTS"); | ||
| jdbcTemplate.execute("CREATE TABLE customers(id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))"); | ||
|
|
||
| List<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream() | ||
| .map(name -> name.split(" ")) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| jdbcTemplate.batchUpdate("INSERT INTO customers(first_name, last_name) VALUES (?,?)", splitUpNames); | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package roomescape; | ||
|
|
||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
|
|
||
| @Controller | ||
| public class HomeController { | ||
|
|
||
| @GetMapping("/") | ||
| public String index() { | ||
| return "home"; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package roomescape; | ||
|
|
||
| public class Reservation { | ||
|
|
||
| private Long id; | ||
| private String name; | ||
| private String date; | ||
| private String time; | ||
|
|
||
| public Reservation(final Long id, final String name, final String date, final String time) { | ||
| this.id = id; | ||
| this.name = name; | ||
| this.date = date; | ||
| this.time = time; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getTime() { | ||
| return time; | ||
| } | ||
|
|
||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public String getDate() { | ||
| return date; | ||
| } | ||
|
|
||
| public static Reservation toEntity(Reservation reservation, Long id) { | ||
| return new Reservation(id, reservation.name, reservation.date, reservation.time); | ||
| } | ||
|
|
||
| public void setId(final Long id) { | ||
| this.id = id; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package roomescape; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.jdbc.core.simple.SimpleJdbcInsert; | ||
| 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.RequestBody; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import roomescape.exceptions.BadRequestException; | ||
|
|
||
| @RestController | ||
| public class ReservationApiController { | ||
|
|
||
| @Autowired | ||
| private JdbcTemplate jdbcTemplate; | ||
|
|
||
| @GetMapping("/reservations") | ||
| public ResponseEntity<List<Reservation>> getReservations() { | ||
| List<Reservation> reservations = jdbcTemplate.query("select * from reservation", | ||
| ((rs, rowNum) -> new Reservation(rs.getLong("id"), rs.getString("name"), | ||
| rs.getString("date"), rs.getString("time")))); | ||
| return ResponseEntity.ok().body(reservations); | ||
| } | ||
|
|
||
| @PostMapping("/reservations") | ||
| public ResponseEntity<Reservation> create(@RequestBody Reservation reservation) { | ||
|
||
| if (Objects.isNull(reservation.getDate()) || reservation.getDate().isEmpty() || | ||
|
||
| Objects.isNull(reservation.getTime()) || reservation.getTime().isEmpty() || | ||
| Objects.isNull(reservation.getName()) || reservation.getName().isEmpty()) { | ||
| throw new BadRequestException("올바르지 않은 입력입니다."); | ||
| } | ||
|
|
||
| SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate).withTableName( | ||
|
||
| "reservation").usingGeneratedKeyColumns("id"); | ||
|
|
||
| Map<String, Object> map = new HashMap<>(); | ||
|
||
| map.put("name", reservation.getName()); | ||
| map.put("date", reservation.getDate()); | ||
| map.put("time", reservation.getTime()); | ||
| Number newId = simpleJdbcInsert.executeAndReturnKey(map); | ||
| reservation.setId(newId.longValue()); | ||
|
|
||
| return ResponseEntity.created(URI.create("/reservations/" + newId.longValue())) | ||
| .body(reservation); | ||
| } | ||
|
|
||
| @DeleteMapping("/reservations/{id}") | ||
| public ResponseEntity<Void> delete(@PathVariable Long id) { | ||
| int count = this.jdbcTemplate.queryForObject( | ||
|
||
| "select count(*) from reservation where id = ?", new Object[]{id}, int.class); | ||
|
|
||
| if (count == 0) { | ||
| throw new BadRequestException("존재하지 않는 예약입니다."); | ||
| } | ||
|
|
||
| this.jdbcTemplate.update("delete from reservation where id = ?", id); | ||
|
||
|
|
||
| return ResponseEntity.noContent().build(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package roomescape; | ||
|
|
||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
|
|
||
| @Controller | ||
| public class ReservationController { | ||
|
|
||
| @GetMapping("/reservation") | ||
| public String index() { | ||
| return "reservation"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package roomescape.exceptions; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
|
||
| @ResponseStatus(HttpStatus.BAD_REQUEST) | ||
| public class BadRequestException extends RuntimeException{ | ||
|
|
||
| public BadRequestException(String message) { | ||
| super(message); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # h2-console ??? ?? | ||
| spring.h2.console.enabled=true | ||
| # db url | ||
| spring.datasource.url=jdbc:h2:mem:database |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| DROP TABLE IF EXISTS reservation; | ||
|
|
||
| CREATE TABLE reservation | ||
| ( | ||
| id BIGINT NOT NULL AUTO_INCREMENT, | ||
| name VARCHAR(255) NOT NULL, | ||
| date VARCHAR(255) NOT NULL, | ||
| time VARCHAR(255) NOT NULL, | ||
| PRIMARY KEY (id) | ||
| ); |



There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
타임리프를 추가하셨군요! 이번 미션에선 사용 안한 것 같은데, 어떤 이유로 쓰셨나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
테스트하다가 지우지 않은 흔적으로 보입니다..;