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

一、前言
小編最近在經(jīng)歷后端框架的遷移,雖然不是小編來做,但是有個分頁的情況讓小編和一個同事去搞。說一下小編這邊的需求:原來框架使用?Mybatis-plus?進(jìn)行分頁,要更換的新框架若依是使用Pagehelper?。所以現(xiàn)在需求讓我們把若依的干掉,使用Mybatis-plus,Mybatis-plus?的生態(tài)還是挺好的,方便,最重要的是和原來的框架一樣,不需要更改。存在問題:需要把若依以前的分頁全部改成?Mybatis-plus的分頁,那我們就按個換嘍,誰讓咱們喜歡搬磚!
先說一下問題出現(xiàn)的原因: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
*/
public class PageUtils extends PageHelper{}
2、注釋BaseController分頁方法
/**
* 設(shè)置請求分頁數(shù)據(jù)
*/
protected void startPage(){
PageUtils.startPage();
}
/**
* 清理分頁的線程變量
*/
protected void clearPage(){
PageUtils.clearPage();
}
/**
* 響應(yīng)請求分頁數(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分頁
1、在ruoyi-common-core中新建配置類
@Configuration
public class MybatisPlusConfig {
/**
* 新的分頁插件,一緩和二緩遵循mybatis的規(guī)則,需要設(shè)置 MybatisConfiguration#useDeprecatedExecutor = false 避免緩存出現(xiàn)問題(該屬性會在舊插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}

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

五、修改ruoyi-modules-system
我們的宗旨是不影響之前的使用,需要我們新寫一個分頁,因為他們的export接口都使用了原來的分頁,雖然分頁沒了,但是只要不調(diào)用還是不會報錯的!
我們以一個controller的改造為例:
1、SysConfigController改造
原來的方法:
/**
* 獲取參數(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即可,原來的方法我們不動,重新寫一個兩個參數(shù)的方法。
/**
* 獲取參數(shù)配置列表
*/
@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新增分頁實現(xiàn)方法
@Override
public Page<SysConfig> 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、總結(jié)
這樣依次對ruoyi-modules-system項目進(jìn)行修改,還有一些job和gen,不要和不用的就注釋掉,只要不報錯,原來的項目分頁就可以展示出來,原來不改造之前是total和pages都是0,改造后恢復(fù)正常。
總的來說就是刪依賴,加依賴,注釋一些不要的,添加一個新的分頁方法即可,都是搬磚的活,哈哈?。?/p>
六、補充
這樣之后我們發(fā)現(xiàn)system項目中的分頁是有問題,是因為xml文件里沒有指定對象.屬性。于是把xml的一個例子修改了,現(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ù)范圍過濾 -->
${dept.params.dataScope}
order by d.parent_id, d.order_num
</select>