فهرست منبع

mysql查询数据库中的表单

xuezizhuo 2 سال پیش
والد
کامیت
f47ae674b1

+ 7 - 3
src/main/java/com/customer/controller/TableInfoController.java

@@ -5,10 +5,12 @@ import com.customer.config.DynamicDataSource;
 import com.customer.config.GenConfig;
 import com.customer.service.ITableInfoService;
 import com.customer.utils.AjaxResult;
+import com.customer.utils.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
+import java.sql.SQLException;
 import java.util.Map;
 
 @RestController
@@ -18,9 +20,6 @@ public class TableInfoController {
     @Resource
     private ITableInfoService tableInfoService;
 
-    @Autowired
-    private DynamicDataSource dynamicDataSource;
-
     @PostMapping("/createDatabase")
     public AjaxResult crateDatabase(@RequestBody Map<String, Object> map){
         String dataBaseName = (String) map.get("dataBaseName");
@@ -64,4 +63,9 @@ public class TableInfoController {
         return AjaxResult.success();
     }
 
+    @GetMapping("/tableInfoList")
+    public AjaxResult tableInfoList(@RequestParam("databaseName") String databaseName) throws SQLException {
+        return AjaxResult.success(tableInfoService.tableInfoList(databaseName));
+    }
+
 }

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

@@ -1,6 +1,7 @@
 package com.customer.mapper;
 
 
+import com.customer.vo.TableInfoVO;
 import org.apache.ibatis.annotations.Param;
 
 import java.util.List;
@@ -28,6 +29,11 @@ public interface TableInfoMapper {
      */
     int mysqlTableExist(@Param("dataBaseName") String dataBaseName,@Param("tableName") String tableName);
 
+    /**
+     * 查询数据库中的表信息
+     */
+    List<TableInfoVO> tableInfoList(String databaseName);
+
     //---------------------------------sqlServer------------------------------------------
 
     /**

+ 7 - 0
src/main/java/com/customer/service/ITableInfoService.java

@@ -1,7 +1,9 @@
 package com.customer.service;
 
+import com.customer.vo.TableInfoVO;
 import org.apache.ibatis.annotations.Param;
 
+import java.sql.SQLException;
 import java.util.List;
 import java.util.Map;
 
@@ -24,6 +26,11 @@ public interface ITableInfoService {
      */
     int mysqlTableExist(String dataBaseName,String tableName);
 
+    /**
+     * 查询数据库中的表信息
+     */
+    List<TableInfoVO> tableInfoList(String databaseName) throws SQLException;
+
     //---------------------------------sqlServer------------------------------------------
 
     /**

+ 15 - 0
src/main/java/com/customer/service/impl/TableInfoServiceImpl.java

@@ -6,12 +6,14 @@ import com.customer.config.GenConfig;
 import com.customer.mapper.TableInfoMapper;
 import com.customer.pojo.TableInfo;
 import com.customer.service.ITableInfoService;
+import com.customer.vo.TableInfoVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.StringUtils;
 
 import javax.annotation.Resource;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -23,6 +25,9 @@ public class TableInfoServiceImpl implements ITableInfoService {
     @Resource
     private TableInfoMapper tableInfoMapper;
 
+    @Autowired
+    private DynamicDataSource dynamicDataSource;
+
     @Override
     @Transactional
     public void createMysqlDataBase(Map<String, Object> map) {
@@ -97,6 +102,16 @@ public class TableInfoServiceImpl implements ITableInfoService {
         return tableInfoMapper.mysqlTableExist(dataBaseName,tableName);
     }
 
+    @Override
+    public List<TableInfoVO> tableInfoList(String databaseName) throws SQLException {
+        if(StringUtils.hasLength(databaseName)){
+            return tableInfoMapper.tableInfoList(databaseName);
+        }else {
+            String database = dynamicDataSource.getConnection().getCatalog();
+            return tableInfoMapper.tableInfoList(database);
+        }
+    }
+
     @Override
     public int sqlServerTableExist(String dataBaseName,String tableName) {
         StringBuilder stringBuilder = new StringBuilder();

+ 29 - 0
src/main/java/com/customer/vo/TableInfoVO.java

@@ -0,0 +1,29 @@
+package com.customer.vo;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+import java.time.LocalDateTime;
+import java.util.Date;
+
+@Data
+@Accessors(chain = true)
+public class TableInfoVO {
+
+    /**
+     * 表名称
+     */
+    private String tableName;
+
+    /**
+     * 创建时间
+     */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    private Date createTime;
+
+    /**
+     * 表描述
+     */
+    private String tableComment;
+}

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

@@ -26,6 +26,10 @@
         SELECT count(1) FROM information_schema.TABLES t, information_schema.SCHEMATA n WHERE t.table_name = #{tableName} AND n.SCHEMA_NAME = #{dataBaseName};
     </select>
 
+    <select id="tableInfoList" resultType="com.customer.vo.TableInfoVO">
+        select table_name tableName,create_time createTime,table_comment tableComment from information_schema.tables where table_schema=#{databaseName}
+    </select>
+
 
 
     <select id="sqlServerTableExist" resultType="int">