7

若依3.6.0使用Mybatis-plus分页失效以及完美替换Pagehelper - 小王写博客

 2 years ago
source link: https://www.cnblogs.com/wang1221/p/16618594.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

若依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);

这样依次对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>

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

点击访问!小编自己的网站,里面也是有很多好的文章哦!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK