1. 程式人生 > >SpringBoot學習-(七)SpringBoot分頁外掛PageHelper

SpringBoot學習-(七)SpringBoot分頁外掛PageHelper

訪問資料庫採用mybatis框架

1.新增pom檔案依賴

<!-- spring mvc支援 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- springboot整合mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId
>
<artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!-- springboot分頁外掛 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId
>
<!-- 特別注意版本問題, 看到評論以後得以糾正 --> <version>1.2.3</version> </dependency> <!-- 阿里巴巴druid資料庫連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.3</version> </dependency
>
<!-- mysql驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>

2.配置application.yml

# 與mybatis整合
mybatis:
  config-location: classpath:mybatis.xml
  mapper-locations:
  - classpath:mapper/*.xml

# 分頁配置
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

3.service層中使用外掛

package com.ahut.serviceImpl;

import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.ContextLoader;

import com.ahut.entity.GoodsType;
import com.ahut.mapper.GoodsTypeMapper;
import com.ahut.service.GoodsTypeService;
import com.github.pagehelper.PageHelper;

/**
 * 
 * @ClassName: GoodsTypeServiceImpl
 * @Description: 商品型別業務邏輯處理
 * @author cheng
 * @date 2017年7月17日 上午10:04:31
 */
@Service
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class GoodsTypeServiceImpl implements GoodsTypeService {
    // 資料訪問
    @Autowired
    private GoodsTypeMapper typeDao;

    /**
     * 
     * @Title: getList
     * @Description: 從資料庫中獲取所有商品型別列表
     * @param pageNum 當前頁
     * @param pageSize 當前頁面展示數目
     * @return
     * @throws Exception
     */
    public List<GoodsType> getList(int pageNum, int pageSize) throws Exception {
        //使用分頁外掛,核心程式碼就這一行
        PageHelper.startPage(pageNum, pageSize);
        // 獲取
        List<GoodsType> typeList = typeDao.getList();
        return typeList;
    }

}

4.controller層程式碼

package com.ahut.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ahut.entity.GoodsType;
import com.ahut.service.GoodsTypeService;

/**
 * 
 * @ClassName: GoodsTypeAction
 * @Description: 商品型別控制層
 * @author cheng
 * @date 2017年7月17日 上午11:09:47
 */
@RestController // 等價於@[email protected]
public class GoodsTypeAction {
    // 業務邏輯
    @Autowired
    private GoodsTypeService typeService;

    /**
     * 
     * @Title: getGoodsTypeList
     * @Description: 獲取商品型別列表
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getGoodsTypeList")
    public List<GoodsType> getGoodsTypeList(int pageNum, int pageSize) throws Exception {
        // 呼叫業務邏輯,返回資料
        return typeService.getList(pageNum,pageSize);
    }

}

5.測試

已知我資料庫中有九條資料:

這裡寫圖片描述

正常情況:

1.顯示第一頁或者第二頁資料
請求url:

http://localhost:8080/getGoodsTypeList?pageNum=1&pageSize=4

返回資料:

[
    {
        "typeId": "708cc61c6a9811e796dee09467355fab",
        "typeName": "全部",
        "createTime": 1500258859000,
        "updateTime": 1500621762000
    },
    {
        "typeId": "98f8a04e6a9811e796dee09467355fab",
        "typeName": "考研資料",
        "createTime": 1500258927000,
        "updateTime": null
    },
    {
        "typeId": "b720c87f6a9811e796dee09467355fab",
        "typeName": "交通工具",
        "createTime": 1500258978000,
        "updateTime": null
    },
    {
        "typeId": "cbe3c2326a9811e796dee09467355fab",
        "typeName": "生活用品",
        "createTime": 1500259013000,
        "updateTime": 1500626046000
    }
]

2.顯示最後一頁
請求url:

http://localhost:8080/getGoodsTypeList?pageNum=3&pageSize=4

返回資料:

[
    {
        "typeId": "d992195f6df111e7bab4e09467355fab",
        "typeName": "測試2改變了",
        "createTime": 1501145516000,
        "updateTime": 1500716178000
    }
]

不正常情況:
1.顯示的頁數小於第一頁(顯示第一頁資料)

pageNumber <= 0

請求url:

http://localhost:8080/getGoodsTypeList?pageNum=0&pageSize=4

返回資料:

[
    {
        "typeId": "708cc61c6a9811e796dee09467355fab",
        "typeName": "全部",
        "createTime": 1500258859000,
        "updateTime": 1500621762000
    },
    {
        "typeId": "98f8a04e6a9811e796dee09467355fab",
        "typeName": "考研資料",
        "createTime": 1500258927000,
        "updateTime": null
    },
    {
        "typeId": "b720c87f6a9811e796dee09467355fab",
        "typeName": "交通工具",
        "createTime": 1500258978000,
        "updateTime": null
    },
    {
        "typeId": "cbe3c2326a9811e796dee09467355fab",
        "typeName": "生活用品",
        "createTime": 1500259013000,
        "updateTime": 1500626046000
    }
]

結論:當請求頁數小於第一頁時,顯示第一頁資料

2.顯示的頁數大於最後一頁(顯示最後一頁資料)
pageNum > 最後一頁

請求url:

http://localhost:8080/getGoodsTypeList?pageNum=4&pageSize=4

返回資料:

[
    {
        "typeId": "d992195f6df111e7bab4e09467355fab",
        "typeName": "測試2改變了",
        "createTime": 1501145516000,
        "updateTime": 1500716178000
    }
]

結論:當請求頁面大於最後一頁時,顯示最後一頁資料