Răsfoiți Sursa

数据源接口

xuezizhuo 2 ani în urmă
părinte
comite
5e67186f67

+ 1 - 1
.idea/misc.xml

@@ -7,7 +7,7 @@
       </list>
     </option>
   </component>
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_16" project-jdk-name="17" project-jdk-type="JavaSDK">
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_16" project-jdk-name="1.8" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/out" />
   </component>
 </project>

+ 0 - 1
pom.xml

@@ -90,7 +90,6 @@
             <version>3.12.0</version>
         </dependency>
 
-
     </dependencies>
 
     <build>

+ 12 - 3
src/main/java/com/customer/controller/CustomerController.java

@@ -1,16 +1,24 @@
 package com.customer.controller;
 
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.customer.config.DynamicDataSource;
 import com.customer.config.GenConfig;
 import com.customer.constant.DataSourceType;
 import com.customer.pojo.Customer;
+import com.customer.pojo.DataSource;
 import com.customer.service.ICustomerService;
+import com.customer.service.IDataSourceService;
+import com.customer.utils.AjaxResult;
+import com.customer.utils.DataSourceUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 
 /**
@@ -37,9 +45,9 @@ public class CustomerController {
             GenConfig genConfig = new GenConfig();
             genConfig.setDbtype("mysql");
             genConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
-            genConfig.setUrl("jdbc:mysql://localhost:3306/test1");
+            genConfig.setUrl("jdbc:mysql://192.168.110.15:3306/zkqy-call");
             genConfig.setUsername("root");
-            genConfig.setPassword("root");
+            genConfig.setPassword("zkqy8888");
             //切换数据源之前先清空
             DynamicDataSource.clearDataSource();
             //切换数据源
@@ -60,7 +68,7 @@ public class CustomerController {
             genConfig.setDbtype("oracle");
             genConfig.setDriverClassName("oracle.jdbc.driver.OracleDriver");
             genConfig.setUrl("jdbc:oracle:thin:@localhost:1521:orcl");
-            genConfig.setUsername("scott");
+            genConfig.setUsername("OTEST");
             genConfig.setPassword("123456");
             //切换数据源之前先清空
             DynamicDataSource.clearDataSource();
@@ -84,4 +92,5 @@ public class CustomerController {
     }
 
 
+
 }

+ 49 - 0
src/main/java/com/customer/controller/DataSourceController.java

@@ -0,0 +1,49 @@
+package com.customer.controller;
+
+import com.customer.pojo.DataSource;
+import com.customer.service.IDataSourceService;
+import com.customer.utils.AjaxResult;
+import com.customer.utils.DataSourceUtils;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+
+@RestController
+@RequestMapping("/dataSource")
+public class DataSourceController {
+
+    @Resource
+    private IDataSourceService dataSourceService;
+
+    /**
+     * 数据源信息列表
+     */
+    @GetMapping("/list")
+    public AjaxResult dataSourceList(){
+        return AjaxResult.success(dataSourceService.list());
+    }
+
+    /**
+     * 新增数据源
+     */
+    @PostMapping("/save")
+    public AjaxResult addDataSource(@RequestBody DataSource dataSource){
+        if(dataSourceService.selectDatabaseExist(dataSource.getDatabaseIp(),dataSource.getDatabaseName(),dataSource.getPortNumber())>0){
+            return AjaxResult.error("数据库已存在");
+        }
+        return AjaxResult.success(dataSourceService.save(dataSource));
+    }
+
+    /**
+     * 切换数据源
+     */
+    @GetMapping("/changeDataSource")
+    public AjaxResult changeDataSource(@RequestParam("id") Long id) throws Exception {
+        //进入主数据源查询需要切换的数据源信息
+        DataSource dataSource =dataSourceService.mainDataSource(id);
+        //切换数据源
+        dataSourceService.changeDataSource(DataSourceUtils.changeDataSource(dataSource));
+        return AjaxResult.success();
+    }
+
+}

+ 9 - 0
src/main/java/com/customer/controller/TableInfoController.java

@@ -54,4 +54,13 @@ public class TableInfoController {
         return AjaxResult.success();
     }
 
+    @PostMapping("/createOracleTable")
+    public AjaxResult createOracleTable(@RequestBody Map<String, Object> map){
+        String tableName = (String) map.get("tableName");
+        if(tableInfoService.oracleTableExist(tableName)>0){
+            return AjaxResult.warn("当前数据库中表已存在");
+        }
+        return AjaxResult.success();
+    }
+
 }

+ 13 - 0
src/main/java/com/customer/mapper/DataSourceMapper.java

