From f36821f2611e7bac0692572c02e474aec6d258fb Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期五, 28 一月 2022 16:41:13 +0800 Subject: [PATCH] Mysql数据库AES加密工具 --- src/main/java/com/hx/mybatis/aes/springbean/ConstantBean.java | 6 +- src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java | 106 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 72 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/hx/mybatis/aes/springbean/ConstantBean.java b/src/main/java/com/hx/mybatis/aes/springbean/ConstantBean.java index f7281c2..2cdc32d 100644 --- a/src/main/java/com/hx/mybatis/aes/springbean/ConstantBean.java +++ b/src/main/java/com/hx/mybatis/aes/springbean/ConstantBean.java @@ -11,13 +11,13 @@ public class ConstantBean { /**鑾峰彇AES绉橀挜鐨勯厤缃紙浠庝粈涔堝寘鑾峰彇鍒帮級*/ - @Value("${mysql.hxe.aes.find.packs:null}") + @Value("${mysql.hxe.aes.find.packs:}") private String packPath; /**鍥哄畾AES鐨勭閽�*/ - @Value("${mysql.hxe.aes.fixd.key:null}") + @Value("${mysql.hxe.aes.fixd.key:}") private String fixedAesKey; /**鏁版嵁搴撳垵濮嬪寲鍔犲瘑瀛楁鐗堟湰鍙�*/ - @Value("${mysql.hxe.aes.init.version:null}") + @Value("${mysql.hxe.aes.init.version:}") private String initVersion; public String getPackPath() { diff --git a/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java b/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java index 92b46d7..d1648b3 100644 --- a/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java +++ b/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java @@ -13,6 +13,8 @@ import javax.annotation.PostConstruct; import java.lang.reflect.Field; import java.util.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class InitMysqlData { @@ -32,50 +34,80 @@ //瑙f瀽琛ㄦ暟鎹� List<FieldData> fieldDatas = entityhandle(classes); - StringBuilder selectField; - StringBuilder setField; - for(FieldData fieldData:fieldDatas){ - //鑾峰彇鏉℃暟 - sqlSentence.sqlSentence("SELECT COUNT(0) FROM "+fieldData.getTableName(),values); - int total = commonService.selectCountSql(sqlSentence); - - //鏌ヨ鏁版嵁 - selectField = new StringBuilder(); - selectField.append(fieldData.getId()); - for(String fieldName:fieldData.getEncrypFields()){ - selectField.append(","+fieldName); - } - sqlSentence.sqlSentence("SELECT "+selectField.toString()+" FROM "+fieldData.getTableName(),values); - - //鏇存柊鏁版嵁 - List<Map<String,Object>> list = commonService.selectListMap(CommonMapper.class,sqlSentence); - for (Map<String,Object> map:list){ - setField = new StringBuilder(); - for (Map.Entry<String, Object> entry : map.entrySet()) { - String mapKey = entry.getKey(); - String mapValue = (String) entry.getValue(); - if(StringUtils.isEmpty(mapValue)){ - continue; - } - if(mapValue.length()%32==0 && MysqlHexAesTool.isHexStrValid(mapValue)){ - continue; - } - if(setField.length()>0){ - setField.append(","); - } - setField.append(mapKey+" = #{m."+mapKey+"}"); + int pageSize = 500; + //鍒涘缓涓�涓畾闀跨嚎绋嬫睜锛屽彲鎺у埗绾跨▼鏈�澶у苟鍙戞暟锛岃秴鍑虹殑绾跨▼浼氬湪闃熷垪涓瓑寰呫�� + ExecutorService fixedThreadPool = Executors.newFixedThreadPool(20); + try{ + for(FieldData fieldData:fieldDatas){ + //鑾峰彇鏉℃暟 + sqlSentence.sqlSentence("SELECT COUNT(0) FROM "+fieldData.getTableName(),values); + int total = commonService.selectCountSql(sqlSentence); + if(total ==0 ){ + continue; } - - values = map; - sqlSentence.sqlSentence("UPDATE "+fieldData.getTableName()+" SET "+setField.toString()+" WHERE id = #{m.id}",values); - if(commonService.updateSentence(sqlSentence)!=1){ - throw new ServiceException("鏇存柊瓒呰繃1鏉★紝鏇存柊澶辫触锛�"); + int pageTotal = total/pageSize+1; + for(int i= 0;i<pageTotal;i++){ + int finalI = i; + fixedThreadPool.execute(new Runnable() { + @Override + public void run() { + //鏇存柊鏁版嵁 + updateData(fieldData,commonService, finalI,pageSize); + } + }); } } + }catch (Exception e){ + e.printStackTrace(); + }finally { + fixedThreadPool.shutdown(); } } } + /**鏇存柊鏁版嵁*/ + public static void updateData(FieldData fieldData,CommonService commonService,int pageNum,int pageSize){ + SqlSentence sqlSentence = new SqlSentence(); + Map<String,Object> values = new HashMap<>(); + + //鏌ヨ鏁版嵁 + StringBuilder selectField = new StringBuilder(); + selectField.append(fieldData.getId()); + for(String fieldName:fieldData.getEncrypFields()){ + selectField.append(","+fieldName); + } + sqlSentence.sqlSentence("SELECT "+selectField.toString()+" FROM "+fieldData.getTableName()+" LIMIT "+pageNum+","+pageSize,values); + List<Map<String,Object>> list = commonService.selectListMap(CommonMapper.class,sqlSentence); + + for (Map<String,Object> map:list){ + StringBuilder setField = new StringBuilder(); + for (Map.Entry<String, Object> entry : map.entrySet()) { + String mapKey = entry.getKey(); + String mapValue = (String) entry.getValue(); + if(StringUtils.isEmpty(mapValue)){ + continue; + } + if(mapValue.length()%32==0 && MysqlHexAesTool.isHexStrValid(mapValue)){ + continue; + } + if(fieldData.getId().equals(mapKey)){ + continue; + } + if(setField.length()>0){ + setField.append(","); + } + setField.append(mapKey+" = #{m."+mapKey+"}"); + } + + values = map; + sqlSentence.sqlSentence("UPDATE "+fieldData.getTableName()+" SET "+setField.toString()+" WHERE "+fieldData.getId()+" = #{m."+fieldData.getId()+"}",values); + if(commonService.updateSentence(sqlSentence)!=1){ + throw new ServiceException("鏇存柊瓒呰繃1鏉★紝鏇存柊澶辫触锛�"); + } + } + + } + /**鑾峰彇鍒拌〃鐨勬暟鎹�*/ public static List<FieldData> entityhandle(Set<Class<?>> classes){ -- Gitblit v1.8.0