Просмотр исходного кода

数据建模相关接口修改

xuezizhuo 2 лет назад
Родитель
Сommit
a36162c3bd

+ 27 - 15
src/main/java/com/customer/controller/TableInfoController.java

@@ -1,18 +1,11 @@
 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;
-import org.springframework.beans.factory.annotation.Autowired;
+import com.customer.vo.TableInfoVO;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
-import java.sql.SQLException;
 import java.util.List;
 import java.util.Map;
 
@@ -66,23 +59,42 @@ public class TableInfoController {
         return AjaxResult.success();
     }
 
+    //---------------------------------------通用接口(支持mysql、sqlserver、达梦)-----------------------------------------------------
+
+    /**
+     * 新增数据库和表
+     */
+    @PostMapping("/createTable")
+    public AjaxResult createTable(@RequestBody Map<String, Object> map){
+        return tableInfoService.createTable(map);
+    }
+
     /**
      * 根据当前数据源显示对应的数据表列表
      */
     @PostMapping("/tableInfoList")
     public AjaxResult tableInfoList(@RequestBody Map<String,Object> map){
-        return AjaxResult.success(tableInfoService.tableInfoList(map));
+        List<TableInfoVO> tableInfoVOS = tableInfoService.tableInfoList(map);
+        AjaxResult ajaxResult = new AjaxResult();
+        ajaxResult.put("code",200)
+                .put("data",tableInfoVOS)
+                .put("msg","操作成功")
+                .put("total",tableInfoVOS.size());
+        return ajaxResult;
     }
 
-    @GetMapping("/removeTable/{tableName}")
+    /**
+     * 根据当前数据源删除对应的数据表
+     */
+    @DeleteMapping("/removeTable/{tableName}")
     public AjaxResult removeTable(@PathVariable String tableName){
-        if(tableInfoService.selectDataCount(tableName)>0){
-            return AjaxResult.warn("表中有数据,不能删除!");
-        }
-        tableInfoService.dropMysqlTable(tableName);
-        return AjaxResult.success();
+        return tableInfoService.dropTable(tableName);
     }
 
