From 4a044670a4b8a9bcc0a38b413ef803b9842a5052 Mon Sep 17 00:00:00 2001
From: fwq <582742538@qq.com>
Date: 星期三, 11 五月 2022 10:30:43 +0800
Subject: [PATCH] redis,list类型操作修改

---
 src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java |  114 ++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 77 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..4a3283d 100644
--- a/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java
+++ b/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java
@@ -3,7 +3,7 @@
 import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
 import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
 import com.hx.common.annotations.MysqlHexAes;
-import com.hx.common.dao.CommonMapper;
+import com.hx.common.dao.mapper.CommonMapper;
 import com.hx.common.service.CommonService;
 import com.hx.exception.ServiceException;
 import com.hx.mybatisTool.SqlSentence;
@@ -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,48 +34,86 @@
             //瑙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);
+            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;
+                    }
+                    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);
+                            }
+                        });
+                    }
                 }
-                sqlSentence.sqlSentence("SELECT "+selectField.toString()+" FROM "+fieldData.getTableName(),values);
+            }catch (Exception e){
+                e.printStackTrace();
+            }finally {
+                fixedThreadPool.shutdown();
+            }
+        }
+    }
 
-                //鏇存柊鏁版嵁
-                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+"}");
-                    }
+    /**鏇存柊鏁版嵁*/
+    public static void updateData(FieldData fieldData,CommonService commonService,int pageNum,int pageSize){
+        SqlSentence sqlSentence = new SqlSentence();
+        Map<String,Object> values = new HashMap<>();
 
-                    values = map;
-                    sqlSentence.sqlSentence("UPDATE "+fieldData.getTableName()+" SET "+setField.toString()+" WHERE id = #{m.id}",values);
-                    if(commonService.updateSentence(sqlSentence)!=1){
-                        throw new ServiceException("鏇存柊瓒呰繃1鏉★紝鏇存柊澶辫触锛�");
-                    }
+        //鏌ヨ鏁版嵁
+        StringBuilder selectField = new StringBuilder();
+        selectField.append(fieldData.getId());
+        for(String fieldName:fieldData.getEncrypFields()){
+            selectField.append(","+fieldName);
+        }
+        pageNum = pageNum*pageSize;
+
+        sqlSentence.sqlSentence("SELECT "+selectField.toString()+" FROM "+fieldData.getTableName()+" LIMIT "+pageNum+","+pageSize,values);
+        List<Map<String,Object>> list = commonService.selectListMap(CommonMapper.class,sqlSentence);
+
+        boolean isUpdate = false;
+        for (Map<String,Object> map:list){
+            isUpdate = false;
+            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+"}");
+                isUpdate = true;
+            }
+
+            if(isUpdate){
+                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鏉★紝鏇存柊澶辫触锛�");
                 }
             }
         }
+
     }
 
     /**鑾峰彇鍒拌〃鐨勬暟鎹�*/

--
Gitblit v1.8.0