SpringBoot集成MyBatis的分頁插件PageHelper--詳細步驟

1.pom中添加依賴包

 1         <!--pageHelper基本依賴 -->
 2         <dependency>
 3             <groupId>com.github.pagehelper</groupId>
 4             <artifactId>pagehelper</artifactId>
 5             <version>5.1.2</version>
 6         </dependency>
 7         <!-- 在個人實驗中不加這兩個依賴分頁不會成功 -->
 8         <dependency>
 9             <groupId>com.github.pagehelper</groupId>
10             <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
11             <version>1.2.5</version>
12         </dependency>
13         <dependency>
14             <groupId>com.github.pagehelper</groupId>
15             <artifactId>pagehelper-spring-boot-starter</artifactId>
16             <version>1.2.5</version>
17         </dependency>

 

2.配置分頁插件

  下面兩者選一配置java

2.1.application.properties配置

  在application.properties文件中添加以下配置mysql

1 #分頁插件
2 pagehelper.helper-dialect=MYSQL
3 pagehelper.reasonable=true
4 pagehelper.support-methods-arguments=true
5 pagehelper.params=count=countSql

 

2.2.配文件配置對象

 1 package com.qiaXXXXXX.config;
 2 
 3 import com.github.pagehelper.PageHelper;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 
 7 import java.util.Properties;
 8 
 9 /**
10  * @Copyright (C) 四川XXXX
11  * @Author: LI 
12  * @Date: 7/1 11:01
13  * @Description:
14  */
15 @Configuration
16 public class PageHelperConfig {
17     @Bean
18     public PageHelper getPageHelper() {
19         PageHelper pageHelper = new PageHelper();
20         Properties properties = new Properties();
21         properties.setProperty("helperDialect", "mysql");
22         properties.setProperty("reasonable", "true");
23         properties.setProperty("supportMethodsArguments", "true");
24         properties.setProperty("params", "count=countSql");
25         pageHelper.setProperties(properties);
26         return pageHelper;
27     }
28 
29 }

 

3.分頁實現

    @Override
    public PageInfo<MyOrderVo> getMyOrder(MyOrderObj obj) {
        if (StringUtils.isEmpty(obj.getQueryMonth())) {
            String endMonth = DateUtil.getEndMonth();
            obj.setStartMonth(DateUtil.getStartMonth(endMonth));
            obj.setEndMonth(endMonth);
        } else {
            obj.setStartMonth(null);
            obj.setEndMonth(null);
        }
        //設置分頁參數
        PageHelper.startPage(obj.getPageNo(), obj.getPageSize());

        //查詢列表數據
        List<MyOrderVo> list = userCenterMapper.getMyOrder(obj);
        
        //獲取分頁對象
        PageInfo<MyOrderVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

 

 

  注意:有了分頁插件,sql語句不須要寫limit,插件會在執行的sql中自動添加,也不須要本身單獨寫count語句獲取總共條數,分頁插件會自動獲取.git

相關文章
相關標籤/搜索