+    //---------------------------------------通用接口-----------------------------------------------------
+
+
+
     @GetMapping("/mysqlTableFieldInfo")
     public AjaxResult mysqlTableFieldInfo(@RequestParam("tableName") String tableName){
         return AjaxResult.success(tableInfoService.mysqlTableFieldInfo(tableName));

+ 14 - 10
src/main/java/com/customer/mapper/TableInfoMapper.java

@@ -20,7 +20,7 @@ public interface TableInfoMapper {
     /**
      * 创建msql数据库表
      */
-    void createMysqlDataBase(@Param("dataBaseName") String dataBaseName,@Param("tableName") String tableName,@Param("filedList") List<String> filedList);
+    void createMysqlDataBase(@Param("dataBaseName") String dataBaseName,@Param("tableName") String tableName,@Param("tableComment") String tableComment,@Param("filedList") List<String> filedList);
 
     /**
      * 查询mysql数据库是否存在
@@ -35,7 +35,7 @@ public interface TableInfoMapper {
     /**
      * 查询数据库中的表信息
      */
-    List<TableInfoVO> tableInfoList(String databaseName, Map<String,Object> map, RowBounds rowBounds);
+    List<TableInfoVO> tableInfoList(@Param("databaseName") String databaseName,@Param("map") Map<String,Object> map, RowBounds rowBounds);
 
     /**
      * 查询表中有没有数据
@@ -50,7 +50,7 @@ public interface TableInfoMapper {
     /**
      * 修改mysql数据表
      */
-    void updateMysqlTable(@Param("dataBaseName") String dataBaseName,@Param("tableName") String tableName,@Param("filedList") List<String> filedList);
+    void updateMysqlTable(@Param("dataBaseName") String dataBaseName,@Param("tableName") String tableName,@Param("tableComment") String tableComment,@Param("filedList") List<String> filedList);
 
     /**
      * 获取表字段信息
@@ -116,15 +116,10 @@ public interface TableInfoMapper {
      */
     void createDmDataBase(String databaseName);
 
-    /**
-     * 使用达梦数据库
-     */
-    void useDmDataBase(String databaseName);
-
     /**
      * 创建数据表
      */
-    void createDmTable(@Param("tableName") String tableName,@Param("filedList") List<String> filedList);
+    void createDmTable(@Param("databaseName") String databaseName,@Param("tableName") String tableName,@Param("filedList") List<String> filedList);
 
     /**
      * 添加表注释
@@ -132,10 +127,19 @@ public interface TableInfoMapper {
     void addTableDescription(String description);
 
     /**
-     *
+     * 库中所有表信息
      */
     List<TableInfoVO> dmTableInfoList(String databaseName);
 
+    /**
+     * 查询表行数
+     */
+    int selectDmDataCount(String tableName);
+
+    /**
+     * 删除数据表
+     */
+    void dropDmTable(String tableName);
 
 
     //---------------------------------oracle------------------------------------------

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

@@ -1,6 +1,7 @@
 package com.customer.service;
 
 import com.customer.pojo.TableInfo;
+import com.customer.utils.AjaxResult;
 import com.customer.vo.TableInfoVO;
 import org.apache.ibatis.annotations.Param;
 
@@ -27,11 +28,33 @@ public interface ITableInfoService {
      */
     int mysqlTableExist(String dataBaseName,String tableName);
 
+
+
+
+
+
+
+
+
+
+
     /**
      * 查询数据库中的表信息
      */
     List<TableInfoVO> tableInfoList(Map<String,Object> map);
 
+    /**
+     * 删除表
+     */
+    AjaxResult dropTable(String tableName);
+
+    /**
+     * 创建表
+     */
+    AjaxResult createTable(Map<String,Object> map);
+
+
+
     /**
      * 查询表中有没有数据
      */
@@ -40,7 +63,9 @@ public interface ITableInfoService {
     /**
      * 删除表
      */
-    void dropMysqlTable(String tableName);
+//    void dropMysqlTable(String tableName);
+
+
 
     /**
      * 获取表字段信息

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

@@ -72,7 +72,7 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
         //切换数据源
         dynamicDataSource.changeDataSource(DataSourceUtils.changeDataSource(dataSource));
         redisTemplate.opsForValue().set("DataSource", JSON.toJSONString(dataSource));
-        System.err.println("当前数据源:" + dynamicDataSource.getConnection().getCatalog());
+        System.err.println("当前数据源:" + dataSource.toString());
     }
 
     @Override

+ 79 - 11
src/main/java/com/customer/service/impl/TableInfoServiceImpl.java

@@ -10,6 +10,8 @@ import com.customer.pojo.TableInfo;
 import com.customer.service.ICustomerService;
 import com.customer.service.IDataSourceService;
 import com.customer.service.ITableInfoService;
+import com.customer.utils.AjaxResult;
+import com.customer.utils.PageUtil;
 import com.customer.vo.TableInfoVO;
 import org.apache.ibatis.session.RowBounds;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -43,6 +45,7 @@ public class TableInfoServiceImpl implements ITableInfoService {
     public void createMysqlDataBase(Map<String, Object> map) {
         String dataBaseName = (String) map.get("dataBaseName");
         String tableName = (String) map.get("tableName");
+        String tableComment = (String) map.get("tableComment");
         List<TableInfo> filedList = JSON.parseArray(JSON.toJSONString(map.get("field")), TableInfo.class);
         List<String> list= filedList.stream().map(filed->{
             StringBuilder stringBuilder = new StringBuilder();
@@ -60,7 +63,7 @@ public class TableInfoServiceImpl implements ITableInfoService {
             stringBuilder.append("COMMENT '"+filed.getFieldDescription()+"'");
             return stringBuilder.toString();
         }).collect(Collectors.toList());
-        tableInfoMapper.createMysqlDataBase(dataBaseName,tableName,list);
+        tableInfoMapper.createMysqlDataBase(dataBaseName,tableName,tableComment,list);
     }
 
     @Override
@@ -116,19 +119,19 @@ public class TableInfoServiceImpl implements ITableInfoService {
     public List<TableInfoVO> tableInfoList(Map<String,Object> map) {
         int pageNum = (int)map.get("pageNum");
         int pageSize = (int)map.get("pageSize");
-
+        RowBounds rowBounds = PageUtil.getPageParam(pageNum,pageSize);
         //获取数据源信息
         if(redisTemplate.hasKey("DataSource")){
             DataSource dataSource =JSON.parseObject(redisTemplate.opsForValue().get("DataSource").toString(),DataSource.class);
             if(dataSource.getDatabaseType().equals(DataSourceType.MYSQL.getDataSourceName())){
-                return tableInfoMapper.tableInfoList(dataSource.getDatabaseName(),map,new RowBounds(pageNum,pageSize));
+                return tableInfoMapper.tableInfoList(dataSource.getDatabaseName(),map,rowBounds);
             }else if(dataSource.getDatabaseType().equals(DataSourceType.SQLSERVER.getDataSourceName())){
                 return tableInfoMapper.sqlServerTableInfoList(dataSource.getDatabaseName());
             }else if(dataSource.getDatabaseType().equals(DataSourceType.DM.getDataSourceName())){
                 return tableInfoMapper.dmTableInfoList(dataSource.getDatabaseName());
             }
         }
-            return tableInfoMapper.tableInfoList(url.substring(url.lastIndexOf("/") + 1),map,new RowBounds(pageNum,pageSize));
+        return tableInfoMapper.tableInfoList(url.substring(url.lastIndexOf("/") + 1),map,rowBounds);
 
     }
 
@@ -137,11 +140,77 @@ public class TableInfoServiceImpl implements ITableInfoService {
         return tableInfoMapper.selectDataCount(tableName);
     }
 
+//    @Override
+//    public void dropMysqlTable(String tableName) {
+//        tableInfoMapper.dropMysqlTable(tableName);
+//    }
+
     @Override
-    public void dropMysqlTable(String tableName) {
+    public AjaxResult dropTable(String tableName) {
+        if(redisTemplate.hasKey("DataSource")){
+            DataSource dataSource = JSON.parseObject(redisTemplate.opsForValue().get("DataSource").toString(),DataSource.class);
+            if(dataSource.getDatabaseType().equals(DataSourceType.MYSQL.getDataSourceName())){
+                if(tableInfoMapper.selectDataCount(tableName)>0){
+                    return AjaxResult.warn("表中有数据,不能删除!");
+                }
+                tableInfoMapper.dropMysqlTable(tableName);
+                return AjaxResult.success();
+            }else if(dataSource.getDatabaseType().equals(DataSourceType.SQLSERVER.getDataSourceName())){
+                if(tableInfoMapper.selectSqlServerDataCount(tableName)>0){
+                    return AjaxResult.warn("表中有数据,不能删除!");
+                }
+                tableInfoMapper.dropSqlserverTable(tableName);
+                return AjaxResult.success();
+            }else if(dataSource.getDatabaseType().equals(DataSourceType.DM.getDataSourceName())){
+                if(tableInfoMapper.selectDmDataCount(tableName)>0){
+                    return AjaxResult.warn("表中有数据,不能删除!");
+                }
+                tableInfoMapper.dropDmTable(tableName);
+                return AjaxResult.success();
+            }
+
+        }
+        if(tableInfoMapper.selectDataCount(tableName)>0){
+            return AjaxResult.warn("表中有数据,不能删除!");
+        }
         tableInfoMapper.dropMysqlTable(tableName);
+        return AjaxResult.success();
     }
 
+    @Override
+    public AjaxResult createTable(Map<String, Object> map) {
+        String dataBaseName = (String) map.get("dataBaseName");
+        String tableName = (String) map.get("tableName");
+        if(redisTemplate.hasKey("DataSource")){
+            DataSource dataSource = JSON.parseObject(redisTemplate.opsForValue().get("DataSource").toString(),DataSource.class);
+            if(dataSource.getDatabaseType().equals(DataSourceType.MYSQL.getDataSourceName())){
+                if(tableInfoMapper.mysqlTableExist(dataBaseName,tableName)>0){
+                    return AjaxResult.warn("当前数据库中表已存在");
+                }
+                createMysqlDataBase(map);
+                return AjaxResult.success();
+            }else if(dataSource.getDatabaseType().equals(DataSourceType.SQLSERVER.getDataSourceName())){
+                if(sqlServerTableExist(dataBaseName,tableName)>0){
+                    return AjaxResult.warn("当前数据库中表已存在");
+                }
+                createSqlServerDataBase(map);
+                return AjaxResult.success();
+            }else if(dataSource.getDatabaseType().equals(DataSourceType.DM.getDataSourceName())){
+                if(tableInfoMapper.dmTableExist(dataBaseName,tableName)>0){
+                    return AjaxResult.warn("当前数据库中表已存在");
+                }
+                createDmDataBase(map);
+                return AjaxResult.success();
+            }
+        }
+        if(tableInfoMapper.mysqlTableExist(dataBaseName,tableName)>0){
+            return AjaxResult.warn("当前数据库中表已存在");
+        }
+        createMysqlDataBase(map);
+        return AjaxResult.success();
+    }
+
+
     @Override
     public List<TableInfo> mysqlTableFieldInfo(String tableName) {
         String dataBaseName = url.substring(url.lastIndexOf("/") + 1);
@@ -155,6 +224,7 @@ public class TableInfoServiceImpl implements ITableInfoService {
     @Override
     public void updateMysqlTable(Map<String, Object> map) {
         String tableName = (String) map.get("tableName");
+        String tableComment = (String) map.get("tableComment");
         List<TableInfo> filedList = JSON.parseArray(JSON.toJSONString(map.get("field")), TableInfo.class);
         List<String> list= filedList.stream().map(filed->{
             StringBuilder stringBuilder = new StringBuilder();
@@ -175,10 +245,10 @@ public class TableInfoServiceImpl implements ITableInfoService {
 
         if(redisTemplate.hasKey("DataSource")){
             DataSource dataSource =JSON.parseObject(redisTemplate.opsForValue().get("DataSource").toString(),DataSource.class);
-            tableInfoMapper.updateMysqlTable(dataSource.getDatabaseName(),tableName,list);
+            tableInfoMapper.updateMysqlTable(dataSource.getDatabaseName(),tableName,tableComment,list);
         }else {
             String dataBaseName = url.substring(url.lastIndexOf("/") + 1);
-            tableInfoMapper.updateMysqlTable(dataBaseName,tableName,list);
+            tableInfoMapper.updateMysqlTable(dataBaseName,tableName,tableComment,list);
         }
 
     }
@@ -288,13 +358,11 @@ public class TableInfoServiceImpl implements ITableInfoService {
             return stringBuilder.toString();
         }).collect(Collectors.toList());
         if(tableInfoMapper.dmDataBaseExist(dataBaseName)>0){
-            tableInfoMapper.useDmDataBase(dataBaseName);
-            tableInfoMapper.createDmTable(tableName,list);
+            tableInfoMapper.createDmTable(dataBaseName,tableName,list);
             descriptionList.stream().forEach(f->tableInfoMapper.addTableDescription(f));
         }else {
             tableInfoMapper.createDmDataBase(dataBaseName);
-            tableInfoMapper.useDmDataBase(dataBaseName);
-            tableInfoMapper.createDmTable(tableName,list);
+            tableInfoMapper.createDmTable(dataBaseName,tableName,list);
             descriptionList.stream().forEach(f->tableInfoMapper.addTableDescription(f));
         }
     }

+ 1 - 1
src/main/java/com/customer/vo/TableInfoVO.java

@@ -19,7 +19,7 @@ public class TableInfoVO {
     /**
      * 创建时间
      */
-    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
+    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
     private Date createTime;
 
     /**

+ 11 - 6
src/main/resources/mapper/TableInfoMapper.xml

@@ -24,7 +24,7 @@
         <foreach collection="filedList" item="filed" separator=",">
             ${filed}
         </foreach>
-        );
+        )COMMENT #{tableComment};
     </update>
 
     <select id="mysqlDataBaseExist" resultType="int">
@@ -63,7 +63,7 @@
         <foreach collection="filedList" item="filed" separator=",">
             ${filed}
         </foreach>
-        );
+        )COMMENT #{tableComment};
     </update>
 
     <select id="mysqlTableFieldInfo" resultMap="mysqlTableInfoResult">
@@ -96,9 +96,7 @@
     </update>
 
     <update id="createSqlServerTable">
-
-        use ${dataBaseName};
-        create table ${tableName}
+        create table ${dataBaseName}.dbo.${tableName}
         (
         <foreach collection="filedList" item="filed" separator=",">
             ${filed}
@@ -163,7 +161,7 @@
 
     <update id="createDmTable">
 
-        create table ${tableName}
+        create table ${databaseName}.${tableName}
         (
             <foreach collection="filedList" item="filed" separator=",">
                 ${filed}
@@ -183,6 +181,13 @@
 
     </select>
 
+    <select id="selectDmDataCount" resultType="int">
+        select count(1) from ${tableName}
+    </select>
+
+    <delete id="dropDmTable">
+        drop table ${tableName}
+    </delete>
 
     <update id="createOracleUser">
         CREATE USER ${username} IDENTIFIED BY ${password} DEFAULT TABLESPACE users QUOTA unlimited ON users