Browse Source

sql导入

ZHOUTD 1 năm trước cách đây
mục cha
commit
4dde058f06

+ 16 - 0
xzl-admin/src/main/java/com/xzl/web/controller/DataGovernanceController.java

@@ -12,7 +12,9 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -78,4 +80,18 @@ public class DataGovernanceController {
     }
 
 
+    @RequestMapping(value = "/importData")
+    public Map<String,String> importData(@RequestParam(value = "databaseName") String databaseName,
+                                         @RequestParam(value = "file")MultipartFile file){
+        try {
+            return dataGovernanceService.importData(databaseName,file);
+        } catch (Exception e) {
+            Map<String, String> result = new HashMap<>();
+            result.put("message", "导入失败");
+            result.put("type", "error");
+            return result;
+        }
+    }
+
+
 }

+ 1 - 1
xzl-admin/src/main/java/com/xzl/web/controller/userPortrait/ViewController.java

@@ -102,7 +102,7 @@ public class ViewController {
         out.close();
 
         // 调用python脚本, 生成新的词云图片
-        String[] commands = {pythonPath, "/usr/local/python/word-cloud.py"};
+        String[] commands = {pythonPath, "D:/Projects/xtyc_yhhx/xzl-admin/src/main/resources/python/word-cloud.py"};
         ProcessBuilder processBuilder = new ProcessBuilder(commands);
         Process process = processBuilder.start();
         // 等待命令执行完成

+ 3 - 0
xzl-admin/src/main/java/com/xzl/web/service/DataGovernanceService.java

@@ -7,6 +7,7 @@ import com.xzl.web.model.dataGovernance.dto.SysDataLogQueryParam;
 import com.xzl.web.model.dataGovernance.entity.DatabaseInfo;
 import com.xzl.web.model.dataGovernance.entity.SysDataLog;
 import com.xzl.web.model.dataGovernance.entity.TableInfo;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.util.List;
 import java.util.Map;
@@ -32,4 +33,6 @@ public interface DataGovernanceService {
     Map<String, String> dataReplication(DataReplicationParam param);
 
     Result<SysDataLog> getDataLog(SysDataLogQueryParam param);
+
+    Map<String, String> importData(String databaseName, MultipartFile file) throws Exception;
 }

+ 129 - 71
xzl-admin/src/main/java/com/xzl/web/service/impl/DataGovernanceServiceImpl.java

@@ -9,11 +9,20 @@ import com.xzl.web.model.dataGovernance.dto.DataReplicationParam;
 import com.xzl.web.model.dataGovernance.dto.SysDataLogQueryParam;
 import com.xzl.web.model.dataGovernance.entity.*;
 import com.xzl.web.service.DataGovernanceService;
+import org.apache.ibatis.io.Resources;
+import org.apache.ibatis.jdbc.ScriptRunner;
 import org.mybatis.spring.SqlSessionTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.io.*;
+import java.nio.charset.Charset;
+import java.sql.Connection;
+import java.sql.Driver;
+import java.sql.DriverManager;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -25,6 +34,18 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
     private DataGovernanceMapper dataGovernanceMapper;
 
 
+    @Autowired
+    private SqlSessionTemplate sqlSessionTemplate;
+
+    @Value("${spring.datasource.druid.master.url}")
+    private String url;
+
+    @Value("${spring.datasource.druid.master.username}")
+    private String username;
+
+    @Value("${spring.datasource.druid.master.password}")
+    private String password;
+
 
     @Override
     public List<DatabaseInfo> getDatabaseList() {
@@ -33,17 +54,17 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
 
     @Override
     public List<TableInfo> getTableListByDatabaseName(String databaseName) {
-        Map<String,String> param=new HashMap<>();
-        param.put("databaseName",databaseName);
+        Map<String, String> param = new HashMap<>();
+        param.put("databaseName", databaseName);
         return dataGovernanceMapper.getTableListByDatabaseName(param);
     }
 
     @Override
     public Map<String, String> createDatabase(String databaseName) {
-        Map<String,String> result = new HashMap<>();
-        if(verifySQLName(databaseName)){
-            result.put("message","数据库名称不符合规则");
-            result.put("type","error");
+        Map<String, String> result = new HashMap<>();
+        if (verifySQLName(databaseName)) {
+            result.put("message", "数据库名称不符合规则");
+            result.put("type", "error");
             return result;
         }
 
@@ -51,67 +72,67 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
             dataGovernanceMapper.createDatabase(databaseName);
         } catch (Exception e) {
             e.printStackTrace();
-            result.put("message","创建失败!");
-            result.put("type","error");
+            result.put("message", "创建失败!");
+            result.put("type", "error");
             return result;
         }
-        result.put("message","创建成功!");
-        result.put("type","success");
+        result.put("message", "创建成功!");
+        result.put("type", "success");
         return result;
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Map<String, String> dropDatabase(List<String> list) {
-        Map<String,String> result = new HashMap<>();
+        Map<String, String> result = new HashMap<>();
         try {
             for (String databaseName : list) {
                 dataGovernanceMapper.dropDatabase(databaseName);
             }
         } catch (Exception e) {
             e.printStackTrace();
-            result.put("message","删除失败!");
-            result.put("type","error");
+            result.put("message", "删除失败!");
+            result.put("type", "error");
             return result;
         }
-        result.put("message","删除成功!");
-        result.put("type","success");
+        result.put("message", "删除成功!");
+        result.put("type", "success");
         return result;
     }
 
     @Override
     public Map<String, String> createTable(CreateTableDTO createTableDTO) {
-        Map<String,String> result = new HashMap<>();
+        Map<String, String> result = new HashMap<>();
         String tableName = createTableDTO.getTableName();
-        if(verifySQLName(tableName)){
-            result.put("message","数据表名称不符合规则");
-            result.put("type","error");
+        if (verifySQLName(tableName)) {
+            result.put("message", "数据表名称不符合规则");
+            result.put("type", "error");
             return result;
         }
         List<TableColumnParam> columns = createTableDTO.getColumns();
         for (TableColumnParam column : columns) {
-            if(verifySQLName(column.getName())){
-                result.put("message","字段名称不符合规则");
-                result.put("type","error");
+            if (verifySQLName(column.getName())) {
+                result.put("message", "字段名称不符合规则");
+                result.put("type", "error");
                 return result;
             }
-            if(!column.getType().equals("text") && column.getLength().isEmpty()){
-                result.put("message","类型为varchar或int时长度不能为空!");
-                result.put("type","error");
+            if (!column.getType().equals("text") && column.getLength().isEmpty()) {
+                result.put("message", "类型为varchar或int时长度不能为空!");
+                result.put("type", "error");
                 return result;
             }
-            if(!column.getType().equals("text") && Integer.parseInt(column.getLength())>255){
+            if (!column.getType().equals("text") && Integer.parseInt(column.getLength()) > 255) {
                 column.setLength("255");
             }
         }
 
-        Map<String,String> param=new HashMap<>();
-        param.put("databaseName",createTableDTO.getDatabaseName());
-        param.put("tableName",createTableDTO.getTableName());
+        Map<String, String> param = new HashMap<>();
+        param.put("databaseName", createTableDTO.getDatabaseName());
+        param.put("tableName", createTableDTO.getTableName());
         List<TableInfo> list = dataGovernanceMapper.getTableListByDatabaseName(param);
-        if(list.size() >0){
-            result.put("message","数据表名称已存在!请更换名称!");
-            result.put("type","error");
+        if (list.size() > 0) {
+            result.put("message", "数据表名称已存在!请更换名称!");
+            result.put("type", "error");
             return result;
         }
 
@@ -119,40 +140,40 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
             dataGovernanceMapper.createTable(createTableDTO);
         } catch (Exception e) {
             e.printStackTrace();
-            result.put("message","创建失败!");
-            result.put("type","error");
+            result.put("message", "创建失败!");
+            result.put("type", "error");
             return result;
         }
-        result.put("message","创建成功!");
-        result.put("type","success");
+        result.put("message", "创建成功!");
+        result.put("type", "success");
         return result;
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Map<String, String> dropTable(List<String> list) {
-        Map<String,String> result = new HashMap<>();
+        Map<String, String> result = new HashMap<>();
         try {
             for (String tableName : list) {
                 dataGovernanceMapper.dropTable(tableName);
             }
         } catch (Exception e) {
             e.printStackTrace();
-            result.put("message","删除失败!");
-            result.put("type","error");
+            result.put("message", "删除失败!");
+            result.put("type", "error");
             return result;
         }
-        result.put("message","删除成功!");
-        result.put("type","success");
+        result.put("message", "删除成功!");
+        result.put("type", "success");
         return result;
     }
 
     @Override
     public List<Map> getTableTitleList(String tableName) {
-        Map<String,String> param=new HashMap<>();
+        Map<String, String> param = new HashMap<>();
         String[] strings = tableName.split("\\.");
-        param.put("dataBaseName",strings[0]);
-        param.put("tableName",strings[1]);
+        param.put("dataBaseName", strings[0]);
+        param.put("tableName", strings[1]);
         return dataGovernanceMapper.getTableTitleListByTableName(param);
     }
 
@@ -163,52 +184,52 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
 
     @Override
     public Map<String, String> dataReplication(DataReplicationParam param) {
-        Map<String,String> result = new HashMap<>();
-        if(verifySQLName(param.getResourceDatabaseName()) && verifySQLName(param.getResourceTableName()) && verifySQLName(param.getTargetDatabaseName()) && verifySQLName(param.getTargetTableName())){
-            result.put("message","传入参数不符合规则!");
-            result.put("type","error");
+        Map<String, String> result = new HashMap<>();
+        if (verifySQLName(param.getResourceDatabaseName()) && verifySQLName(param.getResourceTableName()) && verifySQLName(param.getTargetDatabaseName()) && verifySQLName(param.getTargetTableName())) {
+            result.put("message", "传入参数不符合规则!");
+            result.put("type", "error");
             return result;
         }
-        Map<String,String> resource = new HashMap<>();
-        Map<String,String> target = new HashMap<>();
-        resource.put("databaseName",param.getResourceDatabaseName());
-        resource.put("tableName",param.getResourceTableName());
-        target.put("databaseName",param.getTargetDatabaseName());
-        target.put("tableName",param.getTargetTableName());
+        Map<String, String> resource = new HashMap<>();
+        Map<String, String> target = new HashMap<>();
+        resource.put("databaseName", param.getResourceDatabaseName());
+        resource.put("tableName", param.getResourceTableName());
+        target.put("databaseName", param.getTargetDatabaseName());
+        target.put("tableName", param.getTargetTableName());
         List<TableStructure> resourceTableStructure = dataGovernanceMapper.getTableStructure(resource);
         List<TableStructure> targetTableStructure = dataGovernanceMapper.getTableStructure(target);
-        if(resourceTableStructure.size()!=targetTableStructure.size()){
-            result.put("message","复制失败!选择的表结构不同");
-            result.put("type","error");
+        if (resourceTableStructure.size() != targetTableStructure.size()) {
+            result.put("message", "复制失败!选择的表结构不同");
+            result.put("type", "error");
             return result;
         }
-        for(int i=0;i<resourceTableStructure.size();i++){
-            if(resourceTableStructure.get(0)!=targetTableStructure.get(0)){
-                result.put("message","复制失败!选择的表结构不同");
-                result.put("type","error");
+        for (int i = 0; i < resourceTableStructure.size(); i++) {
+            if (resourceTableStructure.get(0) != targetTableStructure.get(0)) {
+                result.put("message", "复制失败!选择的表结构不同");
+                result.put("type", "error");
                 return result;
             }
         }
         dataGovernanceMapper.dataReplication(param);
-        result.put("message","复制完成!");
-        result.put("type","success");
+        result.put("message", "复制完成!");
+        result.put("type", "success");
         return result;
     }
 
     @Override
     public Result<SysDataLog> getDataLog(SysDataLogQueryParam param) {
-        Result<SysDataLog> result= new Result<>();
+        Result<SysDataLog> result = new Result<>();
         PageParam pageParam = param.getPageParam();
         result.setPageParam(pageParam);
-        Map<String,String> queryParam=new HashMap<>();
+        Map<String, String> queryParam = new HashMap<>();
         Integer page = null;
-        if(pageParam.getPageNum()==1){
-            page=0;
-        }else{
-            page=(pageParam.getPageNum()-1) * pageParam.getPageSize();
+        if (pageParam.getPageNum() == 1) {
+            page = 0;
+        } else {
+            page = (pageParam.getPageNum() - 1) * pageParam.getPageSize();
         }
         queryParam.put("page", String.valueOf(page));
-        queryParam.put("pageSize",String.valueOf(pageParam.getPageSize()));
+        queryParam.put("pageSize", String.valueOf(pageParam.getPageSize()));
         queryParam.put("actionType", param.getActionType());
         Integer logCount = dataGovernanceMapper.getSysDataLogCount(queryParam);
         result.setCount(logCount);
@@ -217,8 +238,45 @@ public class DataGovernanceServiceImpl implements DataGovernanceService {
         return result;
     }
 
+    @Override
+    public Map<String, String> importData(String databaseName, MultipartFile file) throws Exception {
+        Map<String, String> result = new HashMap<>();
+        Connection conn = null;
+        //第一步以?号分割字符串
+        String[] urlSplit = url.split("\\?");
+        //第二步将?号前的内容以/号分割,然后弃用最后一段使用databaseName形成新的url
+        String[] urlSplit2 = urlSplit[0].split("/");
+        StringBuilder newUrlBuilder = new StringBuilder();
+        if (!verifySQLName(databaseName)) {
+            newUrlBuilder.append(urlSplit2[0]);
+            newUrlBuilder.append("//");
+            newUrlBuilder.append(urlSplit2[2]);
+            newUrlBuilder.append("/");
+            newUrlBuilder.append(databaseName);
+            newUrlBuilder.append("?");
+            newUrlBuilder.append(urlSplit[1]);
+        }
+        String newUrl = newUrlBuilder.toString();
+        InputStreamReader isr = null;
+        InputStream inputStream = file.getInputStream();
+        isr = new InputStreamReader(inputStream);
+        conn = DriverManager.getConnection(newUrl, username, password);
+        ScriptRunner scriptRunner = new ScriptRunner(conn);
+        // 设置编码,防止中文乱码
+        Resources.setCharset(Charset.forName("UTF-8"));
+        // 必须为true,不然容易报错
+        scriptRunner.setSendFullScript(true);
+        // 执行
+        scriptRunner.runScript(isr);
+        scriptRunner.closeConnection();
+        isr.close();
+        result.put("message", "复制失败!选择的表结构不同");
+        result.put("type", "error");
+        return result;
+    }
+
 
-    private Boolean verifySQLName(String text){
+    private Boolean verifySQLName(String text) {
         return text.isEmpty() || !text.matches("^[a-zA-Z_][a-zA-Z0-9_]{0,63}$");
     }
 }

+ 9 - 9
xzl-admin/src/main/resources/application-dev.yml

@@ -6,13 +6,13 @@ spring:
         druid:
             # 主库数据源
             master:
-#                url: jdbc:mysql://webapplication.mysql.polardb.rds.aliyuncs.com:3306/xtdb?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
-##                url: jdbc:mysql://webapplication.rwlb.rds.aliyuncs.com:3306/xtdb?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
-#                username: jerry
-#                password: zjr38zjR@
-                url: jdbc:mysql://localhost:3306/xtdb?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
-                username: root
-                password: 1234
+                url: jdbc:mysql://webapplication.mysql.polardb.rds.aliyuncs.com:3306/xtdb?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
+#                url: jdbc:mysql://webapplication.rwlb.rds.aliyuncs.com:3306/xtdb?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
+                username: jerry
+                password: zjr38zjR@
+#                url: jdbc:mysql://localhost:3306/xtdb?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
+#                username: root
+#                password: 1234
             # 从库数据源
             slave:
                 # 从数据源开关/默认关闭
@@ -63,5 +63,5 @@ spring:
                 wall:
                     config:
                         multi-statement-allow: true
-pythonFilePath: /usr/local/python/
-pythonPath: /usr/local/Cellar/python@3.10/3.10.11/bin/python3.10
+pythonFilePath: D:/Projects/xtyc_yhhx/xzl-admin/src/main/resources/python
+pythonPath: D:/python310/python.exe

+ 11 - 11
xzl-admin/src/main/resources/python/wordcloud.txt

@@ -1,11 +1,11 @@
-好猫(金丝猴)
-黄鹤楼(软蓝)
-双喜(软经典)
-黄山(小红方印)
-槟榔口味王
-黄鹤楼(软红)
-炫迈口想糖
-七匹狼(红)
-黄鹤楼(软雪之景)
-黄金叶(浓香中支)
-黄鹤楼(硬雅韵)
+好猫(金丝猴)  (条)	
+黄鹤楼(软蓝)	
+双喜(软经典)	
+黄山(小红方印)	
+槟榔口味王	
+黄鹤楼(软红)  (盒)	
+炫迈口想糖	
+七匹狼(红)  (盒)	
+黄鹤楼(软雪之景)  (盒)	
+黄金叶(浓香中支)	
+黄鹤楼(硬雅韵)  (盒) 购物件数:2	

+ 54 - 3
xzl-ui/src/views/dataGovernance/databaseManage.vue

@@ -42,6 +42,13 @@
         label="操作"
         width="200">
         <template slot-scope="scope">
+          <el-button
+            @click.native.prevent="importBtn(scope.$index, tableData)"
+            type="text"
+            size="small"
+          >
+            导入
+          </el-button>
           <el-button
             @click.native.prevent="view(scope.$index, tableData)"
             type="text"
@@ -68,7 +75,16 @@
         <el-button type="primary" @click="handleSave">保 存</el-button>
       </div>
     </el-dialog>
-
+    <el-upload
+      class="upload"
+      action="aaa"
+      :on-change="handleFile"
+      :file-list="fileList"
+      :limit="1"
+      ref="file"
+      style="display: none"
+      :auto-upload="false"
+    ></el-upload>
   </div>
 </template>
 <script>
@@ -82,14 +98,45 @@ export default {
       form: {
         databaseName: ""
       },
-      multipleSelection: []
-
+      multipleSelection: [],
+      fileList:[],
+      databaseName:""
     }
   },
   mounted() {
     this.initData();
   },
   methods: {
+    handleFile(file,fileList){
+      var fileNameSplit=file.name.split(".");
+      var fileNameSplitLength=fileNameSplit.length;
+      if(fileNameSplit[fileNameSplitLength-1] == "sql"){
+        var formData = new FormData();
+        formData.append("file",file.raw);
+        formData.append("databaseName",this.databaseName);
+        const t = this;
+        request({
+          url: "/dataGovernance/importData",
+          method: "post",
+          data: formData
+        }).then(rs => {
+          t.$message(rs);
+          this.$refs.file.clearFiles();
+        })
+      }else{
+        this.$refs.file.clearFiles();
+        this.$message({
+          type:"error",
+          message:"请选择sql文件"
+        });
+      }
+
+
+    },
+    importBtn(index,tableData){
+      this.databaseName=tableData[index].databaseName;
+      this.$refs.file.$children[0].$refs.input.click();
+    },
     initData() {
       const t = this;
       request({
@@ -149,6 +196,10 @@ export default {
     }
   }
 }
+
+
+
+
 </script>
 
 <style rel="stylesheet/scss" lang="scss">