spring boot集成pagehelper(两种方式)

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
< 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)中加入

?
1
2
3
4
5
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

然后重启即可。

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 基础知识为:

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

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值