@@ -0,0 +1,13 @@
+package com.customer.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.customer.pojo.DataSource;
+import org.apache.ibatis.annotations.Param;
+
+public interface DataSourceMapper extends BaseMapper<DataSource> {
+
+    /**
+     * 查询同一数据源下数据库是否存在
+     */
+    int selectDatabaseExist(@Param("databaseIp") String databaseIp,@Param("databaseName") String databaseName,@Param("portNumber") Long portNumber);
+}

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

@@ -82,6 +82,13 @@ public interface TableInfoMapper {
      */
     void addTableDescription(String description);
 
+    //---------------------------------oracle------------------------------------------
+
+    /**
+     * 判断当前用户下表是否存在
+     */
+    int oracleTableExist(String tableName);
+
 
 
 

+ 55 - 0
src/main/java/com/customer/pojo/DataSource.java

@@ -0,0 +1,55 @@
+package com.customer.pojo;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+import java.io.Serializable;
+
+@Data
+@Accessors(chain = true)
+@TableName("data_source")
+public class DataSource implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 字典编码
+     */
+    @TableId(value = "id", type = IdType.AUTO)
+    private Long id;
+
+    /**
+     * 数据库名
+     */
+    private String databaseName;
+
+    /**
+     * 数据库IP
+     */
+    private String databaseIp;
+
+    /**
+     * 用户名
+     */
+    private String username;
+
+    /**
+     * 密码
+     */
+    private String password;
+
+    /**
+     * 端口号
+     */
+    private Long portNumber;
+
+    /**
+     * 数据库类型
+     */
+    private String databaseType;
+
+
+}

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

@@ -0,0 +1,24 @@
+package com.customer.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.customer.config.GenConfig;
+import com.customer.pojo.DataSource;
+import org.apache.ibatis.annotations.Param;
+
+public interface IDataSourceService extends IService<DataSource> {
+
+    /**
+     * 查询同一数据源下数据库是否存在
+     */
+    int selectDatabaseExist(String databaseIp, String databaseName, Long portNumber);
+
+    /**
+     * 主数据源接口
+     */
+    DataSource mainDataSource(Long id) throws Exception;
+
+    /**
+     * 切换数据源
+     */
+    void changeDataSource(GenConfig genConfig) throws Exception;
+}

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

@@ -48,6 +48,12 @@ public interface ITableInfoService {
      */
     void createDmDataBase(Map<String,Object> map);
 
+    //---------------------------------oracle------------------------------------------
+
+    /**
+     * 判断当前用户下表是否存在
+     */
+    int oracleTableExist(String tableName);
 
 
 }

+ 4 - 0
src/main/java/com/customer/service/impl/CustomerServiceImpl.java

@@ -22,6 +22,10 @@ import java.util.List;
 @Service
 public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
 
+    @Resource
+    private CustomerMapper customerMapper;
+
+
 //    @Resource
 //    private CustomerMapper customerMapper;
 //

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

@@ -0,0 +1,68 @@
+package com.customer.service.impl;
+
+import com.baomidou.dynamic.datasource.annotation.DS;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.customer.config.DynamicDataSource;
+import com.customer.config.GenConfig;
+import com.customer.mapper.DataSourceMapper;
+import com.customer.pojo.Customer;
+import com.customer.pojo.DataSource;
+import com.customer.service.IDataSourceService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+
+@Service
+public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSource> implements IDataSourceService {
+
+    @Resource
+    private DataSourceMapper dataSourceMapper;
+
+    @Autowired
+    private DynamicDataSource dynamicDataSource;
+
+    // 动态注入数据库信息
+    @Value("${spring.datasource.dynamic.datasource.master.url}")
+    private String url;
+    @Value("${spring.datasource.dynamic.datasource.master.username}")
+    private String username;
+    @Value("${spring.datasource.dynamic.datasource.master.password}")
+    private String password;
+    @Value("${spring.datasource.dynamic.datasource.master.driver-class-name}")
+    private String driverClassName;
+    @Value("${spring.datasource.dynamic.datasource.master.type}")
+    private String dataType;
+
+    @Override
+    public int selectDatabaseExist(String databaseIp, String databaseName, Long portNumber) {
+        return dataSourceMapper.selectDatabaseExist(databaseIp,databaseName,portNumber);
+    }
+
+    @Override
+    public DataSource mainDataSource(Long id)  throws Exception{
+        GenConfig genConfig = new GenConfig();
+        genConfig.setUrl(url);
+        genConfig.setDriverClassName(driverClassName);
+        genConfig.setUsername(username);
+        genConfig.setPassword(password);
+        genConfig.setDbtype(dataType);
+        //切换数据源之前先清空
+        DynamicDataSource.clearDataSource();
+        //切换数据源
+        dynamicDataSource.changeDataSource(genConfig);
+        System.err.println(dataSourceMapper.selectById(id));
+        return dataSourceMapper.selectById(id);
+    }
+
+    @Override
+    public void changeDataSource(GenConfig genConfig) throws Exception {
+        //切换数据源之前先清空
+        DynamicDataSource.clearDataSource();
+        //切换数据源
+        dynamicDataSource.changeDataSource(genConfig);
+        System.err.println("当前数据源:" + dynamicDataSource.getConnection());
+
+    }
+}

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

