天天看點

springboot使用pageHelper分頁插件maven引入pageHelpermybatis配置分頁資訊使用PageHelper結果

maven引入pageHelper

<!--  https://github.com/pagehelper/Mybatis-PageHelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.0</version>
        </dependency>
           

mybatis配置分頁資訊

@Configuration
@EnableTransactionManagement
public class MybatisConfig implements TransactionManagementConfigurer {

    @Autowired
    DataSource dataSource;

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean() {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage("cn.vobile.hss.model");

        // 支援下劃線到駝峰
        org.apache.ibatis.session.Configuration conf = new org.apache.ibatis.session.Configuration();
        conf.setMapUnderscoreToCamelCase(true);
        bean.setConfiguration(conf);

        //分頁插件
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "pageNum=page;pageSize=rows;orderBy=orderBy");
        pageHelper.setProperties(properties);

        //添加插件
        bean.setPlugins(new Interceptor[]{pageHelper});

        //添加XML目錄
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            //讀取多個檔案下的xml檔案
            List<Resource> resources = new ArrayList<>();
            Resource[] resources1 = resolver.getResources("classpath*:mapper/*.xml");
            Resource[] resources2 = resolver.getResources("classpath*:sqlmapperext/*.xml");
            resources.addAll(Arrays.asList(resources1));
            resources.addAll(Arrays.asList(resources2));
            bean.setMapperLocations(resources.toArray(new Resource[]{}));
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
           
主要是對properties中進行的一些配置化資訊。
  • reasonable表示分頁合理化,如果pageNum<=0時候會查詢第一頁的資料。如果pageNum>總頁數,會查詢最後一頁的資料。
  • params中可以定義分頁屬性名稱,預設是pageNum和pageSize。你也可以定義其他的名稱。

    例如 properties.setProperty(“params”, “pageNum=page;pageSize=rows;orderBy=orderBy”);

  • pageSizeZero預設false,為true時候,如果pageSize=0時候查詢所有資料

使用PageHelper

//自定義分頁bo類。主要設定三個屬性。與params中設定的對應
public class BaseEntityWithPage {
    @Transient
    private Integer page = ;

    @Transient
    private Integer rows = ;

    @Transient
    private String orderBy;

    @JsonIgnore
    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    @JsonIgnore
    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    @JsonIgnore
    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }
}
           
//service調用處
public List<Customer> getAllCustomers(Customer page){
        page.setOrderBy("id");
        PageHelper.startPage(page);
        return customerMapper.getAllCustomers();
    }
           

直接調用PageHelper的startPage方法。

public static <E> Page<E> startPage(Object params) {}
           

結果

@Test
    public void getCustomersTest(){
        Customer page = new Customer();
        page.setRows();
        page.setPage();
       List<Customer> list = customerService.getAllCustomers(page);
        System.out.println("list = " + list);
    }
           
//列印的結果
    SELECT * FROM customer WHERE id <> 'common0000' order by id limit ?,? 
DEBUG [main] - ==> Parameters: (Integer), (Integer)
           

可能有些不太詳細,具體可以參考插件作者的文檔哦。

https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md

繼續閱讀