<dependencies>
<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.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>8.0.18</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<optional>true</optional>
</dependency>
<!--阿里数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.14</version>
</dependency>
</dependencies>
configuration file
spring:
autoconfigure:
# 排除 Druid 自动配置
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
datasource:
dynamic:
# 设置默认的数据源或者数据源组,默认值即为master
primary: mysql
# 严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
strict: false
datasource:
mysql:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhsot:3306/acquisition?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: program
password: z#Mshkk5
greenplum:
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://localhsot:5432/scada
username: gpadmin
password: 123456
mybatis-plus:
mapper-locations: classpath:/mapper/**/*Mapper.xml
typeAliasesPackage: com.yang.**.entity
configuration:
mapUnderscoreToCamelCase: true
global-config:
db-config:
db-column-underline: true
application code
Use @DS to switch data sources.
@DS can be annotated on methods or classes, and there is a principle of proximity. Method annotations take precedence over class annotations QL
@Service
@DS("greenplum")
public class ScadaDataServiceImpl extends ServiceImpl<ScadaDataMapper, ScadaData> implements ScadaDataService {
@Override
public List<ScadaData> pageQuery() {
List<String> code = baseMapper.getDeviceCode();
code.forEach(System.out::println);
String deviceCode = "D202210180064";
return list(Wrappers.<ScadaData>lambdaQuery()
.eq(ScadaData::getDeviceCode, deviceCode));
}
}
@Mapper
public interface ScadaDataMapper extends BaseMapper<ScadaData> {
List<String> getDeviceCode();
}
<?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.yang.mapper.ScadaDataMapper">
<select id="getDeviceCode" resultType="java.lang.String">
SELECT
DISTINCT
device_code
FROM t_scada_data
</select>
</mapper>
@Service
public class AlarmActionServiceImpl extends ServiceImpl<AlarmActionMapper, AlarmAction> implements AlarmActionService {
private final ScadaDataService scadaDataService;
@Autowired
public AlarmActionServiceImpl(ScadaDataService scadaDataService) {
this.scadaDataService = scadaDataService;
}
@Override
public void getData() {
List<ScadaData> scadaData = scadaDataService.pageQuery();
List<AlarmAction> list = list();
System.out.println(list.size());
System.out.println(scadaData.size());
}
}
Frequently asked questions about @DS annotation invalidation
About transactions: Why does the multi-data source @DS switch of Mybatis plus not work, whose pot, @Transactional_zmyHow's blog-CSDN blog_mybatisplus@ds causes transaction failure
Mybatis-plus multi-data source invalid problem (springboot + shiro)
Official reference documents: