若依ry替换mybatis为mybatis-plus

替换mb为mp

  1. 项目根下的pom.xmlcommon模块的pom.xml加上mp的依赖。
<!--<mp.version>3.4.2</mp.version>-->

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>mybatis-plus-boot-starter</artifactId>
  <version>${mp.version}</version>
</dependency>
  1. framework模块下的MybatisConfig替换为下面的mp配置类:
@Configuration
public class MyBatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        //能够添加很多拦截器实现
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        //乐观锁拦截器
        mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return mybatisPlusInterceptor;
    }
}
  1. 替换admin模块下application.yml里mybatis的配置为下面的mp配置:
mybatis-plus:
  # 搜索指定包别名
  type-aliases-package: com.manage.**.domain
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapper-locations: classpath*:/mapper/**/*Mapper.xml
  # 配置默认的执行器.SIMPLE就是普通执行器;REUSE执行器会重用预处理语句(prepared statements);BATCH执行器将重用语句并执行批量更新
  executor-type: simple
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
  1. 可选操作

    • 移除无用的mybatis-config.xml配置文件。
    • 移除pageHelper分页插件依赖(需要修改system等模块的相关方法,改用mp的分页插件)。
    • 不移除pageHelper,可以在BaseController中新增一个兼容mp分页返回值的方法:
protected TableDataInfo getDataTable(IPage<?> page) {
    TableDataInfo rspData = new TableDataInfo();
    rspData.setCode(HttpStatus.SUCCESS);
    rspData.setMsg("查询成功");
    rspData.setRows(page.getRecords());
    rspData.setTotal(page.getTotal());
    return rspData;
}

使用mp的多数据源

  1. 项目根下的pom.xmlcommon模块的pom.xml加上mp的依赖。
<!--<dynamic-datasource.version>3.3.1</dynamic-datasource.version>-->

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  <version>${dynamic-datasource.version}</version>
</dependency>
  1. admin模块下的application-druid.xml中配置如下内容:
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    dynamic:
      druid:
        # 初始连接数
        initialSize: 5
        # 最小连接池数量
        minIdle: 10
        # 最大连接池数量
        maxActive: 20
        # 配置获取连接等待超时的时间
        maxWait: 60000
        # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        timeBetweenEvictionRunsMillis: 60000
        # 配置一个连接在池中最小生存的时间,单位是毫秒
        minEvictableIdleTimeMillis: 300000
        # 配置一个连接在池中最大生存的时间,单位是毫秒
        maxEvictableIdleTimeMillis: 900000
        # 配置检测连接是否有效
        validationQuery: SELECT 1 FROM DUAL
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
      primary: wf-system
      datasource:
        wf-system:
          url: jdbc:mysql://xxx:3306/wf_system?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
          username: root
          password: xxx
        wf-manage:
          url: jdbc:mysql://xxx:3306/wf_manage?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
          username: root
          password: xxx
  1. admin模块的启动类上排除druid的自动配置类:@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})
  2. 移除framework模块的datasource目录和DruidConfigDruidPropertiesDataSourceType文件,其中DruidConfig替换掉相关内容以配置druid的浏览器页面。
  3. 移除common模块下的DataSourceDataSourceType文件。

如果出现使用mp分页插件查询报错,并且内容含有SELECT COUNT(),则是因为pagehelper依赖 或 generator模块 的jsqlparser库覆盖了mp的,可以修改mp的版本或排除掉其他依赖的jsqlparser库。例如:

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<exclusions>
 <!--pagehelper的4.5版本会和mp的4.0版本冲突,导致mp的分页插件count查询异常-->
 <exclusion>
   <groupId>com.github.jsqlparser</groupId>
   <artifactId>jsqlparser</artifactId>
 </exclusion>
</exclusions>
</dependency>
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