Bibi's DevLog ๐Ÿค“๐ŸŽ

[Java Spring] DB์— ๋‚ ์งœ ์ €์žฅํ•˜๊ธฐ, JSON์œผ๋กœ ๋‚ ์งœ ๋ฐ›๊ธฐ ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/Spring ์Šคํ”„๋ง

[Java Spring] DB์— ๋‚ ์งœ ์ €์žฅํ•˜๊ธฐ, JSON์œผ๋กœ ๋‚ ์งœ ๋ฐ›๊ธฐ

๋น„๋น„ bibi 2021. 5. 28. 22:17

[Java] DB์— LocalDate์ž๋ฃŒํ˜• ์ €์žฅํ•˜๊ณ  ๊บผ๋‚ด๊ธฐ

https://kouzie.github.io/jdbc/JDBC.-2%EC%9D%BC%EC%B0%A8/#javasqlstatement-%EB%A1%9C-select%ED%95%98%EA%B8%B0

DB ํ…Œ์ด๋ธ”

๋‚ ์งœ ์ž๋ฃŒํ˜•์ธ Date๋กœ ์ €์žฅํ•œ๋‹ค

create table `booking`
(
    `id`               int         not null auto_increment,
    `room_id`          int         not null,
    `user_id`          int         not null,
    `check_in`         date not null,
    `check_out`        date not null,
    `number_of_people` int         not null,
    `total_price`      int         not null,
    primary key (`id`),
    foreign key (`room_id`) references `room` (`id`),
    foreign key (`user_id`) references `user` (`id`)
);

Java ์—”ํ‹ฐํ‹ฐ

๋‚ ์งœ ์ž๋ฃŒํ˜•์ธ LocalDate๋กœ ์ €์žฅํ•œ๋‹ค

public class Booking {

    @Id
    private Long id;

    private Long roomId;
    private Long userId;
    private LocalDate checkIn;
    private LocalDate checkOut;
    private int numberOfPeople;
    private int totalPrice;

    //...
}

๊บผ๋‚ด๊ธฐ

(JdbcTemplate ๊ธฐ์ค€)

resultSet.getDate("์ด๋ฆ„").toLocalDate()

resultSet.getDate()์˜ ๋ฆฌํ„ด๊ฐ’์€ java.util.Date๊ฐ€ ์•„๋‹Œ java.sql.Date์ด๋‹ค. ๊ทธ๋ž˜์„œ toLocalDate()๋ฅผ ์‚ฌ์šฉํ•ด LocalDate๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์ด ํ•œ ๋ฒˆ ๋” ํ•„์š”ํ•œ ๊ฒƒ.

@Override
    public List<Booking> findAll() {
        String sql = "select `id`, `room_id`, `user_id`, `check_in`, `check_out`, `number_of_people`, `total_price` from booking";
        return jdbcTemplate.query(sql, bookingRowMapper());
    }

private RowMapper<Booking> bookingRowMapper() {
        return (resultSet, rowNum) -> {
            Booking booking = new Booking(resultSet.getLong("id"), resultSet.getLong("room_id"), resultSet.getLong("user_id"),
                    resultSet.getDate("check_in").toLocalDate(), resultSet.getDate("check_out").toLocalDate(), resultSet.getInt("number_of_people"),
                    resultSet.getInt("total_price"));
            return booking;
        };
    }

์ €์žฅํ•˜๊ธฐ

ํŠน๋ณ„ํ•œ ๋ณ€ํ™˜์€ ํ•„์š” ์—†์Œ. JdbcTemplate.update()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

public void insert(Booking booking) {
        String sql = "insert into `booking` (`room_id`, `user_id`, `check_in`, `check_out`, `number_of_people`, `total_price`) values (?, ?, ?, ?, ?, ?)";
        jdbcTemplate.update(sql, booking.getRoomId(), booking.getUserId(), booking.getCheckIn(),
                booking.getCheckOut(), booking.getNumberOfPeople(), booking.getTotalPrice());
    }

[Spring] JSON Response๋กœ ๋‚ ์งœ ์ž๋ฃŒํ˜• ๋ฐ›๊ธฐ - @DateTimeFormat

์ถœ์ฒ˜๐Ÿ™‡โ€โ™‚๏ธ

https://perfectacle.github.io/2018/01/15/jackson-local-date-time-deserialize/

์ž๋ฐ”์˜ LocalDate์™€ ๊ฐ™์€ ๋‚ ์งœ ์ž๋ฃŒํ˜•์„ JSON์œผ๋กœ ๋ฐ›์œผ๋ ค ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ TypeMisMatchException์ด ๋ฐœ์ƒํ•œ๋‹ค.

{
    ...
    "exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
    "message": Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';
...
}

์ด๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ธด ๊ฐ’์„ String์œผ๋กœ ์ธ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

ํ•ด๊ฒฐ์ฑ… : @DateTimeFormat(pattern = "") ๋˜๋Š” @JsonFormat(pattern = "") ์–ด๋…ธํ…Œ์ด์…˜์„ ๋‚ ์งœ ์ž๋ฃŒํ˜• ํŒŒ๋ผ๋ฏธํ„ฐ ๋˜๋Š” ํ•„๋“œ์— ๋‹ฌ์•„ ์ค€๋‹ค.

@RestController
public class Controller {
    @GetMapping("/")
    public DateType get(@RequestParam
                        @DateTimeFormat(pattern = "yyyy-MM-dd")
                        LocalDate date,
                        @RequestParam
                        @DateTimeFormat(pattern = "kk:mm:ss")
                        LocalTime time,
                        @RequestParam
                        @DateTimeFormat(pattern = "yyyy-MM-dd kk:mm:ss")
                        LocalDateTime dateTime) {}
}