1
zj
2024-06-13 8eea5be3b36875bd4ffe70e6c3a5bb07b1d829bf
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.yami.trading.service.data;
 
 
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
 
import javax.annotation.PostConstruct;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Service
public class ItemTypeTimezoneService {
 
    @Autowired
    private JdbcTemplate jdbcTemplate;
 
 
    @Getter
    private  Map<String, String> itemTypeTimezoneMap;
 
    @PostConstruct
    public void init(){
        itemTypeTimezoneMap = list();
    }
 
 
    public Map<String, String> list() {
        String sql = "SELECT item_type, timezone FROM t_item_type_timezone";
 
        List<ItemTypeTimezone> resultList = jdbcTemplate.query(sql, new ItemTypeTimezoneRowMapper());
 
        // 使用Java 8 Stream将结果列表转换为Map
        itemTypeTimezoneMap = resultList.stream()
                .collect(Collectors.toMap(ItemTypeTimezone::getItemType, ItemTypeTimezone::getTimezone));
 
        return itemTypeTimezoneMap;
    }
 
    private static class ItemTypeTimezoneRowMapper implements RowMapper<ItemTypeTimezone> {
        @Override
        public ItemTypeTimezone mapRow(ResultSet resultSet, int rowNum) throws SQLException {
            ItemTypeTimezone itemTypeTimezone = new ItemTypeTimezone();
            itemTypeTimezone.setItemType(resultSet.getString("item_type"));
            itemTypeTimezone.setTimezone(resultSet.getString("timezone"));
            return itemTypeTimezone;
        }
    }
}