pro文件引入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<!--主从配置依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.5.6</version>
</dependency>
<!--lombok用来简化实体类:需要安装lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
application.yml配置
spring:
datasource:
dynamic:
primary: master #设置默认数据源或数据源组,master默认值(数据源名称可以随意起名,没有固定值,eg:db1,db2)
strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候回抛出异常,不启动会使用默认数据源.
datasource:
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.3.220:3306/mchouse_test1?useUnicode=true&characterEncoding=utf-8
username: *****
password: *****
slave_1:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://112.30.184.149:3306/net_trans_sup_hefei_edi?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: *****
password: *****
slave_2:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://120.55.168.100:33066/net_trans_sup_hefei_edi?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: *****
password: *****
mybatis-plus:
# configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #org.apache.ibatis.logging.slf4j.Slf4jImpl
mapper-locations: classpath:mapper/*.xml #配置mybatis.xml文件路劲 classpath根路径
global-config:
# 逻辑删除配置
db-config:
# 删除后
logic-delete-value: 1
# 删除前
logic-not-delete-value: 0
修改Application启动类
@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})
这里要排除DruidDataSourceAutoConfigure ,因为DruidDataSourceAutoConfigure会注入一个DataSourceWrapper,其会在原生的spring.datasource下找url,username,password等。而我们动态数据源的配置路径是变化的。
创建MybatisPlusConfig
@Configuration
@EnableTransactionManagement
@MapperScan("com.example.md5_demo.com.db.**.mapper")
public class MyBatisPlusConfig {
/**
* SQL 执行性能分析插件
* 开发环境使用,线上不推荐。 maxTime 指的是 sql 最大执行时长
*/
@Bean
@Profile({"dev","test"})// 设置 dev test 环境开启
public PerformanceInterceptor performanceInterceptor() {
PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
performanceInterceptor.setMaxTime(100000);//ms,超过此处设置的ms则sql不执行
performanceInterceptor.setFormat(true);
return performanceInterceptor;
}
/**
* 逻辑删除插件
*/
@Bean
public ISqlInjector sqlInjector() {
return new LogicSqlInjector();
}
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
创建mapper接口
@Mapper
public interface DemoMapper extends BaseMapper<Demo> {
List<Demo> getAllList();
@DS("slave_2")
List<Demo> getShopList();
}
测试类测试
@SpringBootTest
class Md5DemoApplicationTests {
@Autowired
private DemoMapper demoMapper;
@Test
void contextLoads() {
List<Demo> list=demoMapper.getAllList();
System.out.println(list);
System.out.println("***************");
List<Demo> shopList=demoMapper.getShopList();
System.out.println(shopList);
}
}
@DS优先级:方法 > 类
@DS 可以注解在方法上和类上,同时存在方法注解优先于类上注解,mapper或者service都可以添加,建议只在一个方法上添加即可。