1、引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
2、配置application.yml
server:
port: 5555
spring:
datasource:
dynamic:
primary: master
strict: false
datasource:
master:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:oracle:thin:@10.88.5.20:1521:sbgl
username: SBGK
password: SBGK
driver-class-name: oracle.jdbc.driver.OracleDriver
druid:
max-active: 20
initial-size: 10
max-wait: 60000
slave_1:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:oracle:thin:@10.88.5.16:1521:orcl
username: AQGK
password: aqgk
driver-class-name: oracle.jdbc.driver.OracleDriver
druid:
max-active: 20
initial-size: 10
max-wait: 60000
slave_2:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/travel
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
max-active: 20
initial-size: 10
max-wait: 60000
druid:
max-active: 20
initial-size: 10
max-wait: 60000
time-between-eviction-runs-millis: 60000
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml
type-aliases-package: com.cc.pojo
configuration:
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
logging:
level:
com.cc.mapper: debug
file:
path: D:\myLogs\logs1
3、启动类上添加注解
@SpringBootApplication
@MapperScan("com.cc.mapper")
public class MoreDatasourceApplication {
public static void main(String[] args) {
SpringApplication.run(MoreDatasourceApplication.class, args);
}
}
4、相关service上添加注解
@DS("master")
@Service
public class TbElecInfoServiceImpl extends ServiceImpl<TbElecInfoMapper, TbElecInfo>
implements TbElecInfoService{
}
@DS("slave_1")
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users>
implements UsersService{
@Autowired
private UsersMapper usersMapper;
@Override
public List<Users> getAllUser() {
return usersMapper.getAllUser();
}
}