设为首页 加入收藏

TOP

SpringBoot整合PageHelper实现数据库分页的代码教程(一)
2017-11-19 09:06:58 】 浏览:580
Tags:SpringBoot 整合 PageHelper 实现 数据库分 代码 教程

最近学习了SpringBoot 由于需要数据库分页功能 再由本人比较懒就直接上插件了

创建maven项目 这个相信大部分人都会 然后 添加spring boot jar包 这个是从别的地方整理下来并不是个人项目使用(没有经过测试谨慎使用)


  

  
    
   
    4.0.0
   

    
   
    com.luyh.projectv1
   
    
   
    parent
   
    
   
    1.0-SNAPSHOT
   
    
   
    pom
   
    
    
    
     org.springframework.boot
     
    
     spring-boot-starter-parent
     
    
     1.3.3.RELEASE
     
   
    
    
    
     model
     
    
     dao
     
    
     service
     
    
     webapi
     
   

    
   
    
    
     
     
      org.springframework.boot
      
     
      spring-boot-starter-web
      
     
     
     
      org.springframework.boot
      
     
      spring-boot-starter-jdbc
      
     
     
     
      org.mybatis
      
     
      mybatis-spring
      
     
      1.2.2
      
     
     
     
      org.mybatis
      
     
      mybatis
      
     
      3.2.8
      
     
     
     
      org.apache.tomcat
      
     
      tomcat-jdbc
      
     
     
     
      mysql
      
     
      mysql-connector-java
      
     
   

    
   

    
    
     
     
      spring-releases
      
     
      https://repo.spring.io/libs-release
      
     
   
    
    
     
     
      spring-releases
      
     
      https://repo.spring.io/libs-release
      
     
   



  

然后加载PageHelper 插件 在pom中添加

 
  
      
   
    com.github.pagehelper
   
      
   
    pagehelper
   
      
   
    4.1.6
   
 
  

现在框架就整合完成了

然后用工具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
  
    selectAge(int currentPage,int pageSize){ //第一个参数是第几页,第二个参数是一个页面几个数据
    return cmm.Select(currentPage, pageSize);
}

  

新建服务层包

建立服务层接口,在建立imp文件夹 在imp中建一个类实现接口

当然别忘记添加@service

public List
  
    Select(int age, int pageSize) {
        // TODO Auto-generated method stub
        PageHelper.startPage(age, pageSize);
        List
   
     allItems = umm.findAll(); //umm为Mapper文件名 int countNums = umm.countItem(); PageBean
    
      pageData = new PageBean
     
      (age,pageSize,countNums); pageData.setItems(allItems); return pageData.getItems(); }
     
    
   
  

写入方法

当然PageHelper是一个Bean类 直接从官网拷过来直接可以用的

import java.util.List;

public class PageBean
  
    {
    // 当前页
    private Integer currentPage = 1;
    // 每页显示的总条数
    private Integer pageSize = 10;
    // 总条数
    private Integer totalNum;
    // 是否有下一页
    private Integer isMore;
    // 总页数
    private Integer totalPage;
    // 开始索引
    private Integer startIndex;
    // 分页结果
    private List
   
     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; } p
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Redis Basic在CentOS下安装指令 下一篇美团点评SQL优化工具SQLAdvisor开..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目