From db287aac9eb5032b1158b405a9b4807301467ed3 Mon Sep 17 00:00:00 2001
From: zhouxiang <zhouxiang>
Date: 星期四, 24 三月 2022 17:48:03 +0800
Subject: [PATCH] 添加redis获取整个hashKey数据和单个hashKey数据方法

---
 src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java |  106 ++++++++++++++++++++++++++++++++++------------------
 1 files changed, 69 insertions(+), 37 deletions(-)

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