温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot集成mybatis plus和dynamic-datasource的方法是什么

发布时间:2022-01-07 15:40:08 来源:亿速云 阅读:409 作者:iii 栏目:开发技术

这篇文章主要介绍“springboot集成mybatis plus和dynamic-datasource的方法是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“springboot集成mybatis plus和dynamic-datasource的方法是什么”文章能帮助大家解决问题。

springboot集成mybatis plus和dynamic-datasource注意事项

环境

  • spring-boot-starter-parent 1.5.2.RELEASE

  • mybatis-plus-boot-starter 2.x

  • dynamic-datasource-spring-boot-starter 2.5.0

  • druid-spring-boot-starter 1.1.10

注意事项

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)

dynamic所有版本默认启用stat和wall过滤器(默认不支持批量执行sql, 并且有些低版本无法自定义)

开启批量执行sql的方法

# 方法1:升级版本, 如2.5.0
spring:
  datasource:
    dynamic:
      druid: 
        wall:
          noneBaseStatementAllow: true
          multiStatementAllow: true
# 方法2:移除wall过滤器
spring:
  datasource:
    dynamic:
      druid: 
        filters: stat

现有项目集成mybatis plus时,应指定另外的枚举包,否则会出问题

mybatis-plus:
  type-enums-package: com.zxkj.demo.enums.mp

springboot mybatis plus多数据源配置整合dynamic-datasource

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都可以添加,建议只在一个方法上添加即可。

关于“springboot集成mybatis plus和dynamic-datasource的方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI