SpringBoot+Mybatis-Plus多数据源配置整合dynamic-datasource...

SpringBoot+Mybatis-Plus多数据源配置整合dynamic-datasource...

2023年7月9日发(作者:)

SpringBoot+Mybatis-Plus多数据源配置整合dynamic-datasourcepro⽂件引⼊依赖 mysql mysql-connector-java runtime a druid-spring-boot-starter 1.2.6 ou mybatis-plus-boot-starter 3.1.2 ou dynamic-datasource-spring-boot-starter 2.5.6 tlombok lombok 1.18.20 配置spring: datasource: dynamic: primary: master #设置默认数据源或数据源组,master默认值(数据源名称可以随意起名,没有固定值,eg:db1,db2) strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候回抛出异常,不启动会使⽤默认数据源. datasource: master: driver-class-name: url: jdbc:mysql://192.168.3.220:3306/mchouse_test1?useUnicode=true&characterEncoding=utf-8 username: ***** password: ***** slave_1: driver-class-name: 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: 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: Impl #4jImpl mapper-locations: classpath:mapper/*.xml #配置⽂件路劲 classpath根路径 global-config: # 逻辑删除配置 db-config: # 删除后 logic-delete-value: 1 # 删除前 logic-not-delete-value: 0修改Application启动类@SpringBootApplication(exclude = {})这⾥要排除DruidDataSourceAutoConfigure ,因为DruidDataSourceAutoConfigure会注⼊⼀个DataSourceWrapper,其会在原⽣的urce下找url,username,password等。⽽我们动态数据源的配置路径是变化的。创建MybatisPlusConfig@Configuration@EnableTransactionManagement@MapperScan("5_.**.mapper")public class MyBatisPlusConfig { /** * SQL 执⾏性能分析插件 * 开发环境使⽤,线上不推荐。 maxTime 指的是 sql 最⼤执⾏时长 */ @Bean @Profile({"dev","test"})// 设置 dev test 环境开启 public PerformanceInterceptor performanceInterceptor() { PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor(); Time(100000);//ms,超过此处设置的ms则sql不执⾏ mat(true); return performanceInterceptor; } /** * 逻辑删除插件 */ @Bean public ISqlInjector sqlInjector() { return new LogicSqlInjector(); } /** * 分页插件 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); }}创建mapper接⼝@Mapperpublic interface DemoMapper extends BaseMapper { List getAllList(); @DS("slave_2") List getShopList();}测试类测试@SpringBootTestclass Md5DemoApplicationTests { @Autowired private DemoMapper demoMapper; @Test void contextLoads() { List list=List(); n(list); n("***************"); List shopList=pList(); n(shopList); }}@DS优先级:⽅法 > 类@DS 可以注解在⽅法上和类上,同时存在⽅法注解优先于类上注解,mapper或者service都可以添加,建议只在⼀个⽅法上添加即可。

发布者:admin,转转请注明出处:http://www.yc00.com/web/1688907777a182293.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信