chenjiahe
2022-04-06 e0e1d790b1e9137bb863b202c467d7f2e2e792ba
提交 | 用户 | age
93b505 1 package com.hx.mybatis.aes.springbean;
C 2
3 import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
4 import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
5 import com.hx.common.annotations.MysqlHexAes;
a48817 6 import com.hx.common.dao.mapper.CommonMapper;
93b505 7 import com.hx.common.service.CommonService;
C 8 import com.hx.exception.ServiceException;
9 import com.hx.mybatisTool.SqlSentence;
10 import com.hx.util.StringUtils;
11 import com.hx.util.mysql.aes.MysqlHexAesTool;
12
13 import javax.annotation.PostConstruct;
14 import java.lang.reflect.Field;
b1097d 15 import java.util.*;
f36821 16 import java.util.concurrent.ExecutorService;
C 17 import java.util.concurrent.Executors;
93b505 18
C 19 public class InitMysqlData {
20
21     /**
22      * 项目启动就执行后就执行该方法
23      */
24     @PostConstruct
25     public static void initData(String packPath, CommonService commonService){
26
27         //项目启动的时候填入
28         if(!StringUtils.isEmpty(packPath)){
29             Set<Class<?>> classes = VariableAesKey.classData(packPath);
30
31             SqlSentence sqlSentence = new SqlSentence();
32             Map<String,Object> values = new HashMap<>();
33
b1097d 34             //解析表数据
C 35             List<FieldData> fieldDatas = entityhandle(classes);
36
f36821 37             int pageSize = 500;
C 38             //创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
39             ExecutorService fixedThreadPool = Executors.newFixedThreadPool(20);
40             try{
41                 for(FieldData fieldData:fieldDatas){
42                     //获取条数
43                     sqlSentence.sqlSentence("SELECT COUNT(0) FROM "+fieldData.getTableName(),values);
44                     int total = commonService.selectCountSql(sqlSentence);
45                     if(total ==0 ){
46                         continue;
b1097d 47                     }
f36821 48                     int pageTotal = total/pageSize+1;
C 49                     for(int i= 0;i<pageTotal;i++){
50                         int finalI = i;
51                         fixedThreadPool.execute(new Runnable() {
52                             @Override
53                             public void run() {
54                                 //更新数据
55                                 updateData(fieldData,commonService, finalI,pageSize);
56                             }
57                         });
93b505 58                     }
C 59                 }
f36821 60             }catch (Exception e){
C 61                 e.printStackTrace();
62             }finally {
63                 fixedThreadPool.shutdown();
93b505 64             }
C 65         }
66     }
67
f36821 68     /**更新数据*/
C 69     public static void updateData(FieldData fieldData,CommonService commonService,int pageNum,int pageSize){
70         SqlSentence sqlSentence = new SqlSentence();
71         Map<String,Object> values = new HashMap<>();
72
73         //查询数据
74         StringBuilder selectField = new StringBuilder();
75         selectField.append(fieldData.getId());
76         for(String fieldName:fieldData.getEncrypFields()){
77             selectField.append(","+fieldName);
78         }
79         sqlSentence.sqlSentence("SELECT "+selectField.toString()+" FROM "+fieldData.getTableName()+" LIMIT "+pageNum+","+pageSize,values);
80         List<Map<String,Object>> list = commonService.selectListMap(CommonMapper.class,sqlSentence);
81
e0e1d7 82         boolean isUpdate = false;
f36821 83         for (Map<String,Object> map:list){
e0e1d7 84             isUpdate = false;
f36821 85             StringBuilder setField = new StringBuilder();
C 86             for (Map.Entry<String, Object> entry : map.entrySet()) {
87                 String mapKey = entry.getKey();
88                 String mapValue = (String) entry.getValue();
89                 if(StringUtils.isEmpty(mapValue)){
90                     continue;
91                 }
92                 if(mapValue.length()%32==0 && MysqlHexAesTool.isHexStrValid(mapValue)){
93                     continue;
94                 }
95                 if(fieldData.getId().equals(mapKey)){
96                     continue;
97                 }
98                 if(setField.length()>0){
99                     setField.append(",");
100                 }
101                 setField.append(mapKey+" = #{m."+mapKey+"}");
e0e1d7 102                 isUpdate = true;
f36821 103             }
C 104
e0e1d7 105             if(isUpdate){
C 106                 values = map;
107                 sqlSentence.sqlSentence("UPDATE "+fieldData.getTableName()+" SET "+setField.toString()+" WHERE "+fieldData.getId()+" = #{m."+fieldData.getId()+"}",values);
a48817 108
e0e1d7 109                 if(commonService.updateSentence(sqlSentence)!=1){
C 110                     throw new ServiceException("更新超过1条,更新失败!");
111                 }
f36821 112             }
C 113         }
114
115     }
116
b1097d 117     /**获取到表的数据*/
C 118     public static List<FieldData> entityhandle(Set<Class<?>> classes){
119
120         List<FieldData> fildDatas = new ArrayList<>();
121         //存储单表字段信息
122         FieldData fildData;
123         //存储需要加密的字段
124         Set<String> encrypFields;
125
126         String fildName;
127
128         for(Class<?> cl:classes){
129             fildData = new FieldData();
130             encrypFields = new HashSet<>();
131
132             //表名称
133             if(!cl.isAnnotationPresent(Table.class)){
134                 continue;
135             }
136             Table table = cl.getAnnotation(Table.class);
137             fildData.setTableName(table.name());
138
139             // 取得本类的全部属性
140             Field[] fields = cl.getDeclaredFields();
141             fields = VariableAesKey.getPatentFields(fields,cl);
142             for (Field field:fields) {
143
144                 fildName = null;
145                 if(field.isAnnotationPresent(Column.class)){
146                     Column column = field.getAnnotation(Column.class);
147                     fildName = column.name();
148                     if(StringUtils.isEmpty(fildName)){
149                         fildName = field.getName();
150                     }
151                     if(column.isKey()){
152                         fildData.setId(fildName);
153                     }
154                 }else{
155                     fildName = field.getName();
156                 }
157
158                 // 判断方法中是否有指定注解类型的注解
159                 if (!field.isAnnotationPresent(MysqlHexAes.class)) {
160                     continue;
161                 }
162                 // 根据注解类型返回方法的指定类型注解
163                 MysqlHexAes mysqlHexAes = field.getAnnotation(MysqlHexAes.class);
164                 //判断版本号是不是一样的
165                 if(!StringUtils.isEmpty(VariableAesKey.INIT_VERSION)){
166                     if(!VariableAesKey.INIT_VERSION.equals(mysqlHexAes.initVersion())){
167                         continue;
168                     }
169                 }else{
170                     if(!StringUtils.isEmpty(mysqlHexAes.initVersion())){
171                         continue;
172                     }
173                 }
174
175                 String aesKey = mysqlHexAes.aesKey();
176                 if(StringUtils.isEmpty(aesKey)){
177                     aesKey = VariableAesKey.AES_KEY;
178                     if(StringUtils.isEmpty(aesKey)){
179                         throw new RuntimeException("mysql的AES秘钥不能为空:"+field.getName());
180                     }
181                 }
182
183                 encrypFields.add(fildName);
184             }
185             //是否有需要加密得字段
186             if(encrypFields.size()<=0){
187                 continue;
188             }
189             fildData.setEncrypFields(encrypFields);
190             fildDatas.add(fildData);
191         }
192         return fildDatas;
193     }
194
93b505 195
C 196 }