欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

spring boot集成pagehelper(两种方式)

时间:2021-05-25 08:22:43|栏目:JAVA代码|点击:

参看了pagehelper-spring-boot,使用起来非常放方便,关于更多PageHelper可以点击https://github.com/pagehelper/Mybatis-PageHelper

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

 <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)中加入

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

然后重启即可。

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

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

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 基础知识为:

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

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

如果你有疑问可以下载开源项目lenos 快速开发脚手架,spring boot 版本来熟悉学习。

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

上一篇:Java异常分类及统一处理详解

栏    目:JAVA代码

下一篇:深入理解Java高级特性――注解

本文标题:spring boot集成pagehelper(两种方式)

本文地址:http://www.codeinn.net/misctech/128587.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有