本文主要是介绍springboot使用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 {@AutowiredDataSource 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<>(8);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 {@Transientprivate Integer page = 1;@Transientprivate Integer rows = 10;@Transientprivate String orderBy;@JsonIgnorepublic String getOrderBy() {return orderBy;}public void setOrderBy(String orderBy) {this.orderBy = orderBy;}@JsonIgnorepublic Integer getPage() {return page;}public void setPage(Integer page) {this.page = page;}@JsonIgnorepublic 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) {}
结果
@Testpublic void getCustomersTest(){Customer page = new Customer();page.setRows(10);page.setPage(2);List<Customer> list = customerService.getAllCustomers(page);System.out.println("list = " + list);}
//打印的结果SELECT * FROM customer WHERE id <> 'common0000' order by id limit ?,?
DEBUG [main] - ==> Parameters: 0(Integer), 10(Integer)
可能有些不太详细,具体可以参考插件作者的文档哦。
https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
这篇关于springboot使用pageHelper分页插件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!