pagehelper用法

spring boot 和 mybatis 中使用 pagehelper:

1、 在pom.xml 中加入pagehelper的引用:

    <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.4</version>
    </dependency>

  <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>1.2.11</version>
  </dependency>

2. 在 application.properties 文件中增加:

pagehelper.offsetAsPageNum=true
pagehelper.rowBoundsWithCount=true
pagehelper.pageSizeZero=true
pagehelper.reasonable=false
pagehelper.params=pageNum=pageHelperStart;pageSize=pageHelperRows;
pagehelper.supportMethodsArguments=false

在 application.properties 中增加以下的语句,打开 com.example.boot2.mapper 包下的日志, 该包下的 sql 语句都可以在控制台输出。

logging.level.com.example.boot2.mapper=debug

3. Controller 文件:

  @Autowired
  private UserService userService;

 
@RequestMapping(method = RequestMethod.GET, value = "/selectPage/{pageNum}/{pageSize}") public PageInfo<User> selectPage(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) { PageInfo<User> page = userService.selectPage(pageNum, pageSize); System.out.println(page.getPageSize()); return page; }

4. UserService 接口:

public PageInfo<User> selectPage(int pageNum, int pageSize);

 

5. UserServiceImpl 实现类:

    @Autowired
    private UserMapper userMapper;

    public PageInfo<User> selectPage(int pageNum, int pageSize) {
        // 将参数传给这个方法就可以实现物理分页了,非常简单。
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.selectAll();
        System.out.println(list.size());
        PageInfo page = new PageInfo(list);
        return page;
    }

在 查询语句  userMapper.selectAll(); 前面加上:  PageHelper.startPage(pageNum, pageSize);  则后面的查询语句即被加上了分页的功能。

6. UserMapper 接口: List<User> selectAll();

7. UserMapper.xml  文件:

  <select id="selectAll" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List" />
    from se_user
  </select>

浏览器中访问:http://localhost:8080/user/selectPage/5/3

日志如下:

2019-06-03 17:43:24.713 DEBUG 30808 --- [nio-8080-exec-5] c.e.b.mapper.UserMapper.selectAll_COUNT  : ==>  Preparing: SELECT count(0) FROM se_user 
2019-06-03 17:43:24.713 DEBUG 30808 --- [nio-8080-exec-5] c.e.b.mapper.UserMapper.selectAll_COUNT  : ==> Parameters: 
2019-06-03 17:43:24.783 DEBUG 30808 --- [nio-8080-exec-5] c.e.b.mapper.UserMapper.selectAll_COUNT  : <==      Total: 1
2019-06-03 17:43:24.783 DEBUG 30808 --- [nio-8080-exec-5] c.e.boot2.mapper.UserMapper.selectAll    : ==>  Preparing: select sid, user_num, user_real_name, user_nick_name, user_id_code, 
user_address, user_head_pic, user_self_head_pic, user_id_code_front_pic, user_id_code_back_pic, user_approve_status, user_create_date, user_longitude, user_latitude, user_frequent_address,
user_service_num, user_speciality, user_balance, user_service_status, user_apply_datetime, user_service_city, user_company, user_age, user_sex, user_refusal_reason, user_collection_num, user_mcount,
user_telephone, user_approved_datetime, user_is_star, user_invite_no, two_dimension_code, used_capacity, max_capacity, friend_background, click_num, is_discount, user_role, is_use, user_stock_num, is_agent,
member_point, member_glory, member_level, frozen_money, agent_create_time, business_code, user_address_detail, is_sign, user_source, is_house_manager, data_source, deposit_money from se_user LIMIT 12,3 2019-06-03 17:43:24.783 DEBUG 30808 --- [nio-8080-exec-5] c.e.boot2.mapper.UserMapper.selectAll :
==> Parameters: 2019-06-03 17:43:24.789 DEBUG 30808 --- [nio-8080-exec-5] c.e.boot2.mapper.UserMapper.selectAll : <== Total: 3 3 3

返回 3条数据:

 

posted @ 2019-06-03 17:46  zhoudingzhao  阅读(4444)  评论(1编辑  收藏  举报