1、需要的依赖,版本自行控制
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
</dependency>
2、配置文件
spring:
datasource:
dynamic:
primary: master
strict: false
datasource:
master:
url: jdbc:mysql://xxx?useSSL=false&useUnicode=true&characterEncoding=utf8
username: root
password: pas
driver-class-name: com.mysql.cj.jdbc.Driver
two:
url: jdbc:mysql://xxx?useSSL=false&useUnicode=true&characterEncoding=utf8
username: root
password: pas
driver-class-name: com.mysql.cj.jdbc.Driver
3、使用方法,添加DS注解,值为配置的数据源名称
4、直接使用