1.配置多数据源的配置信息
spring:
datasource:
dynamic:
primary:master
lazy:true
strict:false
datasource:
url:jdbc:mysql://ip:port:dataname?.......
代码实现
1.引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
2.使用多个数据源
@Service
@DS("master")
public class Example extends ServiceImpl<ExampleMapper,ExampleEntity> implements
ExampleUserService{
@Resource
private ExampleProductMapper exampleProductMapper;
@DS("master")
@Override
public List<ExampleProductEntity> selectFromMaster(ExampleEntity entity){
return exampleProductMapper.queryGoodsinfoByGoodsId(entity);
}
@DS("slave")
@Override
public List<ExampleProductEntity> selectFromSlave(exampleProductEntity entity){
return exampleProductMapper.queryGoodInfoByGoodId(entity);
}
}
|