@@ -156,4 +156,9 @@ public class TableInfoServiceImpl implements ITableInfoService {
             descriptionList.stream().forEach(f->tableInfoMapper.addTableDescription(f));
         }
     }
+
+    @Override
+    public int oracleTableExist(String tableName) {
+        return tableInfoMapper.oracleTableExist(tableName);
+    }
 }

+ 37 - 0
src/main/java/com/customer/utils/DataSourceUtils.java

@@ -0,0 +1,37 @@
+package com.customer.utils;
+
+import com.customer.config.DynamicDataSource;
+import com.customer.config.GenConfig;
+import com.customer.constant.DataSourceType;
+import com.customer.pojo.DataSource;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.sql.SQLException;
+
+public class DataSourceUtils {
+
+    public static GenConfig changeDataSource(DataSource dataSource) {
+
+
+        GenConfig genConfig = new GenConfig();
+        genConfig.setDbtype(dataSource.getDatabaseType());
+        genConfig.setUsername(dataSource.getUsername());
+        genConfig.setPassword(dataSource.getPassword());
+        if(dataSource.getDatabaseType().equals(DataSourceType.MYSQL.getDataSourceName())){
+            genConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
+            genConfig.setUrl("jdbc:mysql://"+dataSource.getDatabaseIp()+":"+dataSource.getPortNumber()+"/"+dataSource.getDatabaseName());
+        }else if(dataSource.getDatabaseType().equals(DataSourceType.SQLSERVER.getDataSourceName())){
+            genConfig.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
+            genConfig.setUrl("jdbc:sqlserver://"+dataSource.getDatabaseIp()+":"+dataSource.getPortNumber()+";DatabaseName="+dataSource.getDatabaseName());
+        }else if(dataSource.getDatabaseType().equals(DataSourceType.ORACLE.getDataSourceName())){
+            genConfig.setDriverClassName("oracle.jdbc.driver.OracleDriver");
+            genConfig.setUrl("jdbc:oracle:thin:@"+dataSource.getDatabaseIp()+":"+dataSource.getPortNumber()+dataSource.getDatabaseName());
+        }else if(dataSource.getDatabaseType().equals(DataSourceType.DM.getDataSourceName())){
+            genConfig.setDriverClassName("dm.jdbc.driver.DmDriver");
+            genConfig.setUrl("jdbc:dm://"+dataSource.getDatabaseIp()+":"+dataSource.getPortNumber()+"?schema="+dataSource.getDatabaseName());
+        }
+        return genConfig;
+    }
+}

+ 12 - 2
src/main/resources/application.yml

@@ -6,9 +6,19 @@ spring:
   datasource:
     dbtype: mysql
     driver-class-name: com.mysql.jdbc.Driver
-    url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true
+    url: jdbc:mysql://192.168.110.15:3306/zkqy-call?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai&allowMultiQueries=true
     username: root
-    password: root
+    password: zkqy8888
+    dynamic:
+      primary: master
+      strict: false
+      datasource:
+        master:
+          url: jdbc:mysql://192.168.110.15:3306/zkqy-call
+          username: root
+          password: zkqy8888
+          driver-class-name: com.mysql.cj.jdbc.Driver
+          type: com.alibaba.druid.pool.DruidDataSource
 logging:
   level:
     com.customer.mapper : debug

+ 11 - 0
src/main/resources/mapper/DataSourceMapper.xml

@@ -0,0 +1,11 @@
+<?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.DataSourceMapper">
+
+    <select id="selectDatabaseExist" resultType="int">
+        select count(1) from data_source where database_ip = #{databaseIp} and database_name = #{databaseName} and port_number = #{portNumber}
+    </select>
+
+</mapper>

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

@@ -87,4 +87,9 @@
     </update>
 
 
+    <select id="oracleTableExist" resultType="int">
+        select count(1) from user_tables where table_name=upper(#{tableName})
+    </select>
+
+
 </mapper>