本来自己写的动态数据源,后来看到苞米豆开源框架中有动态数据源,于是就直接改用苞米豆多数据源
依赖
<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;
}