刪Pagehelper和Mybatis的依賴,然后一點(diǎn)點(diǎn)的改若依一些基本配置的分頁(yè)就好,最后在加上Mybatis-plus的分頁(yè)插件配置!最最重要的是要掃描到寫(xiě)的分頁(yè)插件,不然不生效!

一、前言
小編最近在經(jīng)歷后端框架的遷移,雖然不是小編來(lái)做,但是有個(gè)分頁(yè)的情況讓小編和一個(gè)同事去搞。說(shuō)一下小編這邊的需求:原來(lái)框架使用?Mybatis-plus?進(jìn)行分頁(yè),要更換的新框架若依是使用Pagehelper?。所以現(xiàn)在需求讓我們把若依的干掉,使用Mybatis-plus,Mybatis-plus?的生態(tài)還是挺好的,方便,最重要的是和原來(lái)的框架一樣,不需要更改。存在問(wèn)題:需要把若依以前的分頁(yè)全部改成?Mybatis-plus的分頁(yè),那我們就按個(gè)換嘍,誰(shuí)讓咱們喜歡搬磚!
先說(shuō)一下問(wèn)題出現(xiàn)的原因:Mybatis和Mybatis-plus存在沖突,?Pagehelper依賴于Mybatis,所以沖突了!!
解決方案:刪?Pagehelper和Mybatis?的依賴,然后一點(diǎn)點(diǎn)的改若依一些基本配置的分頁(yè)就好,最后在加上Mybatis-plus的分頁(yè)插件配置!最最重要的是要掃描到寫(xiě)的分頁(yè)插件,不然不生效!?
二、刪依賴
1、刪除根目錄的依賴
<!-- Mybatis 依賴配置 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${spring-boot.mybatis}</version>
</dependency>
<!-- pagehelper 分頁(yè)插件 -->
<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
整個(gè)類(lèi)全部注釋?zhuān)?/p>
/**
* 分頁(yè)工具類(lèi)
*
* @author ruoyi
*/
public class PageUtils extends PageHelper{}
2、注釋BaseController分頁(yè)方法
/**
* 設(shè)置請(qǐng)求分頁(yè)數(shù)據(jù)
*/
protected void startPage(){
PageUtils.startPage();
}
/**
* 清理分頁(yè)的線程變量
*/
protected void clearPage(){
PageUtils.clearPage();
}
/**
* 響應(yīng)請(qǐng)求分頁(yè)數(shù)據(jù)
*/
@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分頁(yè)
1、在ruoyi-common-core中新建配置類(lèi)
@Configuration
public class MybatisPlusConfig {
/**
* 新的分頁(yè)插件,一緩和二緩遵循mybatis的規(guī)則,需要設(shè)置 MybatisConfiguration#useDeprecatedExecutor = false 避免緩存出現(xiàn)問(wèn)題(該屬性會(huì)在舊插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}

2、配置上可以掃描的
我們發(fā)現(xiàn)在core中已經(jīng)給了我們提示,他配置了一個(gè),我們只需要把我們剛剛寫(xiě)的配置類(lèi)加上去,就可以掃描到這配置,然后生效了??!
不配置不生效(切記切記)!
我們找到所在位置,添加上全路徑即可,這里我們對(duì)若依的架構(gòu)修改了名稱(chēng),也就是若依的core包下的!

五、修改ruoyi-modules-system
我們的宗旨是不影響之前的使用,需要我們新寫(xiě)一個(gè)分頁(yè),因?yàn)樗麄兊膃xport接口都使用了原來(lái)的分頁(yè),雖然分頁(yè)沒(méi)了,但是只要不調(diào)用還是不會(huì)報(bào)錯(cuò)的!
我們以一個(gè)controller的改造為例:
1、SysConfigController改造
原來(lái)的方法:
/**
* 獲取參數(shù)配置列表
*/
@RequiresPermissions("system:config:list")
@GetMapping("/list")
public TableDataInfo list(SysConfig config){
startPage();
List<SysConfig> list = configService.selectConfigList(config);
return getDataTable(list);
}
修改后的方法:
這里統(tǒng)一返回值我是使用我們以前架構(gòu)的,大家也可以使用若依自帶的AjaxResult,只需要添加上Page即可,原來(lái)的方法我們不動(dòng),重新寫(xiě)一個(gè)兩個(gè)參數(shù)的方法。
/**
* 獲取參數(shù)配置列表
*/
@RequiresPermissions("system:config:list")
@GetMapping("/list")
public R list(Page page, SysConfig config) {
return R.ok(configService.selectConfigList(page, config));
}
2、ISysConfigService新增分頁(yè)方法
/**
* 新分頁(yè)
* @param page
* @param config
* @return
*/
Page<SysConfig> selectConfigList(Page page,SysConfig config);
3、SysConfigServiceImpl新增分頁(yè)實(shí)現(xiàn)方法
@Override
public Page<SysConfig> selectConfigList(Page page, SysConfig config) {
return configMapper.selectConfigList(page,config);
}
4、SysConfigMapper新增分頁(yè)接口
/**
* 新分頁(yè)
* @param page
* @param config
* @return
*/
Page<SysConfig> selectConfigList(Page page,@Param("config") SysConfig config);
5、總結(jié)
這樣依次對(duì)ruoyi-modules-system項(xiàng)目進(jìn)行修改,還有一些job和gen,不要和不用的就注釋掉,只要不報(bào)錯(cuò),原來(lái)的項(xiàng)目分頁(yè)就可以展示出來(lái),原來(lái)不改造之前是total和pages都是0,改造后恢復(fù)正常。
總的來(lái)說(shuō)就是刪依賴,加依賴,注釋一些不要的,添加一個(gè)新的分頁(yè)方法即可,都是搬磚的活,哈哈??!
六、補(bǔ)充
這樣之后我們發(fā)現(xiàn)system項(xiàng)目中的分頁(yè)是有問(wèn)題,是因?yàn)閤ml文件里沒(méi)有指定對(duì)象.屬性。于是把xml的一個(gè)例子修改了,現(xiàn)在分享給大家:
<select id="selectDeptList" resultMap="SysDeptResult">
<include refid="selectDeptVo"/>
where d.del_flag = '0'
<if test="dept.deptId != null and dept.deptId != 0">
AND dept_id = #{dept.deptId}
</if>
<if test="dept.parentId != null and dept.parentId != 0">
AND parent_id = #{dept.parentId}
</if>
<if test="dept.deptName != null and dept.deptName != ''">
AND dept_name like concat('%', #{dept.deptName}, '%')
</if>
<if test="dept.status != null and dept.status != ''">
AND status = #{dept.status}
</if>
<!-- 數(shù)據(jù)范圍過(guò)濾 -->
${dept.params.dataScope}
order by d.parent_id, d.order_num
</select>