若依3.6.0使用Mybatis-plus分頁失效以及完美替換Pagehelper

一、前言

小編最近在經歷後端框架的遷移,雖然不是小編來做,但是有個分頁的情況讓小編和一個同事去搞。
說一下小編這邊的需求:
原來框架使用Mybatis-plus進行分頁,要更換的新框架若依是使用Pagehelper。所以現在需求讓我們把若依的幹掉,使用Mybatis-plusMybatis-plus的生態還是挺好的,方便,最重要的是和原來的框架一樣,不需要更改。
存在問題:需要把若依以前的分頁全部改成Mybatis-plus的分頁,那我們就按個換嘍,誰讓咱們喜歡搬磚!

先說一下問題出現的原因:
Mybatis和Mybatis-plus存在衝突,Pagehelper依賴於Mybatis,所以衝突了!!

解決方案:
PagehelperMybatis的依賴,然後一點點的改若依一些基本配置的分頁就好,最後在加上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分頁方法

/**
 * 設置請求分頁數據
 */
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<SysConfig> 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<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. 總結

這樣依次對ruoyi-modules-system項目進行修改,還有一些jobgen,不要和不用的就注釋掉,只要不報錯,原來的項目分頁就可以展示出來,原來不改造之前是total和pages都是0,改造後恢復正常。

總的來說就是刪依賴,加依賴,注釋一些不要的,添加一個新的分頁方法即可,都是搬磚的活,哈哈!!

如果解決了你的問題,還不趕緊一鍵三連來支持一波小編!!謝謝大家嘍~~

六、補充

這樣之後我們發現system項目中的分頁是有問題,是因為xml文件里沒有指定對象.屬性。於是把xml的一個例子修改了,現在分享給大家:

<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>
    <!-- 數據範圍過濾 -->
    ${dept.params.dataScope}
    order by d.parent_id, d.order_num
</select>

有緣人才可以看得到的哦!!!

點擊訪問!小編自己的網站,裏面也是有很多好的文章哦!