springboot(四)整合分页插件PageHelper

引入依赖

<!--分页插件-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

配置数据库方言

传统的ssm框架整合PageHelper插件时一般是在SqlMapConfig.xml文件中配置

<plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>

springboot只需要配置一行

#pagehelper分页插件配置
pagehelper:
  helper-dialect:mysql

mapper接口

 @Select("select * from account")
    Page<Account> selectAccounts();

service实现

   public Page<Account> selectAccount(Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        Page<Account> accounts = accountMapper.selectAccounts();
        return accounts;
    }

controller

@RequestMapping("/selectAccount")
    public Page<Account> selectAccount(int pageNum,int pageSize){
        return accountService.selectAccount(pageNum,pageSize);
    }

结果

页面访问 http://localhost:8080/selectAccount?pageNum=1&pageSize=2
[{“id”:1,”name”:”aaa”,”money”:1000.0},{“id”:2,”name”:”bbb”,”money”:1000.0}]

猜你喜欢

转载自blog.csdn.net/jjkang_/article/details/80961463
今日推荐