SSM 分页

首先在pom.xml中配置依赖


    
      com.github.pagehelper
      pagehelper
      5.0.0
    

添加完依赖,配置xml文件,这个配置有两种方式

如果是使用mybatis.xml+spring.xml+spring-mvc.xml 的方式,在mybatis.xml进行如下配置

 
        
        
            
           
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    
这个根据需要打开,我的打开会有异常,控制台还有有其他问题,进行单元测试时排出的错误,如图,此为自己的排错方式

SSM 分页_第1张图片

SSM 分页_第2张图片

另外一种是spring.xml+spring-mvc.xml 的方式,那就在spring.xml配置,如下

   
    
        
        
        

        
        
            
                
                    
                        

                            reasonable=true
                        
                    
                
            
        
    

以上方式,任选,根据需要进行配置

xml配置完毕,创建一个工具类

@Component
public class PageParams {
	/** 页码 */
	private int pageNo;
	/** 每页查询数目 */
	private int pageSize;

	public PageParams() {
		// TODO Auto-generated constructor stub
	}

	public PageParams(int pageNo, int pageSize) {
		super();
		this.pageNo = pageNo;
		this.pageSize = pageSize;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

}

工具类创建好进行分页查询

 //分页查询,分类查询或查询全部
    @Override
    public PageInfo selectAll(FactoryC record, PageParams pageParams) {
        PageHelper.startPage(pageParams.getPageNo(), pageParams.getPageSize());
        List list = factoryCMapper.selectAll(record);
        PageInfo pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
List list = factoryCMapper.selectAll(record);这个是本人调用mapper的接口按需求查出所有数据,它的上下两句代码是必写的,此时mapper.xml文件不需要进行limit的分页操作,这两句代码按传入的页码与查询数量自动分好页,实现原理可以看底层代码

你可能感兴趣的:(SSM 分页)