參看了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 */@Configurationpublic 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
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答
圖片精選