【若依框架】引入mybatis-plus并结合pagehelper完成分页实现

若依分离版采用的是mybatis的技术,需要自己去手动些一些sql语句,不如mybatis-plus方便,所以在基于若依开发的过程中,我引入了mybatis-plus,在尽量少改动代码的情况下,决定使用若依框架引入的pagehelper完成分页。

一、引入依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>${mybatis-plus-boot-starter.version}</version>
</dependency>

<!-- pagehelper 分页插件 (排除依赖,避免依赖冲突)-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>${pagehelper.boot.version}</version>
    <exclusions>
        <exclusion>
            <artifactId>mybatis-spring</artifactId>
            <groupId>org.mybatis</groupId>
        </exclusion>
        <exclusion>
            <artifactId>mybatis</artifactId>
            <groupId>org.mybatis</groupId>
        </exclusion>
    </exclusions>
</dependency>

二、配置yml文件

mybatis-plus:
  # 搜索指定包别名
  typeAliasesPackage: com.ruoyi.**.domain
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapperLocations: classpath*:mapper/**/*Mapper.xml
  global-config:
    db-config:
      id-type: auto
  configuration:
    map-underscore-to-camel-case: true

# PageHelper分页插件
pagehelper:
  helperDialect: mysql
  supportMethodsArguments: true
  params: count=countSql

三、分页代码

@GetMapping("/list")
public TableDataInfo list(int pageNum,int pageSize)
{
    PageHelper.startPage(pageNum,pageSize);
    List<Goods> list = goodsService.list();
    return getDataTable(list);

}

四、说明

@PreAuthorize("@ss.hasPermi('system:config:list')")
@GetMapping("/list")
public TableDataInfo list(SysConfig config)
{
    startPage();
    List<SysConfig> list = configService.selectConfigList(config);
    return getDataTable(list);
}

这个是官方原版的写法,这里的controller是继承了一个baseController,里面封装了一个getDataTable的方法,如下:

/**
 * 响应请求分页数据
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
protected TableDataInfo getDataTable(List<?> list)
{
    TableDataInfo rspData = new TableDataInfo();
    rspData.setCode(HttpStatus.SUCCESS);
    rspData.setMsg("查询成功");
    rspData.setRows(list);
    rspData.setTotal(new PageInfo(list).getTotal());
    return rspData;
}

这个方法主要是设置一些返回页面的一些数据,mybtis-plus整合pagehelper主要是接受来自页面的分页参数pageNum和pageSize,然后调用startPage做一个分页设置,最后service直接调用list查出所有的数据,再调用getDataTable这个方法,传入查询结果就可以


                
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus提供的PageHelper类是用于分页查询的工具类,它可以帮助我们简化分页查询的代码,提高开发效率。使用PageHelper类进行分页查询的步骤如下: 1. 引入MyBatis-Plus的依赖包和PageHelper的依赖包。 2. 在MyBatis的配置文件中配置PageHelper插件。 3. 在DAO层的Mapper接口中定义查询方法,并添加Page对象作为方法参数。 4. 在查询方法中使用PageHelper.startPage()方法设置分页参数,然后调用Mapper接口的查询方法查询数据。 5. 将查询结果封装到Page对象中,并返回Page对象。 下面是一个示例代码: ``` // 引入PageHelper依赖包 <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency> // 在MyBatis的配置文件中配置PageHelper插件 <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <property name="helperDialect" value="mysql"/> </plugin> </plugins> // 在DAO层的Mapper接口中定义查询方法,并添加Page对象作为方法参数 public interface UserMapper { List<User> selectUserList(Page<User> page); } // 在查询方法中使用PageHelper.startPage()方法设置分页参数,然后调用Mapper接口的查询方法查询数据 public List<User> getUserList(int pageNum, int pageSize) { Page<User> page = new Page<>(pageNum, pageSize); userMapper.selectUserList(page); return page.getRecords(); } ``` 在上面的示例代码中,我们使用PageHelper.startPage()方法设置分页参数,然后调用Mapper接口的selectUserList()方法查询数据。查询结果封装在Page对象中,最后返回Page对象中的记录。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值