pom引入依赖

<!-- 动态数据源 -->
<dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
        <version>2.5.4</version>
</dependency>

yml配置

spring:
  datasource:
    dynamic: 
      #默认是master
      primary: test
      datasource:
          #sqlserver
          master:
            url: jdbc:sqlserver://ip:port;database=database
            username: root
            password: 123456
            driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
          #mysql
          test1:
            url: jdbc:mysql://ip:port/database?characterEncoding=UTF-8&useUnicode=true
            username: root
            password: 123456
            driver-class-name: com.mysql.jdbc.Driver
          #oracle 如果是用服务名登录,url的最后的:(ip:port:sid)需要改为/(ip:port/服务名) 
          test2:
            url: jdbc:oracle:thin:@ip:port:sid
            driver-class-name: oracle.jdbc.OracleDriver
            username: root
            password: 123456

通过注解方式注解到类或者方法上

@DS(注解不能直接用于mapper类上只能用在service层的类或方法上

@DS("test1") //不主动声明就是使用primary指定的数据库
@Service
public class StudentImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
     ...........
}
posted on 2021-05-06 09:57  飞飞乐园  阅读(117)  评论(0编辑  收藏  举报