ssm+PageHelper插件实现分页查询

在springmvc+mybatis表单增删改查的基础上,然后通过mybatis的分页插件pagehelp进行分页查询。

源码:https://gitee.com/smfx1314/pagehelper

项目结构:

项目结构

pom.xml


        
            org.springframework
            spring-core
            4.3.2.RELEASE
        
        
            org.springframework
            spring-aop
            4.3.2.RELEASE
        
        
            org.springframework
            spring-aspects
            4.3.2.RELEASE
        
        
            org.springframework
            spring-beans
            4.3.2.RELEASE
        
        
            org.springframework
            spring-context
            4.3.2.RELEASE
        
        
            org.springframework
            spring-expression
            4.3.2.RELEASE
        
        
            org.springframework
            spring-jdbc
            4.3.2.RELEASE
        
        
            org.springframework
            spring-test
            4.3.2.RELEASE
        
        
            org.springframework
            spring-tx
            4.3.2.RELEASE
        
        
            org.springframework
            spring-web
            4.3.2.RELEASE
        
        
            org.springframework
            spring-webmvc
            4.3.2.RELEASE
        
        
            org.mybatis
            mybatis-spring
            1.3.0
        
        
            org.mybatis
            mybatis
            3.4.4
        
        
            commons-io
            commons-io
            2.4
        
        
            org.apache.commons
            commons-lang3
            3.4
        
        
            commons-logging
            commons-logging
            1.1.1
        
        
            org.apache.logging.log4j
            log4j-core
            2.9.1
        
        
            org.slf4j
            slf4j-log4j12
            1.7.21
            test
        
        
            aopalliance
            aopalliance
            1.0
        
        
            mysql
            mysql-connector-java
            5.1.38
        
        
            com.mchange
            c3p0
            0.9.5.2
        
        
            javax.servlet
            jstl
            1.2
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        
        
        
            javax.servlet.jsp
            javax.servlet.jsp-api
            2.2.1
            provided
        

        
            taglibs
            standard
            1.1.2
        
        
            com.github.pagehelper
            pagehelper
            5.1.2
        

    
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.6.1
                
                    utf-8
                
            

            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
                
                    UTF-8
                
            
        
    

pom.xml中引入page分页的jar包

 
          com.github.pagehelper
          pagehelper
          5.1.2
      

jar包这里就引入完了。下面引入配置文件

applicationContext.xml


    
   
   
   
   
   
       
       
       
       
                    
   
   
       
       
       
       
       
        
    
   
   
       
       
   
   
   
       
     
   
   
   
   
   

springmvc.xml



        
        
        
        
        
        

        
        
            
            
            

mybatis-config.xml





    
      
          
              
              
          
     

以上你也可以直接配置的applicationContext中。

web.xml



  pagehelper
  
    index.jsp
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
    contextConfigLocation
    classpath:applicationContext.xml
  
  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springmvc.xml
    
    1
  
  
    springmvc
    /
  
  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      UTF-8
    
  
  
    encodingFilter
    /*
  

controller

package com.jiangfx.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiangfx.entity.User;
import com.jiangfx.service.UserService;

@Controller
public class UserController {
    
    @Autowired
    private UserService pageService;
    
    /**
     * 分页查询
     */
    @RequestMapping(value="/list",method=RequestMethod.GET)
    public String pageList(ModelMap map,@RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo){
        Integer pageSize=4;//每页显示记录数
        //分页查询
        PageHelper.startPage(pageNo, pageSize);
        List userList = pageService.list();//获取所有用户信息
        PageInfo pageInfo=new PageInfo(userList);
        map.addAttribute("pageInfo", pageInfo);return "list";
    }

}

serviceImp :接口自己定义,这里就不贴了

package com.jiangfx.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiangfx.entity.User;
import com.jiangfx.mapper.UserMapper;
import com.jiangfx.service.UserService;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    
    /**
     * 查询所有用户
     * @return
     */
    @Override
    public List list() {

        return userMapper.getAllUser();
    }

}

entity

package com.jiangfx.entity;

public class User {

    private Integer id;
    private String username;
    private String sex;
    private String city;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", city=" + city + "]";
    }
    
}

mapper

package com.jiangfx.mapper;

import java.util.List;

import com.jiangfx.entity.User;

public interface UserMapper {

    //查询所有用户
    List getAllUser();
}

mapper.xml





    
    

下面是jsp

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


分页查询


返回list

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>




Insert title here


  
ID 姓名 性别 城市
${user.id} ${user.username} ${user.sex} ${user.city}

当前 ${pageInfo.pageNum }页,总${pageInfo.pages } 页,总 ${pageInfo.total } 条记录

第一页 上一页 下一页 最后页

访问localhost:8080/pagehelper


分页查询

直接点击分页查询即可看到效果如下:


效果

你可能感兴趣的:(ssm+PageHelper插件实现分页查询)