xuezizhuo 2 years ago
parent
commit
871f8e1f46

+ 18 - 2
.idea/zkqy.iml

@@ -72,10 +72,26 @@
     <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.7.8" level="project" />
     <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.7.8" level="project" />
     <orderEntry type="library" name="Maven: com.zaxxer:HikariCP:4.0.3" level="project" />
-    <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.36" level="project" />
     <orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.3.25" level="project" />
-    <orderEntry type="library" name="Maven: org.springframework:spring-tx:5.3.25" level="project" />
     <orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.24" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-redis:2.7.8" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.data:spring-data-redis:2.7.7" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.data:spring-data-keyvalue:2.7.7" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.7.7" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-tx:5.3.25" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-oxm:5.3.25" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-context-support:5.3.25" level="project" />
+    <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.36" level="project" />
+    <orderEntry type="library" name="Maven: io.lettuce:lettuce-core:6.1.10.RELEASE" level="project" />
+    <orderEntry type="library" name="Maven: io.netty:netty-common:4.1.87.Final" level="project" />
+    <orderEntry type="library" name="Maven: io.netty:netty-handler:4.1.87.Final" level="project" />
+    <orderEntry type="library" name="Maven: io.netty:netty-resolver:4.1.87.Final" level="project" />
+    <orderEntry type="library" name="Maven: io.netty:netty-buffer:4.1.87.Final" level="project" />
+    <orderEntry type="library" name="Maven: io.netty:netty-transport-native-unix-common:4.1.87.Final" level="project" />
+    <orderEntry type="library" name="Maven: io.netty:netty-codec:4.1.87.Final" level="project" />
+    <orderEntry type="library" name="Maven: io.netty:netty-transport:4.1.87.Final" level="project" />
+    <orderEntry type="library" name="Maven: io.projectreactor:reactor-core:3.4.26" level="project" />
+    <orderEntry type="library" name="Maven: org.reactivestreams:reactive-streams:1.0.4" level="project" />
     <orderEntry type="library" name="Maven: com.alibaba:druid:1.1.13" level="project" />
     <orderEntry type="library" name="Maven: com.microsoft.sqlserver:mssql-jdbc:10.2.3.jre8" level="project" />
     <orderEntry type="library" name="Maven: com.baomidou:dynamic-datasource-spring-boot-starter:3.5.0" level="project" />

+ 5 - 0
pom.xml

@@ -41,6 +41,11 @@
             <artifactId>lombok</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+        </dependency>
+
         <!-- alibaba 数据连接池 -->
         <dependency>
             <groupId>com.alibaba</groupId>

+ 1 - 0
src/main/java/com/customer/CustomerApplication.java

@@ -5,6 +5,7 @@ import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+import org.springframework.cache.annotation.CacheEvict;
 
 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
 @MapperScan(basePackages = "com.customer.mapper")

+ 29 - 0
src/main/java/com/customer/config/RedisConfig.java

@@ -0,0 +1,29 @@
+package com.customer.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.connection.RedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.RedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+@Configuration
+public class RedisConfig {
+    @Bean(name="redisTemplate")
+    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
+        RedisTemplate<String, String> template = new RedisTemplate<>();
+        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
+        template.setConnectionFactory(factory);
+        //key序列化方式
+        template.setKeySerializer(redisSerializer);
+        //value序列化
+        template.setValueSerializer(redisSerializer);
+        //value hashmap序列化
+        template.setHashValueSerializer(redisSerializer);
+        //key haspmap序列化
+        template.setHashKeySerializer(redisSerializer);
+        //
+        return template;
+    }
+
+}

+ 18 - 0
src/main/java/com/customer/config/RedisDataClear.java

@@ -0,0 +1,18 @@
+package com.customer.config;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Component;
+
+@Component
+
+public class RedisDataClear implements CommandLineRunner {
+    @Autowired
+    private RedisTemplate redisTemplate;
+
+    @Override
+    public void run(String... args) throws Exception {
+        redisTemplate.getConnectionFactory().getConnection().flushDb();
+    }
+}

