參看了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 > < 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>接收
如果你有疑問可以下載開源項目lenos 快速開發腳手架,spring boot 版本來熟悉學習。
地址:https://gitee.com/bweird/lenosp
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://my.oschina.net/u/3312115/blog/1600825