若依ry替换mybatis为mybatis-plus
若依ry替换mybatis为mybatis-plus
- 替换mb为mp
- 使用mp的多数据源
替换mb为mp
- 项目根下的
pom.xml
和common
模块的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>
framework
模块下的MybatisConfig
替换为下面的mp配置类:
@Configuration
public class MyBatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();//能够添加很多拦截器实现mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//乐观锁拦截器mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return mybatisPlusInterceptor;}
}
- 替换
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: simpleconfiguration:map-underscore-to-camel-case: truelog-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
-
可选操作
- 移除无用的
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的多数据源
- 项目根下的
pom.xml
和common
模块的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>
admin
模块下的application-druid.xml
中配置如下内容:
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverdynamic:druid:# 初始连接数initialSize: 5# 最小连接池数量minIdle: 10# 最大连接池数量maxActive: 20# 配置获取连接等待超时的时间maxWait: 60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒timeBetweenEvictionRunsMillis: 60000# 配置一个连接在池中最小生存的时间,单位是毫秒minEvictableIdleTimeMillis: 300000# 配置一个连接在池中最大生存的时间,单位是毫秒maxEvictableIdleTimeMillis: 900000# 配置检测连接是否有效validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falseprimary: wf-systemdatasource:wf-system:url: jdbc:mysql://xxx:3306/wf_system?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8username: rootpassword: xxxwf-manage:url: jdbc:mysql://xxx:3306/wf_manage?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8username: rootpassword: xxx
admin
模块的启动类上排除druid
的自动配置类:@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})
- 移除
framework
模块的datasource
目录和DruidConfig
、DruidProperties
、DataSourceType
文件,其中DruidConfig
替换掉相关内容以配置druid
的浏览器页面。 - 移除
common
模块下的DataSource
、DataSourceType
文件。
如果出现使用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>