spring boot集成pagehelper,两种方式

当spring boot集成好mybatis时候须要进行分页,咱们首先添加maven支持java

<dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.2</version>
    </dependency>
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
      <version>1.2.3</version>
    </dependency>
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>1.2.3</version>
    </dependency>

方式一:咱们在application.yml(spring 须要读取的yml)中加入mysql

pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql

而后重启便可。git

配置文件最终会被java所读取,最终注入到spring bean中,因此咱们方法二是配置其bean类,热加载方便修改固然方式一更简单,github

方式二:在注解涵盖package下面新建PageHeleperConfigspring

import com.github.pagehelper.PageHelper;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhuxiaomeng
 * @date 2018/1/2.
 * @email 154040976@qq.com
 */
@Configuration
public class PageHelperConfig {


  @Bean
  public PageHelper getPageHelper(){
    PageHelper pageHelper=new PageHelper();
    Properties properties=new Properties();
    properties.setProperty("helperDialect","mysql");
    properties.setProperty("reasonable","true");
    properties.setProperty("supportMethodsArguments","true");
    properties.setProperty("params","count=countSql");
    pageHelper.setProperties(properties);
    return pageHelper;
  }

}

pageHelper 基础知识为:sql

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
Page<T> tPage= PageHelper.startPage(page,limit);

下一句的查询语句来进行分页。你只须要用List<T>接收mybatis

若是你有疑问能够下载开源项目lenos 快速开发脚手架,spring boot 版原本熟悉学习。app

地址:https://gitee.com/bweird/lenospmaven

演示地址:www.lenosp.cnspring-boot

相关文章
相关标签/搜索