一、前言
小编最近在经历后端框架的迁移,虽然不是小编来做,但是有个分页的情况让小编和一个同事去搞。说一下小编这边的需求:原来框架使用Mybatis-plus进行分页,要更换的新框架若依是使用Pagehelper。所以现在需求让我们把若依的干掉,使用Mybatis-plus,Mybatis-plus的生态还是挺好的,方便,最重要的是和原来的框架一样,不需要更改。存在问题:需要把若依以前的分页全部改成Mybatis-plus的分页,那我们就按个换喽,谁让咱们喜欢搬砖!
先说一下问题出现的原因:Mybatis和Mybatis-plus存在冲突,Pagehelper依赖于Mybatis,所以冲突了!!
解决方案:删Pagehelper和Mybatis的依赖,然后一点点的改若依一些基本配置的分页就好,最后在加上Mybatis-plus的分页插件配置!最最重要的是要扫描到写的分页插件,不然不生效!
二、删依赖
1、删除根目录的依赖
<!--Mybatis依赖配置-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${spring-boot.mybatis}</version>
</dependency>
<!--pagehelper分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.boot.version}</version>
</dependency>
<spring-boot.mybatis>2.2.2</spring-boot.mybatis>
2、根目录添加依赖
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${spring-boot.mybatis-plus}</version>
</dependency>
<spring-boot.mybatis-plus>3.5.1</spring-boot.mybatis-plus>
3、ruoyi-common-core模块删除依赖
<!--Pagehelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
三、修改文件
1、注释PageUtils
整个类全部注释!
/**
* 分页工具类
*
* @author ruoyi
*/
publicclassPageUtilsextendsPageHelper{}
2、注释BaseController分页方法
/**
* 设置请求分页数据
*/
protectedvoidstartPage(){
PageUtils.startPage();
}
/**
* 清理分页的线程变量
*/
protectedvoidclearPage(){
PageUtils.clearPage();
}
/**
* 响应请求分页数据
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protectedTableDataInfogetDataTable(List<?>list){
TableDataInforspData=newTableDataInfo();
rspData.setCode(HttpStatus.SUCCESS);
rspData.setRows(list);
rspData.setMsg("查询成功");
rspData.setTotal(newPageInfo(list).getTotal());
returnrspData;
}
四、配置Mybatis-plus分页
1、在ruoyi-common-core中新建配置类
@Configuration
publicclassMybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
publicMybatisPlusInterceptormybatisPlusInterceptor() {
MybatisPlusInterceptorinterceptor=newMybatisPlusInterceptor();
interceptor.addInnerInterceptor(newPaginationInnerInterceptor(DbType.MYSQL));
returninterceptor;
}
}
2、配置上可以扫描的
我们发现在core中已经给了我们提示,他配置了一个,我们只需要把我们刚刚写的配置类加上去,就可以扫描到这配置,然后生效了!!
不配置不生效(切记切记)!
我们找到所在位置,添加上全路径即可,这里我们对若依的架构修改了名称,也就是若依的core包下的!
五、修改ruoyi-modules-system
我们的宗旨是不影响之前的使用,需要我们新写一个分页,因为他们的export接口都使用了原来的分页,虽然分页没了,但是只要不调用还是不会报错的!
我们以一个controller的改造为例:
1、SysConfigController改造
原来的方法:
/**
* 获取参数配置列表
*/
@RequiresPermissions("system:config:list")
@GetMapping("/list")
publicTableDataInfolist(SysConfigconfig){
startPage();
List<SysConfig>list=configService.selectConfigList(config);
returngetDataTable(list);
}
修改后的方法:
这里统一返回值我是使用我们以前架构的,大家也可以使用若依自带的AjaxResult,只需要添加上Page即可,原来的方法我们不动,重新写一个两个参数的方法。
/**
* 获取参数配置列表
*/
@RequiresPermissions("system:config:list")
@GetMapping("/list")
publicRlist(Pagepage, SysConfigconfig) {
returnR.ok(configService.selectConfigList(page, config));
}
2、ISysConfigService新增分页方法
/**
* 新分页
* @param page
* @param config
* @return
*/
Page<SysConfig>selectConfigList(Pagepage,SysConfigconfig);
3、SysConfigServiceImpl新增分页实现方法
@Override
publicPage<SysConfig>selectConfigList(Pagepage, SysConfigconfig) {
returnconfigMapper.selectConfigList(page,config);
}
4、SysConfigMapper新增分页接口
/**
* 新分页
* @param page
* @param config
* @return
*/
Page<SysConfig>selectConfigList(Pagepage,@Param("config") SysConfigconfig);
5、总结
这样依次对ruoyi-modules-system项目进行修改,还有一些job和gen,不要和不用的就注释掉,只要不报错,原来的项目分页就可以展示出来,原来不改造之前是total和pages都是0,改造后恢复正常。
总的来说就是删依赖,加依赖,注释一些不要的,添加一个新的分页方法即可,都是搬砖的活,哈哈!!
六、补充
这样之后我们发现system项目中的分页是有问题,是因为xml文件里没有指定对象.属性。于是把xml的一个例子修改了,现在分享给大家:
<selectid="selectDeptList"resultMap="SysDeptResult">
<includerefid="selectDeptVo"/>
whered.del_flag='0'
<iftest="dept.deptId != null and dept.deptId != 0">
ANDdept_id=#{dept.deptId}
</if>
<iftest="dept.parentId != null and dept.parentId != 0">
ANDparent_id=#{dept.parentId}
</if>
<iftest="dept.deptName != null and dept.deptName != ''">
ANDdept_namelikeconcat('%', #{dept.deptName}, '%')
</if>
<iftest="dept.status != null and dept.status != ''">
ANDstatus=#{dept.status}
</if>
<!--数据范围过滤-->
${dept.params.dataScope}
orderbyd.parent_id, d.order_num
</select>
文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/229734.html<

