天天看點

MyBatis 分頁插件 - PageHelper

1、簡單介紹Mybaits和PageHelper

Mybaits超強的動态sql功能和性能使得它是目前開發中最常使用的ORM持久化架構;PageHelper是一款開源免費的Mybatis第三方實體分頁插件,該插件目前支援orcale、mysql、DB2等資料庫的實體分頁。但Mybaits版本需要3.1.0+才能支援使用PageHelper分頁插件。

2、快速搭建項目執行個體

a.使用maven,在pom.xml檔案中添加依賴

springboot添加依賴:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>
<!--mapper-->
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>1.1.0</version>
</dependency>

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

說明:這裡要注意下mybatis-spring-boot-starter請不要使用1.0.0版本,因為還不支援攔截器插件。

本文中主要記錄springboot架構中整合問題,如果你使用的項目中沒有使用到它,也沒有關系。那麼可以直接添加PageHelper的jar包,配置檔案以正确的方式放在自己項目mybatis-config.xml檔案中即可(具體檔案名要看項目)。

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

b.檔案application.yml 配置

mybatis:
#    type-aliases-package: tk.mybatis.springboot.model
    mapper-locations: classpath:com/zhang/financial/**/*.xml

mapper:
    mappers:
        - com.zhang.financial.nirvana.core.mapper.BaseMapper
    not-empty: false
    identity: oracle

pagehelper:
    helperDialect: oracle
    returnPageInfo: check
    supportMethodsArguments: true
    reasonable: true
    params: count=countSql
           

說明:

returnPageInfo: check –>總是傳回PageInfo類型,check檢查傳回類型是否為PageInfo,none傳回Page。

supportMethodsArguments: true –> 支援通過Mapper接口參數來傳遞分頁參數

reasonable: true –> 如果pageNum<1會查詢第一頁,如果pageNum>pages會查詢最後一頁

params: count=countSql –> 支援startPage(Object params)方法

c.建立basebean

public class BaseBean implements Serializable {
    //==============分頁字段開始================
    private Integer pageNum;//目前頁
    private Integer pageSize;//目前頁數量
    private String orderBy;
    private Integer pn = Integer.valueOf();
    private Integer ps = Integer.valueOf();
    //==============分頁字段結束================
    getter/setter省略
}
           

basebean是封裝的基礎bean,其中預設pageNum=1,pageSize=10

d.建立TestController進行測試

@RestController
@RequestMapping(value="/demo")
public class HelloController {

    @Autowired
    AccountFlowService accountFlowService;

    @RequestMapping("/pagehelper")
    public JSONObject demo() {
        JSONObject result = new JSONObject();
        JSONObject data = new JSONObject();
        MerchantFlowBean bean = new MerchantFlowBean();
        Page<MerchantFlowDto> mfList = accountFlowService.selectFlow(bean);
        if (mfList != null && mfList.size() > ) {
            data.put("tp", mfList.getPages());//總頁數
            data.put("rc", mfList.getTotal());//總條數
            data.put("flow", mfList.getResult());
            result.put("code", "1");
            result.put("desc", "查詢商家賬号流水");
            result.put("data", data);
            logger.info("查詢商家賬号流水成功");
        } else {
            //省略
        }
        return result;
    }
}
           

f.測試結果

{
    "code":"1",
    "data":{
        "rc":,
        "tp":,
        "flow":[
            {
                "iflowid":,
                "inout":"0",
                "imoney":"100",
                "ioldmoney":".5",
                "icurrbalance":"100.5",
                "cadddate":"2017-04-07 17:43:22",
                "num":"1"
            },
            {
                "iflowid":,
                "inout":"1",
                "imoney":"3",
                "ioldmoney":"2800",
                "icurrbalance":"2797",
                "cadddate":"2017-04-07 17:32:37",
                "num":"2"
            },
            {
                "iflowid":,
                "inout":"1",
                "imoney":"3",
                "ioldmoney":"1089",
                "icurrbalance":"1086",
                "cadddate":"2017-04-07 17:31:32",
                "num":"3"
            }
        ]
    },
    "desc":"查詢商家賬号流水"
}
           

繼續閱讀