본문으로 바로가기

jpa로 mysql을 쓸 때 mapping을 아래와 같이 하였다.


timestamp로 mapping하기

@Column(nullable = false, name = "TIME_TO_12HOURS", 
        columnDefinition = "TIMESTAMP(3)")
private LocalDateTime timeTo12Hours;


decimal로 mapping하기

@Column(nullable = false, precision = 36, scale = 18)
private BigDecimal open;

BigDecimal은 그냥 mapping하면 decimal로 mapping된다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class OhlcvMaker {
    public Ohlcv make(String ohlcvString) {
        // String -> LocalDateTime
        // String -> BigDecimal
        
        String[] arr = ohlcvString.split(",");
        // 0 currency_pair, time_to_mins, close, high(decimal), low, open
        // 6 time_to_12hours(timestamp), time_to_15mins, time_to_30mins, time_to_5mins, time_to_6hours
        // time_to_days, time_to_hours, time_to_months, time_to_weeks, updated_at
        // mapping은 array 순서대로 했다. 빠뜨릴까봐.
        Ohlcv ohlcv = Ohlcv.builder()
                .currencyPair(arr[0])
                .timeToMins(stringToLocalDateTime(arr[1]))
                .close(new BigDecimal(arr[2]))
                .high(new BigDecimal(arr[3]))
                .low(new BigDecimal(arr[4]))
                .open(new BigDecimal(arr[5]))
                .timeTo12Hours(stringToLocalDateTime(arr[6]))
                .timeTo15Mins(stringToLocalDateTime(arr[7]))
                .timeTo30Mins(stringToLocalDateTime(arr[8]))
                .timeTo5Mins(stringToLocalDateTime(arr[9]))
                .timeTo6Hours(stringToLocalDateTime(arr[10]))
                .timeToDays(stringToLocalDateTime(arr[11]))
                .timeToHours(stringToLocalDateTime(arr[12]))
                .timeToWeeks(stringToLocalDateTime(arr[13]))
                .build();
 
        return ohlcv;
    }
 
    private LocalDateTime stringToLocalDateTime(String string) {
        LocalDateTime timeToMins = LocalDateTime.parse(string, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return timeToMins;
    }
}
cs



댓글을 달아 주세요