浏览代码

数据建模

xuezizhuo 2 年之前
父节点
当前提交
edec9f7fbc

+ 1 - 51
src/main/java/com/customer/controller/CustomerController.java

@@ -36,59 +36,9 @@ public class CustomerController {
     @Resource
     private ICustomerService customerService;
 
-    @Autowired
-    private DynamicDataSource dynamicDataSource;
-
     @GetMapping("/list")
-    public List<Customer> list(@RequestParam("databaseType") String databaseType) throws NoSuchFieldException, IllegalAccessException, SQLException {
-        if(databaseType.equals(DataSourceType.MYSQL.getDataSourceName())){
-            GenConfig genConfig = new GenConfig();
-            genConfig.setDbtype("mysql");
-            genConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
-            genConfig.setUrl("jdbc:mysql://192.168.110.15:3306/zkqy-call");
-            genConfig.setUsername("root");
-            genConfig.setPassword("zkqy8888");
-            //切换数据源之前先清空
-            DynamicDataSource.clearDataSource();
-            //切换数据源
-            dynamicDataSource.changeDataSource(genConfig);
-        }else if(databaseType.equals(DataSourceType.SQLSERVER.getDataSourceName())){
-            GenConfig genConfig = new GenConfig();
-            genConfig.setDbtype("sqlserver");
-            genConfig.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
-            genConfig.setUrl("jdbc:sqlserver://localhost:1433;DatabaseName=test");
-            genConfig.setUsername("sa");
-            genConfig.setPassword("root1234");
-            //切换数据源之前先清空
-            DynamicDataSource.clearDataSource();
-            //切换数据源
-            dynamicDataSource.changeDataSource(genConfig);
-        }else if(databaseType.equals(DataSourceType.ORACLE.getDataSourceName())){
-            GenConfig genConfig = new GenConfig();
-            genConfig.setDbtype("oracle");
-            genConfig.setDriverClassName("oracle.jdbc.driver.OracleDriver");
-            genConfig.setUrl("jdbc:oracle:thin:@localhost:1521:orcl");
-            genConfig.setUsername("OTEST");
-            genConfig.setPassword("123456");
-            //切换数据源之前先清空
-            DynamicDataSource.clearDataSource();
-            //切换数据源
-            dynamicDataSource.changeDataSource(genConfig);
-        }else if(databaseType.equals(DataSourceType.DM.getDataSourceName())){
-            GenConfig genConfig = new GenConfig();
-            genConfig.setDbtype("dm");
-            genConfig.setDriverClassName("dm.jdbc.driver.DmDriver");
-            genConfig.setUrl("jdbc:dm://localhost:5236?schema=test1");
-            genConfig.setUsername("SYSDBA");
-            genConfig.setPassword("sysdba1234");
-            //切换数据源之前先清空
-            DynamicDataSource.clearDataSource();
-            //切换数据源
-            dynamicDataSource.changeDataSource(genConfig);
-        }
+    public List<Customer> list(@RequestParam("databaseType") String databaseType) {
         return customerService.list();
-
-
     }
 
 

+ 19 - 1
src/main/java/com/customer/controller/TableInfoController.java

@@ -3,6 +3,8 @@ package com.customer.controller;
 
 import com.customer.config.DynamicDataSource;
 import com.customer.config.GenConfig;
+import com.customer.pojo.TableInfo;
+import com.customer.service.IDataSourceService;
 import com.customer.service.ITableInfoService;
 import com.customer.utils.AjaxResult;
 import com.customer.utils.StringUtils;
@@ -11,6 +13,7 @@ import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 import java.sql.SQLException;
+import java.util.List;
 import java.util.Map;
 
 @RestController
@@ -64,8 +67,23 @@ public class TableInfoController {
     }
 
     @GetMapping("/tableInfoList")
-    public AjaxResult tableInfoList(@RequestParam("databaseName") String databaseName) throws SQLException {
+    public AjaxResult tableInfoList(@RequestParam("databaseName") String databaseName){
         return AjaxResult.success(tableInfoService.tableInfoList(databaseName));
     }
 
+    @GetMapping("/removeTable/{tableName}")
+    public AjaxResult removeTable(@PathVariable String tableName){
+        if(tableInfoService.selectDataCount(tableName)>0){
+            return AjaxResult.warn("表中有数据,不能删除!");
+        }
+        tableInfoService.dropMysqlTable(tableName);
+        return AjaxResult.success();
+    }
+
+    @GetMapping("/mysqlTableFieldInfo")
+    public AjaxResult mysqlTableFieldInfo(@RequestParam("databaseName") String databaseName,@RequestParam("tableName") String tableName){
+        return AjaxResult.success(tableInfoService.mysqlTableFieldInfo(databaseName,tableName));
+    }
+
+
 }

+ 16 - 0
src/main/java/com/customer/mapper/TableInfoMapper.java

@@ -1,6 +1,7 @@
 package com.customer.mapper;
 
 
+import com.customer.pojo.TableInfo;
 import com.customer.vo.TableInfoVO;
 import org.apache.ibatis.annotations.Param;
 
@@ -34,6 +35,21 @@ public interface TableInfoMapper {
      */
     List<TableInfoVO> tableInfoList(String databaseName);
 
+    /**
+     * 查询表中有没有数据
+     */
+    int selectDataCount(String tableName);
+
+    /**
+     * 删除表
+     */
+    void dropMysqlTable(String tableName);
+
+    /**
+     * 获取表字段信息
+     */
+    List<TableInfo> mysqlTableFieldInfo(@Param("dataBaseName") String dataBaseName,@Param("tableName") String tableName);
+
     //---------------------------------sqlServer------------------------------------------
 
     /**

+ 3 - 0
src/main/java/com/customer/pojo/TableInfo.java

@@ -26,5 +26,8 @@ public class TableInfo {
     /** 字段描述 */
     private String fieldDescription;
 
+    /** 字段长度 */
+    private Long fieldLength;
+
 
 }

+ 5 - 0
src/main/java/com/customer/service/IDataSourceService.java

@@ -21,4 +21,9 @@ public interface IDataSourceService extends IService<DataSource> {
      * 切换数据源
      */
     void changeDataSource(GenConfig genConfig) throws Exception;
+
+    /**
+     * 获取当前数据源数据库
+     */
+    String getDatabaseName() throws Exception;
 }

+ 17 - 1
src/main/java/com/customer/service/ITableInfoService.java

@@ -1,5 +1,6 @@
 package com.customer.service;
 
+import com.customer.pojo.TableInfo;
 import com.customer.vo.TableInfoVO;
 import org.apache.ibatis.annotations.Param;
 
@@ -29,7 +30,22 @@ public interface ITableInfoService {
     /**
      * 查询数据库中的表信息
      */
-    List<TableInfoVO> tableInfoList(String databaseName) throws SQLException;
+    List<TableInfoVO> tableInfoList(String databaseName);
+
+    /**
+     * 查询表中有没有数据
+     */
+    int selectDataCount(String tableName);
+
+    /**
+     * 删除表
+     */
+    void dropMysqlTable(String tableName);
+
+    /**
+     * 获取表字段信息
+     */
+    List<TableInfo> mysqlTableFieldInfo(String dataBaseName, String tableName);
 
     //---------------------------------sqlServer------------------------------------------
 

+ 7 - 1
src/main/java/com/customer/service/impl/DataSourceServiceImpl.java

@@ -62,7 +62,13 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
         DynamicDataSource.clearDataSource();
         //切换数据源
         dynamicDataSource.changeDataSource(genConfig);
-        System.err.println("当前数据源:" + dynamicDataSource.getConnection());
+        System.err.println("当前数据源:" + dynamicDataSource.getConnection().getCatalog());
 
     }
+
+    @Override
+    public String getDatabaseName() throws Exception{
+        System.err.println(dynamicDataSource.getConnection().getCatalog());
+        return dynamicDataSource.getConnection().getCatalog();
+    }
 }

+ 18 - 9
src/main/java/com/customer/service/impl/TableInfoServiceImpl.java

@@ -5,6 +5,8 @@ import com.customer.config.DynamicDataSource;
 import com.customer.config.GenConfig;
 import com.customer.mapper.TableInfoMapper;
 import com.customer.pojo.TableInfo;
+import com.customer.service.ICustomerService;
+import com.customer.service.IDataSourceService;
 import com.customer.service.ITableInfoService;
 import com.customer.vo.TableInfoVO;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -25,9 +27,6 @@ public class TableInfoServiceImpl implements ITableInfoService {
     @Resource
     private TableInfoMapper tableInfoMapper;
 
-    @Autowired
-    private DynamicDataSource dynamicDataSource;
-
     @Override
     @Transactional
     public void createMysqlDataBase(Map<String, Object> map) {
@@ -103,13 +102,23 @@ public class TableInfoServiceImpl implements ITableInfoService {
     }
 
     @Override
-    public List<TableInfoVO> tableInfoList(String databaseName) throws SQLException {
-        if(StringUtils.hasLength(databaseName)){
+    public List<TableInfoVO> tableInfoList(String databaseName) {
             return tableInfoMapper.tableInfoList(databaseName);
-        }else {
-            String database = dynamicDataSource.getConnection().getCatalog();
-            return tableInfoMapper.tableInfoList(database);
-        }
+    }
+
+    @Override
+    public int selectDataCount(String tableName) {
+        return tableInfoMapper.selectDataCount(tableName);
+    }
+
+    @Override
+    public void dropMysqlTable(String tableName) {
+        tableInfoMapper.dropMysqlTable(tableName);
+    }
+
+    @Override
+    public List<TableInfo> mysqlTableFieldInfo(String dataBaseName, String tableName) {
+        return tableInfoMapper.mysqlTableFieldInfo(dataBaseName,tableName);
     }
 
     @Override

+ 41 - 0
src/main/resources/mapper/TableInfoMapper.xml

@@ -1,6 +1,16 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.customer.mapper.TableInfoMapper">
+    
+    <resultMap id="mysqlTableInfoResult" type="com.customer.pojo.TableInfo">
+        <result property="fieldName" column="fieldName"></result>
+        <result property="fieldType" column="fieldType"></result>
+        <result property="isNull" column="isNull" javaType="boolean"></result>
+        <result property="isPrimary" column="isPrimary" javaType="boolean"></result>
+        <result property="fieldDescription" column="fieldDescription"></result>
+        <result property="fieldLength" column="fieldLength"></result>
+        <result property="isAuto" column="isAuto" javaType="boolean"></result>
+    </resultMap>
 
     <update id="useDataBase">
         use ${dataBaseName}
@@ -30,7 +40,38 @@
         select table_name tableName,create_time createTime,table_comment tableComment from information_schema.tables where table_schema=#{databaseName}
     </select>
 
+<!--    <select id="selectDataCount" resultType="int">-->
+<!--        SELECT table_rows FROM information_schema.tables-->
+<!--        WHERE TABLE_SCHEMA = #{dataBaseName}-->
+<!--          and table_name = #{tableName}-->
+<!--    </select>-->
 
+    <select id="selectDataCount" resultType="int">
+        select count(1) from ${tableName}
+    </select>
+
+    <delete id="dropMysqlTable">
+        DROP TABLE IF EXISTS ${tableName};
+    </delete>
+
+    <select id="mysqlTableFieldInfo" resultMap="mysqlTableInfoResult">
+        SELECT
+            column_name fieldName,
+            IF
+                ( is_nullable = 'NO', TRUE, FALSE ) isNull,
+            data_type fieldType,
+            character_maximum_length fieldLength,
+            IF
+                ( column_key = 'PRI', TRUE, FALSE ) isPrimary,
+            column_comment fieldDescription ,
+            if(extra='auto_increment',true,false) isAuto
+        FROM
+            information_schema.COLUMNS
+        WHERE
+            table_name = #{tableName}
+          AND table_schema = #{dataBaseName};
+
+    </select>
 
     <select id="sqlServerTableExist" resultType="int">
         select count(1) from sysobjects where id = object_id(#{tableName})