springboot使用分页

**

java springboot分页使用

**
1、pom文件添加依赖

<dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>
<!--        分页-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.2</version>
        </dependency>

2、controller使用pageHelper

@GetMapping("attr/list")
    public JSONResult attrList(String attribute_code,
                               String name,
                               @RequestParam(required = false) Integer type,
                               @RequestParam(required = false) Integer category,
                               @RequestParam(required = false,defaultValue = "10") Integer page,
                               @RequestParam(required = false,defaultValue = "1") Integer pageNum )
    {
        try
        {
            AttributesName attributesName = new AttributesName();
            attributesName.setType(type);
            attributesName.setName(name);
            attributesName.setAttribute_code(attribute_code);
            attributesName.setCategory(category);

            PageHelper.startPage(pageNum,page);
            List<AttributesRet> list = classificationService.getAttrList(attributesName);
            return JSONResult.ok(list);
        }
        catch (Exception e)
        {
            return JSONResult.errorMsg(e.getMessage());
        }
    }

### Spring Boot 分页插件使用教程 #### PageHelper 插件简介 PageHelper 是一款非常流行的分页插件,支持多种数据库和持久层框架。对于基于 Spring Boot 的项目而言,可以通过引入 `pagehelper-spring-boot-starter` 组件来简化集成过程[^1]。 #### 安装与配置 为了在 Spring Boot 中启用 PageHelper 功能,在项目的 `pom.xml` 文件内添加如下依赖: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>${pagehelper.version}</version> </dependency> ``` 接着,在应用程序属性文件(application.properties 或 application.yml)里设置必要的参数以满足业务需求: ```yaml # YAML 配置方式 pagehelper: helperDialect: mysql # 数据库方言, 如mysql/sqlserver/oracle等 reasonable: true # 是否合理化分页 supportMethodsArguments: true # 支持通过Mapper接口参数控制分页 ``` #### 实现分页查询 当完成上述准备工作之后,便可以在具体的 DAO 层接口中利用该工具执行带有限制条件的数据检索操作。下面给出一段 Java 方法作为示范,假设有一个名为 User 的实体类以及对应的 Mapper 接口: ```java // 假设这是UserMapper.java中的部分定义 public interface UserMapper { List<User> selectAllUsers(); } @Service public class UserService { @Autowired private UserMapper userMapper; public PageInfo<User> getUsers(int pageNum, int pageSize){ // 设置分页参数 PageHelper.startPage(pageNum, pageSize); // 执行查询语句并自动处理分页逻辑 List<User> list = userMapper.selectAllUsers(); // 返回包含分页信息的结果集 return new PageInfo<>(list); } } ``` 此段代码片段展示了如何调用 `PageHelper.startPage()` 来启动一次新的分页会话,并随后立即发起数据读取请求;最终返回的对象不仅包含了当前页面内的记录集合,还携带有关总条目数、总页码等相关统计信息[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值