这里使用 mySQL/postgreSQL两种数据源。
1.pom
1.1 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
</dependencies>
1.2 build
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</build>
2. Application
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.modelone.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
3. yml
server:
port: 8088
spring:
datasource:
dynamic:
primary: postgresql
strict: false
datasource:
postgresql:
url: jdbc:postgresql://10.0.0.0:5432/tstmbgcpfr?currentSchema=public&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: a_appconnect
password: du%Rf0yp
type: com.zaxxer.hikari.HikariDataSource
hikari:
minimum-idle: 5
maximum-pool-size: 15
connection-test-query: SELECT 1
max-lifetime: 1800000
connection-timeout: 30000
pool-name: cprfPlanningHikariCP-pgsql
olap:
url: jdbc:mysql://10.0.0.0:9030/tstmbgcpfr?serverTimezone=Asia/Shanghai
username: cpfr
password: CPFR@022
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
hikari:
minimum-idle: 3
maximum-pool-size: 10
connection-test-query: SELECT 1
max-lifetime: 1800000
connection-timeout: 30000
pool-name: cprfPlanningHikariCP-OLAP
mybatis:
mapper-locations: classpath*:mapper
4. 其他java 代码
4.1 controller
@RestController
@RequestMapping("/dynamic/db/")
public class MoonTestController {
@Resource
private MoonTestService moonTestService;
@Resource
private OlapService olapService;
@Resource
private PostGreService postGreService;
@GetMapping("getPdpVersionList")
public List<String> getPdpVersionList() {
return olapService.getPdpVersionList();
}
@GetMapping("getBusinessCaseVersionList")
public List<String> getBusinessCaseVersionList() {
return postGreService.getBusinessCaseVersionList();
}
}
4.2 service
public interface MoonTestService {
List<String> getPdpVersionList();
List<String> getBusinessCaseVersionList();
}
public interface OlapService {
List<String> getPdpVersionList();
}
4.3 serviceImpl
两种实现方式
@Service
public class MoonTestServiceImpl implements MoonTestService {
@Resource
private MoonTestMapper moonTestMapper;
@Override
public List<String> getPdpVersionList() {
DynamicDataSourceContextHolder.push("olap");
List<String> list = null;
try {
list = moonTestMapper.getPdpVersionList();
} finally {
DynamicDataSourceContextHolder.clear();
}
return list;
}
@Override
public List<String> getBusinessCaseVersionList() {
List<String> list = moonTestMapper.getBusinessCaseVersionList();
return list;
}
}
@Service
@DS("olap")
public class OlapServiceImpl implements OlapService {
@Resource
private MoonTestMapper moonTestMapper;
@Override
public List<String> getPdpVersionList() {
List<String> list = moonTestMapper.getPdpVersionList();
return list;
}
}
4.4 mapper
@Repository
public interface MoonTestMapper {
List<String> getPdpVersionList();
List<String> getBusinessCaseVersionList();
}
<?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.example.demo.modelone.mapper.MoonTestMapper">
<select id="getPdpVersionList" resultType="java.lang.String">
select version from ludp_pdp group by version ORDER BY version desc
</select>
<select id="getBusinessCaseVersionList" resultType="java.lang.String">
select version from ludp_business_case_version group by version order by version desc
</select>
</mapper>