chenjiahe
2022-01-28 b1097dce55d857ae1ae63798911c1e7027c00de4
提交 | 用户 | 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.*;
93b505 16
C 17 public class InitMysqlData {
18
19     /**
20      * 项目启动就执行后就执行该方法
21      */
22     @PostConstruct
23     public static void initData(String packPath, CommonService commonService){
24
25         //项目启动的时候填入
26         if(!StringUtils.isEmpty(packPath)){
27             Set<Class<?>> classes = VariableAesKey.classData(packPath);
28
29             SqlSentence sqlSentence = new SqlSentence();
30             Map<String,Object> values = new HashMap<>();
31
b1097d 32             //解析表数据
C 33             List<FieldData> fieldDatas = entityhandle(classes);
34
35             StringBuilder selectField;
36             StringBuilder setField;
37             for(FieldData fieldData:fieldDatas){
38                 //获取条数
39                 sqlSentence.sqlSentence("SELECT COUNT(0) FROM "+fieldData.getTableName(),values);
40                 int total = commonService.selectCountSql(sqlSentence);
41
42                 //查询数据
43                 selectField = new StringBuilder();
44                 selectField.append(fieldData.getId());
45                 for(String fieldName:fieldData.getEncrypFields()){
46                     selectField.append(","+fieldName);
93b505 47                 }
b1097d 48                 sqlSentence.sqlSentence("SELECT "+selectField.toString()+" FROM "+fieldData.getTableName(),values);
93b505 49
b1097d 50                 //更新数据
C 51                 List<Map<String,Object>> list = commonService.selectListMap(CommonMapper.class,sqlSentence);
52                 for (Map<String,Object> map:list){
53                     setField = new StringBuilder();
54                     for (Map.Entry<String, Object> entry : map.entrySet()) {
55                         String mapKey = entry.getKey();
56                         String mapValue = (String) entry.getValue();
57                         if(StringUtils.isEmpty(mapValue)){
58                             continue;
93b505 59                         }
b1097d 60                         if(mapValue.length()%32==0 && MysqlHexAesTool.isHexStrValid(mapValue)){
C 61                             continue;
62                         }
63                         if(setField.length()>0){
64                             setField.append(",");
65                         }
66                         setField.append(mapKey+" = #{m."+mapKey+"}");
67                     }
93b505 68
b1097d 69                     values = map;
C 70                     sqlSentence.sqlSentence("UPDATE "+fieldData.getTableName()+" SET "+setField.toString()+" WHERE id = #{m.id}",values);
71                     if(commonService.updateSentence(sqlSentence)!=1){
72                         throw new ServiceException("更新超过1条,更新失败!");
93b505 73                     }
C 74                 }
75             }
76         }
77     }
78
b1097d 79     /**获取到表的数据*/
C 80     public static List<FieldData> entityhandle(Set<Class<?>> classes){
81
82         List<FieldData> fildDatas = new ArrayList<>();
83         //存储单表字段信息
84         FieldData fildData;
85         //存储需要加密的字段
86         Set<String> encrypFields;
87
88         String fildName;
89
90         for(Class<?> cl:classes){
91             fildData = new FieldData();
92             encrypFields = new HashSet<>();
93
94             //表名称
95             if(!cl.isAnnotationPresent(Table.class)){
96                 continue;
97             }
98             Table table = cl.getAnnotation(Table.class);
99             fildData.setTableName(table.name());
100
101             // 取得本类的全部属性
102             Field[] fields = cl.getDeclaredFields();
103             fields = VariableAesKey.getPatentFields(fields,cl);
104             for (Field field:fields) {
105
106                 fildName = null;
107                 if(field.isAnnotationPresent(Column.class)){
108                     Column column = field.getAnnotation(Column.class);
109                     fildName = column.name();
110                     if(StringUtils.isEmpty(fildName)){
111                         fildName = field.getName();
112                     }
113                     if(column.isKey()){
114                         fildData.setId(fildName);
115                     }
116                 }else{
117                     fildName = field.getName();
118                 }
119
120                 // 判断方法中是否有指定注解类型的注解
121                 if (!field.isAnnotationPresent(MysqlHexAes.class)) {
122                     continue;
123                 }
124                 // 根据注解类型返回方法的指定类型注解
125                 MysqlHexAes mysqlHexAes = field.getAnnotation(MysqlHexAes.class);
126                 //判断版本号是不是一样的
127                 if(!StringUtils.isEmpty(VariableAesKey.INIT_VERSION)){
128                     if(!VariableAesKey.INIT_VERSION.equals(mysqlHexAes.initVersion())){
129                         continue;
130                     }
131                 }else{
132                     if(!StringUtils.isEmpty(mysqlHexAes.initVersion())){
133                         continue;
134                     }
135                 }
136
137                 String aesKey = mysqlHexAes.aesKey();
138                 if(StringUtils.isEmpty(aesKey)){
139                     aesKey = VariableAesKey.AES_KEY;
140                     if(StringUtils.isEmpty(aesKey)){
141                         throw new RuntimeException("mysql的AES秘钥不能为空:"+field.getName());
142                     }
143                 }
144
145                 encrypFields.add(fildName);
146             }
147             //是否有需要加密得字段
148             if(encrypFields.size()<=0){
149                 continue;
150             }
151             fildData.setEncrypFields(encrypFields);
152             fildDatas.add(fildData);
153         }
154         return fildDatas;
155     }
156
93b505 157
C 158 }