若依3.6.0使用Mybatis-plus分页失效以及完美替换Pagehelpe···
一、前言
小编最近在经历后端框架的迁移,虽然不是小编来做,但是有个分页的情况让小编和一个同事去搞。说一下小编这边的需求:原来框架使用Mybatis-plus进行分页,要更换的新框架若依是使用Pagehelper。所以现在需求让我们把若依的干掉,使用Mybatis-plus,Mybatis-plus的生态还是挺好的,方便,最重要的是和原来的框架一样,不需要更改。存在问题:需要把若依以前的分页全部改成Mybatis-plus的分页,那我们就按个换喽,谁让咱们喜欢搬砖!
先说一下问题出现的原因:Mybatis和Mybatis-plus存在冲突,Pagehelper依赖于Mybatis,所以冲突了!!
解决方案:删Pagehelper和Mybatis的依赖,然后一点点的改若依一些基本配置的分页就好,最后在加上Mybatis-plus的分页插件配置!最最重要的是要扫描到写的分页插件,不然不生效!
二、删依赖
1、删除根目录的依赖
org.mybatis.spring.boot
mybatis-spring-boot-starter
${spring-boot.mybatis}
com.github.pagehelper
pagehelper-spring-boot-starter
${pagehelper.boot.version}
</dependency><spring-boot.mybatis>2.2.2</spring-boot.mybatis>2、根目录添加依赖
com.baomidou
mybatis-plus-boot-starter
${spring-boot.mybatis-plus}
</dependency><spring-boot.mybatis-plus>3.5.1</spring-boot.mybatis-plus>3、ruoyi-common-core模块删除依赖
com.github.pagehelper
pagehelper-spring-boot-starter
</dependency>三、修改文件
1、注释PageUtils
整个类全部注释!
/**
* 分页工具类
*
* @author ruoyi
*/
public class PageUtils extends PageHelper{}2、注释BaseController分页方法
/**
* 设置请求分页数据
*/
protected void startPage(){
PageUtils.startPage();
}
/**
* 清理分页的线程变量
*/
protected void clearPage(){
PageUtils.clearPage();
}
/**
* 响应请求分页数据
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected TableDataInfo getDataTable(List list){
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(HttpStatus.SUCCESS);
rspData.setRows(list);
rspData.setMsg("查询成功");
rspData.setTotal(new PageInfo(list).getTotal());
return rspData;
}四、配置Mybatis-plus分页
1、在ruoyi-common-core中新建配置类
@Configuration
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}2、配置上可以扫描的
我们发现在core中已经给了我们提示,他配置了一个,我们只需要把我们刚刚写的配置类加上去,就可以扫描到这配置,然后生效了!!
不配置不生效(切记切记)!
我们找到所在位置,添加上全路径即可,这里我们对若依的架构修改了名称,也就是若依的core包下的!
五、修改ruoyi-modules-system
我们的宗旨是不影响之前的使用,需要我们新写一个分页,因为他们的export接口都使用了原来的分页,虽然分页没了,但是只要不调用还是不会报错的!
我们以一个controller的改造为例:1、SysConfigController改造
原来的方法:
/**
* 获取参数配置列表
*/
@RequiresPermissions("system:config:list")
@GetMapping("/list")
public TableDataInfo list(SysConfig config){
startPage();
List list = configService.selectConfigList(config);
return getDataTable(list);
}修改后的方法:
这里统一返回值我是使用我们以前架构的,大家也可以使用若依自带的AjaxResult,只需要添加上Page即可,原来的方法我们不动,重新写一个两个参数的方法。
/**
* 获取参数配置列表
*/
@RequiresPermissions("system:config:list")
@GetMapping("/list")
public R list(Page page, SysConfig config) {
return R.ok(configService.selectConfigList(page, config));
}2、ISysConfigService新增分页方法
/**
* 新分页
* @param page
* @param config
* @return
*/
Page<SysConfig> selectConfigList(Page page,SysConfig config);3、SysConfigServiceImpl新增分页实现方法
@Override
public Page selectConfigList(Page page, SysConfig config) {
return configMapper.selectConfigList(page,config);
}4、SysConfigMapper新增分页接口
/**
* 新分页
* @param page
* @param config
* @return
*/
Page<SysConfig> selectConfigList(Page page,@Param("config") SysConfig config);5、总结
这样依次对ruoyi-modules-system项目进行修改,还有一些job和gen,不要和不用的就注释掉,只要不报错,原来的项目分页就可以展示出来,原来不改造之前是total和pages都是0,改造后恢复正常。
总的来说就是删依赖,加依赖,注释一些不要的,添加一个新的分页方法即可,都是搬砖的活,哈哈!!
六、补充
这样之后我们发现system项目中的分页是有问题,是因为xml文件里没有指定对象.属性。于是把xml的一个例子修改了,现在分享给大家:
where d.del_flag = 0
AND dept_id = #{dept.deptId}
AND parent_id = #{dept.parentId}
AND dept_name like concat(%, #{dept.deptName}, %)
AND status = #{dept.status}
${dept.params.dataScope}
order by d.parent_id, d.order_num
</select>扫一扫,关注我们