+ 3 - 3
src/main/java/com/customer/controller/DataSourceController.java

@@ -39,10 +39,10 @@ public class DataSourceController {
      */
     @GetMapping("/changeDataSource")
     public AjaxResult changeDataSource(@RequestParam("id") Long id) throws Exception {
-        //进入主数据源查询需要切换的数据源信息
-        DataSource dataSource =dataSourceService.mainDataSource(id);
+//        //进入主数据源查询需要切换的数据源信息
+//        DataSource dataSource =dataSourceService.mainDataSource(id);
         //切换数据源
-        dataSourceService.changeDataSource(DataSourceUtils.changeDataSource(dataSource));
+        dataSourceService.changeDataSource(id);
         return AjaxResult.success();
     }
 

+ 14 - 14
src/main/java/com/customer/controller/TableInfoController.java

@@ -67,8 +67,8 @@ public class TableInfoController {
     }
 
     @GetMapping("/tableInfoList")
-    public AjaxResult tableInfoList(@RequestParam("databaseName") String databaseName){
-        return AjaxResult.success(tableInfoService.tableInfoList(databaseName));
+    public AjaxResult tableInfoList(){
+        return AjaxResult.success(tableInfoService.tableInfoList());
     }
 
     @GetMapping("/removeTable/{tableName}")
@@ -99,10 +99,10 @@ public class TableInfoController {
     /**
      * 获取sqlServer当前库中所有表信息
      */
-    @GetMapping("/sqlServerTableInfoList")
-    public AjaxResult sqlServerTableInfoList(){
-        return AjaxResult.success(tableInfoService.sqlServerTableInfoList());
-    }
+//    @GetMapping("/sqlServerTableInfoList")
+//    public AjaxResult sqlServerTableInfoList(){
+//        return AjaxResult.success(tableInfoService.sqlServerTableInfoList());
+//    }
 
     /**
      * 删除sqlserver数据表
@@ -129,14 +129,14 @@ public class TableInfoController {
         return AjaxResult.success();
     }
 
-
-    /**
-     * 获取DM当前库中所有表信息
-     */
-    @GetMapping("/getDataBasesInfo")
-    public AjaxResult sqlServerTableInfoListtest(String name){
-        return AjaxResult.success(tableInfoService.getDataBasesInfo(name));
-    }
+//
+//    /**
+//     * 获取DM当前库中所有表信息
+//     */
+//    @GetMapping("/getDataBasesInfo")
+//    public AjaxResult sqlServerTableInfoListtest(String name){
+//        return AjaxResult.success(tableInfoService.getDataBasesInfo(name));
+//    }
 
 
 }

+ 3 - 3
src/main/java/com/customer/mapper/TableInfoMapper.java

@@ -81,7 +81,7 @@ public interface TableInfoMapper {
     /**
      * 获取当前库中所有表信息
      */
-    List<TableInfoVO> sqlServerTableInfoList();
+    List<TableInfoVO> sqlServerTableInfoList(String databaseName);
 
     /**
      * 查询表中是否有数据
@@ -131,9 +131,9 @@ public interface TableInfoMapper {
     void addTableDescription(String description);
 
     /**
-     * 根据数据库名称获取当前数据源所有表info
+     *
      */
-    List<Map<String,Object>> getDataBasesInfo(@Param("DBname") String DBname);
+    List<TableInfoVO> dmTableInfoList(String databaseName);
 
 
 

+ 1 - 1
src/main/java/com/customer/service/IDataSourceService.java

@@ -20,7 +20,7 @@ public interface IDataSourceService extends IService<DataSource> {
     /**
      * 切换数据源
      */
-    void changeDataSource(GenConfig genConfig) throws Exception;
+    void changeDataSource(Long id) throws Exception;
 
     /**
      * 获取当前数据源数据库

+ 9 - 9
src/main/java/com/customer/service/ITableInfoService.java

@@ -30,7 +30,7 @@ public interface ITableInfoService {
     /**
      * 查询数据库中的表信息
      */
-    List<TableInfoVO> tableInfoList(String databaseName);
+    List<TableInfoVO> tableInfoList();
 
     /**
      * 查询表中有没有数据
@@ -64,10 +64,10 @@ public interface ITableInfoService {
      */
     int sqlServerTableExist(String dataBaseName,String tableName);
 
-    /**
-     * 获取当前库中所有表信息
-     */
-    List<TableInfoVO> sqlServerTableInfoList();
+//    /**
+//     * 获取当前库中所有表信息
+//     */
+//    List<TableInfoVO> sqlServerTableInfoList();
 
     /**
      * 查询表中是否有数据
@@ -84,10 +84,10 @@ public interface ITableInfoService {
      */
     void updateSqlserverTable(Map<String, Object> map);
 
-    /**
-     * 根据数据库名称获取当前数据源所有表info
-     */
-    List<Map<String,Object>> getDataBasesInfo(@Param("DBname") String DBname);
+//    /**
+//     * 根据数据库名称获取当前数据源所有表info
+//     */
+//    List<Map<String,Object>> getDataBasesInfo(@Param("DBname") String DBname);
 
     //---------------------------------DM------------------------------------------
 

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

@@ -1,5 +1,6 @@
 package com.customer.service.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.baomidou.dynamic.datasource.annotation.DS;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.customer.config.DynamicDataSource;
@@ -7,12 +8,16 @@ import com.customer.config.GenConfig;
 import com.customer.mapper.DataSourceMapper;
 import com.customer.pojo.Customer;
 import com.customer.pojo.DataSource;
+import com.customer.pojo.TableInfo;
 import com.customer.service.IDataSourceService;
+import com.customer.utils.DataSourceUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.util.List;
 
 @Service
 public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSource> implements IDataSourceService {
@@ -23,6 +28,9 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
     @Autowired
     private DynamicDataSource dynamicDataSource;
 
+    @Resource
+    private RedisTemplate redisTemplate;
+
     // 动态注入数据库信息
     @Value("${spring.datasource.dynamic.datasource.master.url}")
     private String url;
@@ -52,18 +60,19 @@ public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSou
         DynamicDataSource.clearDataSource();
         //切换数据源
         dynamicDataSource.changeDataSource(genConfig);
-        System.err.println(dataSourceMapper.selectById(id));
         return dataSourceMapper.selectById(id);
     }
 
     @Override
-    public void changeDataSource(GenConfig genConfig) throws Exception {
+    public void changeDataSource(Long id) throws Exception {
+        //进入主数据源查询需要切换的数据源信息
+        DataSource dataSource =mainDataSource(id);
         //切换数据源之前先清空
         DynamicDataSource.clearDataSource();
         //切换数据源
-        dynamicDataSource.changeDataSource(genConfig);
+        dynamicDataSource.changeDataSource(DataSourceUtils.changeDataSource(dataSource));
+        redisTemplate.opsForValue().set("DataSource", JSON.toJSONString(dataSource));
         System.err.println("当前数据源:" + dynamicDataSource.getConnection().getCatalog());
-
     }
 
     @Override

+ 32 - 10
src/main/java/com/customer/service/impl/TableInfoServiceImpl.java

@@ -3,13 +3,17 @@ package com.customer.service.impl;
 import com.alibaba.fastjson.JSON;
 import com.customer.config.DynamicDataSource;
 import com.customer.config.GenConfig;
+import com.customer.constant.DataSourceType;
 import com.customer.mapper.TableInfoMapper;
+import com.customer.pojo.DataSource;
 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;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.StringUtils;
@@ -27,6 +31,12 @@ public class TableInfoServiceImpl implements ITableInfoService {
     @Resource
     private TableInfoMapper tableInfoMapper;
 
+    @Resource
+    private RedisTemplate redisTemplate;
+
+    @Value("${spring.datasource.dynamic.datasource.master.url}")
+    private String url;
+
     @Override
     @Transactional
     public void createMysqlDataBase(Map<String, Object> map) {
@@ -102,8 +112,20 @@ public class TableInfoServiceImpl implements ITableInfoService {
     }
 
     @Override
-    public List<TableInfoVO> tableInfoList(String databaseName) {
-            return tableInfoMapper.tableInfoList(databaseName);
+    public List<TableInfoVO> tableInfoList() {
+        //获取数据源信息
+        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());
+            }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));
+
     }
 
     @Override
@@ -154,10 +176,10 @@ public class TableInfoServiceImpl implements ITableInfoService {
         return tableInfoMapper.sqlServerTableExist(stringBuilder.toString());
     }
 
-    @Override
-    public List<TableInfoVO> sqlServerTableInfoList() {
-        return tableInfoMapper.sqlServerTableInfoList();
-    }
+//    @Override
+//    public List<TableInfoVO> sqlServerTableInfoList() {
+//        return tableInfoMapper.sqlServerTableInfoList();
+//    }
 
     @Override
     public int selectSqlServerDataCount(String tableName) {
@@ -205,10 +227,10 @@ public class TableInfoServiceImpl implements ITableInfoService {
         tableInfoMapper.updateSqlserverTable(tableName,list,descriptionList);
     }
 
-    @Override
-    public List<Map<String, Object>> getDataBasesInfo(String DBname) {
-        return tableInfoMapper.getDataBasesInfo(DBname);
-    }
+//    @Override
+//    public List<Map<String, Object>> getDataBasesInfo(String DBname) {
+//        return tableInfoMapper.getDataBasesInfo(DBname);
+//    }
 
     @Override
     public int dmTableExist(String dataBaseName, String tableName) {

+ 17 - 0
src/main/resources/application.yml

@@ -19,6 +19,23 @@ spring:
           password: zkqy8888
           driver-class-name: com.mysql.cj.jdbc.Driver
           type: com.alibaba.druid.pool.DruidDataSource
+  redis:
+    host: localhost
+    port: 6379
+    database: 1
+    password: 123456
+    # 连接超时时间
+    timeout: 10s
+    lettuce:
+      pool:
+        # 连接池中的最小空闲连接
+        min-idle: 0
+        # 连接池中的最大空闲连接
+        max-idle: 8
+        # 连接池的最大数据库连接数
+        max-active: 8
+        # #连接池最大阻塞等待时间(使用负值表示没有限制)
+        max-wait: -1ms
 logging:
   level:
     com.customer.mapper : debug

+ 10 - 7
src/main/resources/mapper/TableInfoMapper.xml

@@ -113,10 +113,11 @@
         select
             a.name AS tableName,
             a.create_date AS createTime,
-            CONVERT(NVARCHAR(100),isnull(g.[value],'')) AS tableComment
-        from
-            sys.tables a left join sys.extended_properties g
-                                   on (a.object_id = g.major_id AND g.minor_id = 0)
+            CONVERT ( NVARCHAR ( 100 ), isnull( g.[value], '' ) ) AS tableComment
+        from ${databaseName}.sys.tables a
+                 LEFT JOIN sys.extended_properties g ON ( a.object_id = g.major_id AND g.minor_id = 0 )
+        where a.type = 'U';
+
     </select>
 
     <select id="selectSqlServerDataCount" resultType="int">
@@ -170,9 +171,11 @@
            ${description}
     </update>
 
-    <select id="getDataBasesInfo" resultType="java.util.HashMap">
-select * from dba_tables a LEFT OUTER JOIN  USER_TAB_COMMENTS b on a.table_name = b.table_name
-                                                            where a.owner=#{DBname}
+    <select id="dmTableInfoList" resultType="com.customer.vo.TableInfoVO">
+        select a.object_name as tableName,b.comment$ as tableComment,a.created as createTime
+        from dba_objects a
+                 left join SYSTABLECOMMENTS b on b.tvname = a.object_name
+        where a.object_type = 'TABLE' and a.owner = #{databaseName}
 
     </select>