SSM博客开发:后台插件-PageHelper

一、PageHelper简介

PageHelper是Mybatis的一个分页插件,能够有效快速地帮助开发者完成分页的Java后台代码。熟练Spring+Mybatis+PageHelper的配置后对于普通的分页查询,能够极简地完成。从上面的GitHub地址中能够看到相关的使用说明,这里简要说一下在Spring+SpringMVC+Mybatis的博客开发中的使用。

二、配置

2.1 先看一下官网的配置

在Maven的pom.xml中添加如下代码:


    com.github.pagehelper
    pagehelper
    latest version


在 mybatis-config.xml 文件中的配置



    
        
        
    


在 Spring application.xml文件中的配置


  
  
    
      
        
          
          
            param1=value1
          
        
      
    
  

2.2 博客项目中的配置

2.2.1 pom中Mybatis配置


      org.mybatis
      mybatis
      3.3.0

注意,这里用到的Mybatis版本3.3.0

2.2.2 pom中Spring配置


      org.springframework
      spring-context
      4.2.5.RELEASE
    

    
      org.springframework
      spring-test
      4.2.5.RELEASE
    
    
      org.springframework
      spring-webmvc
      4.2.5.RELEASE
    

    
      org.springframework
      spring-context-support
      4.2.5.RELEASE
    
    
      org.springframework
      spring-core
      4.2.5.RELEASE
    
    
      org.springframework
      spring-jdbc
      4.2.5.RELEASE
    
    
      org.mybatis
      mybatis-spring
      1.1.1
    

2.2.3 pom中PageHelper配置


    
      com.github.pagehelper
      pagehelper
      4.0.0
    

注意,这里用到的PageHelper版本4.0.0


解释一下为什么要强调MybatisPageHelper的版本,GitHub上PageHelper版本已经出了5.0+了,但由于项目中用到的Maybatis版本为3.3.0,PageHlper版本在4.0.0时不会报错(其他4.0+的版本没有测试),或者使用5.0+版的PageHelper,相应的Mybatis的版本也要提升,否则会报错。

2.2.4 mybatis-config.xml中的配置


  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
    
    
    
    
  
  

2.2.5 spring-mybatis.xml中的配置


        
         
        
            
                
                classpath*:com/haitao/common/mapper/*Mapper.xml
            
        

三、实际调用

 @RequestMapping(value="/archives/reading")
    public String readingArchive(Model model, HttpSession httpSession){
        int pageNumber=1;
        PageHelper.startPage(pageNumber, 5);                        // (第一个参数)pageNumber:第几页;(第二个参数)5:每页显示条数
        List
articles = articleService.findArticlesByCat(1); // 直接调用Service就可 model.addAttribute("articles",articles); getAllPages(pageNumber,model,httpSession,articles); for(Article article:articles){ System.out.println(article.getTitle()); } return "read"; }

四、坑爹小问题

  1. jsp标签不能正常使用
    解决:需要在jsp文件头加上:<%@ page isELIgnored="false" %>
  2. 启动tomcat时报错不能找到Mapper对应的Bean
    解决:检查一下pom.xml中有没有添加资源配置没有的话加上,如我的项目中
    
      
        src/main/resources
        
          **/*.properties
          **/*.xml
          **/*.ini
        
        false
      
      
        src/main/java
        
          **/*.properties
          **/*.xml
        
        false
      
    
    

你可能感兴趣的:(SSM博客开发:后台插件-PageHelper)