chenjiahe
2022-04-02 0344dc66783353faff118a0ea91f7d2aa07bbd4a
提交 | 用户 | 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;
6 import com.hx.common.dao.CommonMapper;
7 import com.hx.common.service.CommonService;
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
82         for (Map<String,Object> map:list){
83             StringBuilder setField = new StringBuilder();
84             for (Map.Entry<String, Object> entry : map.entrySet()) {
85                 String mapKey = entry.getKey();
86                 String mapValue = (String) entry.getValue();
87                 if(StringUtils.isEmpty(mapValue)){
88                     continue;
89                 }
90                 if(mapValue.length()%32==0 && MysqlHexAesTool.isHexStrValid(mapValue)){
91                     continue;
92                 }
93                 if(fieldData.getId().equals(mapKey)){
94                     continue;
95                 }
96                 if(setField.length()>0){
97                     setField.append(",");
98                 }
99                 setField.append(mapKey+" = #{m."+mapKey+"}");
100             }
101
102             values = map;
103             sqlSentence.sqlSentence("UPDATE "+fieldData.getTableName()+" SET "+setField.toString()+" WHERE "+fieldData.getId()+" = #{m."+fieldData.getId()+"}",values);
104             if(commonService.updateSentence(sqlSentence)!=1){
105                 throw new ServiceException("更新超过1条,更新失败!");
106             }
107         }
108
109     }
110
b1097d 111     /**获取到表的数据*/
C 112     public static List<FieldData> entityhandle(Set<Class<?>> classes){
113
114         List<FieldData> fildDatas = new ArrayList<>();
115         //存储单表字段信息
116         FieldData fildData;
117         //存储需要加密的字段
118         Set<String> encrypFields;
119
120         String fildName;
121
122         for(Class<?> cl:classes){
123             fildData = new FieldData();
124             encrypFields = new HashSet<>();
125
126             //表名称
127             if(!cl.isAnnotationPresent(Table.class)){
128                 continue;
129             }
130             Table table = cl.getAnnotation(Table.class);
131             fildData.setTableName(table.name());
132
133             // 取得本类的全部属性
134             Field[] fields = cl.getDeclaredFields();
135             fields = VariableAesKey.getPatentFields(fields,cl);
136             for (Field field:fields) {
137
138                 fildName = null;
139                 if(field.isAnnotationPresent(Column.class)){
140                     Column column = field.getAnnotation(Column.class);
141                     fildName = column.name();
142                     if(StringUtils.isEmpty(fildName)){
143                         fildName = field.getName();
144                     }
145                     if(column.isKey()){
146                         fildData.setId(fildName);
147                     }
148                 }else{
149                     fildName = field.getName();
150                 }
151
152                 // 判断方法中是否有指定注解类型的注解
153                 if (!field.isAnnotationPresent(MysqlHexAes.class)) {
154                     continue;
155                 }
156                 // 根据注解类型返回方法的指定类型注解
157                 MysqlHexAes mysqlHexAes = field.getAnnotation(MysqlHexAes.class);
158                 //判断版本号是不是一样的
159                 if(!StringUtils.isEmpty(VariableAesKey.INIT_VERSION)){
160                     if(!VariableAesKey.INIT_VERSION.equals(mysqlHexAes.initVersion())){
161                         continue;
162                     }
163                 }else{
164                     if(!StringUtils.isEmpty(mysqlHexAes.initVersion())){
165                         continue;
166                     }
167                 }
168
169                 String aesKey = mysqlHexAes.aesKey();
170                 if(StringUtils.isEmpty(aesKey)){
171                     aesKey = VariableAesKey.AES_KEY;
172                     if(StringUtils.isEmpty(aesKey)){
173                         throw new RuntimeException("mysql的AES秘钥不能为空:"+field.getName());
174                     }
175                 }
176
177                 encrypFields.add(fildName);
178             }
179             //是否有需要加密得字段
180             if(encrypFields.size()<=0){
181                 continue;
182             }
183             fildData.setEncrypFields(encrypFields);
184             fildDatas.add(fildData);
185         }
186         return fildDatas;
187     }
188
93b505 189
C 190 }