From a0ad14b0a0fb60604b0a9cf978a537e64479a8d4 Mon Sep 17 00:00:00 2001
From: chenjiahe <763432473@qq.com>
Date: 星期一, 27 六月 2022 19:05:01 +0800
Subject: [PATCH] 新增Redis可以切换数据库

---
 src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java |  117 +++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 78 insertions(+), 39 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..4975d2b 100644
--- a/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java
+++ b/src/main/java/com/hx/mybatis/aes/springbean/InitMysqlData.java
@@ -3,23 +3,24 @@
 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;
 import com.hx.util.StringUtils;
 import com.hx.util.mysql.aes.MysqlHexAesTool;
 
-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 {
 
     /**
      * 椤圭洰鍚姩灏辨墽琛屽悗灏辨墽琛岃鏂规硶
      */
-    @PostConstruct
+    //@PostConstruct 2022-06-17灞忔帀锛屾殏鏃剁敤涓嶄笂
     public static void initData(String packPath, CommonService commonService){
 
         //椤圭洰鍚姩鐨勬椂鍊欏~鍏�
@@ -32,48 +33,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