chenjiahe
2022-04-06 a48817ffcb840ed803c813c010f3730eb7e9cb87
提交 | 用户 | 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);
a48817 44                     System.out.println("sqlSentence3:"+sqlSentence.getSqlSentence());
f36821 45                     int total = commonService.selectCountSql(sqlSentence);
C 46                     if(total ==0 ){
47                         continue;
b1097d 48                     }
f36821 49                     int pageTotal = total/pageSize+1;
C 50                     for(int i= 0;i<pageTotal;i++){
51                         int finalI = i;
52                         fixedThreadPool.execute(new Runnable() {
53                             @Override
54                             public void run() {
55                                 //更新数据
56                                 updateData(fieldData,commonService, finalI,pageSize);
57                             }
58                         });
93b505 59                     }
C 60                 }
f36821 61             }catch (Exception e){
C 62                 e.printStackTrace();
63             }finally {
64                 fixedThreadPool.shutdown();
93b505 65             }
C 66         }
67     }
68
f36821 69     /**更新数据*/
C 70     public static void updateData(FieldData fieldData,CommonService commonService,int pageNum,int pageSize){
71         SqlSentence sqlSentence = new SqlSentence();
72         Map<String,Object> values = new HashMap<>();
73
74         //查询数据
75         StringBuilder selectField = new StringBuilder();
76         selectField.append(fieldData.getId());
77         for(String fieldName:fieldData.getEncrypFields()){
78             selectField.append(","+fieldName);
79         }
80         sqlSentence.sqlSentence("SELECT "+selectField.toString()+" FROM "+fieldData.getTableName()+" LIMIT "+pageNum+","+pageSize,values);
a48817 81         System.out.println("sqlSentence:"+sqlSentence.getSqlSentence());
f36821 82         List<Map<String,Object>> list = commonService.selectListMap(CommonMapper.class,sqlSentence);
C 83
84         for (Map<String,Object> map:list){
85             StringBuilder setField = new StringBuilder();
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+"}");
102             }
103
104             values = map;
105             sqlSentence.sqlSentence("UPDATE "+fieldData.getTableName()+" SET "+setField.toString()+" WHERE "+fieldData.getId()+" = #{m."+fieldData.getId()+"}",values);
a48817 106             System.out.println("sqlSentence2:"+sqlSentence.getSqlSentence());
C 107
f36821 108             if(commonService.updateSentence(sqlSentence)!=1){
C 109                 throw new ServiceException("更新超过1条,更新失败!");
110             }
111         }
112
113     }
114
b1097d 115     /**获取到表的数据*/
C 116     public static List<FieldData> entityhandle(Set<Class<?>> classes){
117
118         List<FieldData> fildDatas = new ArrayList<>();
119         //存储单表字段信息
120         FieldData fildData;
121         //存储需要加密的字段
122         Set<String> encrypFields;
123
124         String fildName;
125
126         for(Class<?> cl:classes){
127             fildData = new FieldData();
128             encrypFields = new HashSet<>();
129
130             //表名称
131             if(!cl.isAnnotationPresent(Table.class)){
132                 continue;
133             }
134             Table table = cl.getAnnotation(Table.class);
135             fildData.setTableName(table.name());
136
137             // 取得本类的全部属性
138             Field[] fields = cl.getDeclaredFields();
139             fields = VariableAesKey.getPatentFields(fields,cl);
140             for (Field field:fields) {
141
142                 fildName = null;
143                 if(field.isAnnotationPresent(Column.class)){
144                     Column column = field.getAnnotation(Column.class);
145                     fildName = column.name();
146                     if(StringUtils.isEmpty(fildName)){
147                         fildName = field.getName();
148                     }
149                     if(column.isKey()){
150                         fildData.setId(fildName);
151                     }
152                 }else{
153                     fildName = field.getName();
154                 }
155
156                 // 判断方法中是否有指定注解类型的注解
157                 if (!field.isAnnotationPresent(MysqlHexAes.class)) {
158                     continue;
159                 }
160                 // 根据注解类型返回方法的指定类型注解
161                 MysqlHexAes mysqlHexAes = field.getAnnotation(MysqlHexAes.class);
162                 //判断版本号是不是一样的
163                 if(!StringUtils.isEmpty(VariableAesKey.INIT_VERSION)){
164                     if(!VariableAesKey.INIT_VERSION.equals(mysqlHexAes.initVersion())){
165                         continue;
166                     }
167                 }else{
168                     if(!StringUtils.isEmpty(mysqlHexAes.initVersion())){
169                         continue;
170                     }
171                 }
172
173                 String aesKey = mysqlHexAes.aesKey();
174                 if(StringUtils.isEmpty(aesKey)){
175                     aesKey = VariableAesKey.AES_KEY;
176                     if(StringUtils.isEmpty(aesKey)){
177                         throw new RuntimeException("mysql的AES秘钥不能为空:"+field.getName());
178                     }
179                 }
180
181                 encrypFields.add(fildName);
182             }
183             //是否有需要加密得字段
184             if(encrypFields.size()<=0){
185                 continue;
186             }
187             fildData.setEncrypFields(encrypFields);
188             fildDatas.add(fildData);
189         }
190         return fildDatas;
191     }
192
93b505 193
C 194 }