RedisApplicationRunner.java
3.6 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.skua.redis.starter;
import com.skua.core.context.SpringContextUtils;
import com.skua.redis.util.CustomRedisUtil;
import com.skua.tool.javassist.JavassistFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author sonin
* @date 2021/9/30 14:03
*/
@Slf4j
@Component
public class RedisApplicationRunner implements ApplicationRunner {
@Autowired
private CustomRedisUtil customRedisUtil;
@Override
public void run(ApplicationArguments args) {
this.loadSysDepartFunc();
this.createPgClass();
}
/**
* 厂区信息 id => departName 映射
*/
private void loadSysDepartFunc() {
try {
JdbcTemplate masterDB = (JdbcTemplate) SpringContextUtils.getBean("master");
String sysDepartSql = "select id, depart_name from sys_depart";
List<Map<String, Object>> sysDepartMapList = masterDB.queryForList(sysDepartSql);
Map<String, Object> redisMap = new HashMap<>();
for (Map<String, Object> item : sysDepartMapList) {
redisMap.put(item.get("id").toString(), item.get("depart_name"));
}
customRedisUtil.hsets("sys_depart", redisMap);
redisMap.clear();
} catch (Exception e) {
e.printStackTrace();
log.error("init load厂区信息error: {}", e.getMessage());
}
}
private void createPgClass() {
try {
// 创建Xsinsert基础类
JavassistFactory.create()
.className("Xsinsert")
.field("id", Integer.class)
.field("nm", String.class)
.field("v", String.class)
.field("ts", String.class)
.field("createtime", Integer.class)
.field("factoryname", String.class)
.field("devicename", String.class)
.field("type", Integer.class)
.field("gatewaycode", String.class)
.buildClass();
// 创建Count基础类
JavassistFactory.create()
.className("Count")
.field("id", Integer.class)
.field("nm", String.class)
.field("v", String.class)
.field("ts", String.class)
.field("createtime", Integer.class)
.field("factoryname", String.class)
.field("devicename", String.class)
.field("type", String.class)
.field("gatewaycode", String.class)
.buildClass();
// 创建Realtimedata类
JavassistFactory.create()
.className("Realtimedata")
.field("nm", String.class)
.field("v", String.class)
.field("ts", String.class)
.field("createtime", Integer.class)
.field("factoryname", String.class)
.field("devicename", String.class)
.field("type", String.class)
.field("gatewaycode", String.class)
.buildClass();
} catch (Exception e) {
e.printStackTrace();
}
}
}