ChenJiaHe
2022-01-22 f12d2c4c92dcd7929bce4d2df74f48f8abc08350
提交 | 用户 | 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;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19
20 public class InitMysqlData {
21
22     /**
23      * 项目启动就执行后就执行该方法
24      */
25     @PostConstruct
26     public static void initData(String packPath, CommonService commonService){
27
28         //项目启动的时候填入
29         if(!StringUtils.isEmpty(packPath)){
30             Set<Class<?>> classes = VariableAesKey.classData(packPath);
31             Map<String,String> aesKeysFild = new HashMap<>();
32             boolean isAes = false;
33             String tableName = null;
34             String fildName = null;
35             String fildValue = null;
36
37             SqlSentence sqlSentence = new SqlSentence();
38             Map<String,Object> values = new HashMap<>();
39
40             for(Class<?> cl:classes){
41                 //表名称
42                 boolean hasAnnotation = cl.isAnnotationPresent(Table.class);
43                 if(!hasAnnotation){
44                     continue;
45                 }
46                 Table table = cl.getAnnotation(Table.class);
47                 tableName = table.name();
48
49                 aesKeysFild = new HashMap<>();
50                 isAes = false;
51
52                 // 取得本类的全部属性
53                 Field[] fields = cl.getDeclaredFields();
54                 fields = VariableAesKey.getPatentFields(fields,cl);
55                 for (Field field:fields) {
56                     fildName = null;
57                     // 判断方法中是否有指定注解类型的注解
58                     hasAnnotation = field.isAnnotationPresent(MysqlHexAes.class);
59                     if (hasAnnotation) {
60                         // 根据注解类型返回方法的指定类型注解
61                         MysqlHexAes mysqlHexAes = field.getAnnotation(MysqlHexAes.class);
62
63                         //String aesKeyField = mysqlHexAes.aesKeyField();
64                         String aesKey = mysqlHexAes.aesKey();
65
66                         if(StringUtils.isEmpty(aesKey)){
67                             aesKey = VariableAesKey.AES_KEY;
68                             if(StringUtils.isEmpty(aesKey)){
69                                 throw new RuntimeException("mysql的AES秘钥不能为空:"+field.getName());
70                             }
71                         }
72
73                         hasAnnotation = field.isAnnotationPresent(Column.class);
74                         if(hasAnnotation){
75                             Column column = field.getAnnotation(Column.class);
76                             fildName = column.name();
77                         }
78                         if(StringUtils.isEmpty(fildName)){
79                             fildName = field.getName();
80                         }
81
82                         sqlSentence.sqlSentence("SELECT id,"+fildName+" FROM "+tableName,values);
83                         List<Map<String,Object>> list = commonService.selectListMap(CommonMapper.class,sqlSentence);
84                         for(Map<String,Object> map:list){
85                             fildValue = (String)map.get(fildName);
86                             System.out.println("fildValue:"+fildValue);
87                             if(StringUtils.isEmpty(fildValue)){
88                                 continue;
89                             }
90                             if(fildValue.length()%32==0 && MysqlHexAesTool.isHexStrValid(fildValue)){
91                                 continue;
92                             }
93                             values.clear();
94                             values.put("id",map.get("id"));
95                             values.put("filedData",fildValue);
96                             sqlSentence.sqlSentence("UPDATE "+tableName+" SET "+fildName+" = #{m.filedData} WHERE id = #{m.id}",values);
97                             if(commonService.updateSentence(sqlSentence)!=1){
98                                 throw new ServiceException("更新超过1条,更新失败!");
99                             }
100                         }
101                     }
102                 }
103             }
104         }
105     }
106
107
108 }