动态数据源查询-引用苞米豆多数据源

该博客介绍了如何在Spring Boot应用中集成苞米豆动态数据源框架,通过XML配置实现数据源的添加、删除和测试。博主展示了如何从数据库读取动态数据源信息,并提供了添加、删除数据源的API接口示例代码。
摘要由CSDN通过智能技术生成

本来自己写的动态数据源,后来看到苞米豆开源框架中有动态数据源,于是就直接改用苞米豆多数据源

依赖

  <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

把动态数据源信息放到数据库中所以要个默认数据源配置

server:
  port: 8080
spring:
  datasource:
    dynamic:
      datasource:
        master:
          username: test
          password: test
          url: jdbc:oracle:thin:@192.168.2.110:1521/orcl
          driver-class-name: oracle.jdbc.OracleDriver
#          username: sa
#          password: ""
#          url: jdbc:h2:mem:test
#          driver-class-name: org.h2.Driver
logging:
  path: C:/logs
  file: C:/logs/interface.log

pagehelper:
  helperDialect: Oracle
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

#打印日志
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mybatis/*.xml
#mybatis-plus:
#  mapper-locations: classpath:mybatis/*.xml
#  configuration:
#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#  mapper-locations: classpath:mybatis/*.xml

添加,删除以及测试数据源


import com.baomidou.dynamic.datasource.creator.BasicDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DruidDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.HikariDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.mzy.model.DataSourceDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 动态数据源添加删除
 */

@RestController
@RequestMapping("/datasources")
@Api(tags = "添加删除数据源")
public class LoadController {

  @Autowired
  private DataSource dataSource;
  @Autowired
  private DataSourceCreator dataSourceCreator;
  @Autowired
  private  BasicDataSourceCreator basicDataSourceCreator;


  /**
   * 获取当前数据源
   * @return
   */
  @GetMapping
  @ApiOperation("获取当前所有数据源")
  public Set<String> now() {
    DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
    return ds.getCurrentDataSources().keySet();
  }

  /**
   * 添加数据源
   * dataSourceProperty数据类型:{"pollName": "dataSource_1","username": "ssodata","password": "ssodata","driverClassName":"oracle.jdbc.OracleDriver","url": "jdbc:oracle:thin:@192.168.2.166:1521/orcl"}
   * @return
   */
  @PostMapping("/add")
  @ApiOperation("通用添加数据源(推荐)")
  public Set<String> add(@Validated @RequestBody DataSourceDTO dto) {
    DataSourceProperty dataSourceProperty = new DataSourceProperty();
    BeanUtils.copyProperties(dto, dataSourceProperty);
    DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
    DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
    ds.addDataSource(dto.getPollName(), dataSource);
    return ds.getCurrentDataSources().keySet();
  }

  @PostMapping("/addBasic")
  @ApiOperation(value = "添加基础数据源", notes = "调用Springboot内置方法创建数据源,兼容1,2")
  public Set<String> addBasic(@Validated @RequestBody DataSourceDTO dto) {
    DataSourceProperty dataSourceProperty = new DataSourceProperty();
    BeanUtils.copyProperties(dto, dataSourceProperty);
    DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
    DataSource dataSource = basicDataSourceCreator.createDataSource(dataSourceProperty);
    ds.addDataSource(dto.getPollName(), dataSource);
    return ds.getCurrentDataSources().keySet();
  }

  @DeleteMapping
  @ApiOperation("删除数据源")
  public String remove(String name) {
    DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
    ds.removeDataSource(name);
    return "删除成功";
  }

  @GetMapping("/testNowDataSource")
  public  List<Map<String, Object>> testNowDataSource(String dataSourcesName){
    DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
    Map<String, DataSource> currentDataSources = ds.getCurrentDataSources();
    JdbcTemplate jdbcTemplate = new JdbcTemplate(currentDataSources.get(dataSourcesName));
    List<Map<String, Object>> mapList = jdbcTemplate.queryForList("select * from dba_tablespaces ");
    return  mapList;
  }

}

实体类:


import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotBlank;
import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class DataSourceDTO {
    @NotBlank
    @ApiModelProperty(value = "连接池名称", example = "test")
    private String pollName;

    @NotBlank
    @ApiModelProperty(value = "JDBC driver", example = "org.h2.Driver")
    private String driverClassName;

    @NotBlank
    @ApiModelProperty(value = "JDBC url 地址", example = "jdbc:h2:mem:test10")
    private String url;

    @NotBlank
    @ApiModelProperty(value = "JDBC 用户名", example = "sa")
    private String username;

    @ApiModelProperty(value = "JDBC 密码")
    private String password;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值