[置顶] Spring Boot整合PageHelper 实现数据库分页
2017-11-18 00:24
555 查看
最近学习了SpringBoot 由于需要数据库分页功能 再由本人比较懒就直接上插件了 创建maven项目 这个相信大部分人都会 然后 添加spring boot jar包 这个是从别的地方整理下来并不是个人项目使用(没有经过测试谨慎使用) 官网 https://github.com/pagehelper/Mybatis-PageHelper
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.luyh.projectv1</groupId> <artifactId>parent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <modules> <module>model</module> <module>dao</module> <module>service</module> <module>webapi</module> </modules> <!--申明依赖关系--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <!--设置maven仓库--> <repositories> <repository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-releases</id> <url>https://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories> </project>
然后加载PageHelper 插件 在pom中添加
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency>
现在框架就整合完成了
然后用工具mybatis-generator-core-1.3.2(我就不提供资源了) 把数据库中的表生成模型,映射文件和xml文件 导入工程中,在建立Application.java类
@SpringBootApplication
@MapperScan(“”) //扫描Dao包
@EnableTransactionManagement //开启事务
在本类的Main方法中添加,当然一个字不用修改
@Bean public PageHelper pageHelper(){ PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("offsetAsPageNum","true"); properties.setProperty("rowBoundsWithCount","true"); properties.setProperty("reasonable","true"); properties.setProperty("dialect","mysql"); //配置mysql数据库的方言 pageHelper.setProperties(properties); return pageHelper; }
新建控制层
@RestController 这个应该都知道干什么的吧 我就不说了
@Autowired
私有化服务层的接口 //当然我们现在还没有建服务层
@RequestMapping("你的请求") public List<UserMst> selectAge(int currentPage,int pageSize){ //第一个参数是第几页,第二个参数是一个页面几个数据 return cmm.Select(currentPage, pageSize); }
新建服务层包
建立服务层接口,在建立imp文件夹 在imp中建一个类实现接口
当然别忘记添加@service
public List<UserMst> Select(int age, int pageSize) { // TODO Auto-generated method stub PageHelper.startPage(age, pageSize); List<UserMst> allItems = umm.findAll(); //umm为Mapper文件名 int countNums = umm.countItem(); PageBean<UserMst> pageData = new PageBean<UserMst>(age,pageSize,countNums); pageData.setItems(allItems); return pageData.getItems(); }
写入方法
当然PageHelper是一个Bean类 直接从官网拷过来直接可以用的
import java.util.List; public class PageBean<T> { // 当前页 private Integer currentPage = 1; // 每页显示的总条数 private Integer pageSize = 10; // 总条数 private Integer totalNum; // 是否有下一页 private Integer isMore; // 总页数 private Integer totalPage; // 开始索引 private Integer startIndex; // 分页结果 private List<T> items; public PageBean() { super(); } public PageBean(Integer currentPage, Integer pageSize, Integer totalNum) { super(); this.currentPage = currentPage; this.pageSize = pageSize; this.totalNum = totalNum; this.totalPage = (this.totalNum+this.pageSize-1)/this.pageSize; this.startIndex = (this.currentPage-1)*this.pageSize; this.isMore = this.currentPage >= this.totalPage?0:1; } public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getTotalNum() { return totalNum; } public void setTotalNum(Integer totalNum) { this.totalNum = totalNum; } public Integer getIsMore() { return isMore; } public void setIsMore(Integer isMore) { this.isMore = isMore; } public Integer getTotalPage() { return totalPage; } public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } public Integer getStartIndex() { return startIndex; } public void setStartIndex(Integer startIndex) { this.startIndex = startIndex; } public List<T> getItems() { return items; } public void setItems(List<T> items) { this.items = items; } }
最后在自己的xml文件 中写自己的查询方法吧 然后大功告成 本人就是小白一个,好不容易自己实现了功能,也就分享自己的成果,给以后的初学者留一个路,也是给自己锻炼锻炼写博客
相关文章推荐
- Spring与Mybatis+PageHelper实现分页整合(三)
- spring boot 整合mybatis分页插件pagehelper5.1
- 【Spring+SpringMVC+MyBatis深入学习及搭建】18.MyBatis+PageHelper实现分页
- bootstrap与pagehelper实现分页
- Spring+SpringMVC+Mybatis+PageHelper+laypage+Ajax实现的分页
- SpringBoot+Mybatis+ Druid+PageHelper 实现多数据源并分页
- 基于spring boot的mysql使用pagehelper实现分页功能
- SSM框架中集成TKMybatis+PageHelper实现高效查询和分页
- ASP.NET MVC4 HtmlHelper扩展类,实现分页功能 @Html.ShowPageNavigate
- Spring Boot学习笔记----分页查询(pageHelper)
- pageHelper分页插件实现原理及使用方法
- Mybatis 数据库物理分页插件 PageHelper
- Mybatis学习(4):Mybatis及PageHelper插件和easyUI实现分页
- SSM+easyUI结合Mybatis-PageHelper实现分页功能
- 采用shardbatis在springBoot中实现表的水平拆分,整合swagger,mybatis,shardbatis,pagehelper
- Mybatis的pageHelper插件实现分页
- SSM框架集成PageHelper插件,实现分页功能
- [Extjs] Ext4 Ext.grid.Panel 分页实现(mybatis 分页插件-PageHelper 使用)
- PageHelper+Bootstrap Paginator实现分页效果
- spring+Mybatis+ PageHelper实现分页