SSM+PageHelper实现分页

 参考地址:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md

一、环境准备工程搭建

环境:IDEA、JDK1.8、MAVEN

SSM+PageHelper实现分页_第1张图片

 注意:

由于IDEA与Eclipse文件解析不同

所以xml配置文件在resources文件中的位置与在java包的位置一致

 

二、整合Spring + MyBatis

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
        
        
        
            commons-dbcp
            commons-dbcp
            1.4
        
    

  

注意添加pageHelper依赖

        
            com.github.pagehelper
            pagehelper
            5.1.2
        

 

web.xml


xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 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 /*

  

applicationContext.xml

"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans.xsd
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring-tx.xsd
                     http://www.springframework.org/schema/aop
                     http://www.springframework.org/schema/aop/spring-aop.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context.xsd">
    
    base-package="com.mrchengs"/>
    
    "classpath:db.properties"/>
    
    "dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        "driverClassName" value="${jdbc.driver}">
        "url" value="${jdbc.url}">
        "username" value="${jdbc.username}">
        "password" value="${jdbc.password}">
    
    
    "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        "dataSource" ref="dataSource">
        
        "configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
        
        

        "typeAliasesPackage" value="com.mrchengs"/>
    

    
    "userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        "mapperInterface" value="com.mrchengs.mapper.UserMapper">
        "sqlSessionFactory" ref="sqlSessionFactory">
    

 

 

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/shijian
jdbc.username=root
jdbc.password=1234567890

 

 

log4j.properties

#Global logging configuration
# 在开发环境下日志级别要设成
log4j.rootLogger = DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =%5p [%t] - %m%n

 

 

springmvc.xml

"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    base-package="com.mrchengs.controller"/>
    
    
    
    default-servlet-handler/>

    
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        "prefix" value="/WEB-INF/jsp/">
        "suffix" value=".jsp">
    

 

 

Mybatis的配置文件

SqlMapConfig.xml

引入 pageHelper插件 
注意这里要写成PageInterceptor, 5.0之前的版本都是写PageHelper, 5.0之后要换成PageInterceptor
"1.0" encoding="UTF-8" ?>
DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

    
    
    
        
            
            
        
    


    
    
       "com.mrchengs.mapper"/>
    

 

 

UserMapper.java

package com.mrchengs.mapper;

import com.mrchengs.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
@Mapper
public interface UserMapper {

    public List  allUser() throws  Exception;
}

 

 

UserMapper.xml

"1.0" encoding="UTF-8" ?>
DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
namespace="com.mrchengs.mapper.UserMapper">
    <select id="getAllUser"  resultType="com.mrchengs.User">
           SELECT * FROM USERS
     select>

    <select id="allUser" resultType="com.mrchengs.User">
        select  * from  users
    select>

 

 

User.java

package com.mrchengs;

public class User {

    private String user;
    private  Integer password;

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public Integer getPassword() {
        return password;
    }

    public void setPassword(Integer password) {
        this.password = password;
    }
}

 

 

pageHelperController.java

package com.mrchengs.controller;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrchengs.User;

import com.mrchengs.mapper.UserMapper;
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.RequestParam;


import java.util.List;

@Controller
public class pageHelperCOntroller {

    @Autowired
    UserMapper userMapper ;



    @RequestMapping("/test")
    public String  test(ModelMap request,
           @RequestParam(defaultValue="1",required=true,value="pageNo") Integer pageNo) throws Exception {//每页显示的数量
        Integer pageSize = 4;
        //分页查询
        PageHelper.startPage(pageNo, pageSize);
     //进行查询数据 List
userList = userMapper.allUser(); PageInfo pageInfo = new PageInfo<>(userList); request.addAttribute("pageInfo",pageInfo); return "test"; } }

 

 

三、页面显示的jsp

index.jps

 

test.jsp

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

    Title


"200" border="1"> "${pageInfo.list}" var="user">
"col">name "col">age
${user.user} ${user.password}

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

"test?pageNo=${pageInfo.firstPage}">第一页 if test="${pageInfo.hasPreviousPage }"> "test?pageNo=${pageInfo.pageNum-1}">上一页 if> if test="${pageInfo.hasNextPage }"> "test?pageNo=${pageInfo.pageNum+1}">下一页 if> "test?pageNo=${pageInfo.lastPage}">最后页

 

 

四、测试

SSM+PageHelper实现分页_第2张图片

 

 

|

SSM+PageHelper实现分页_第3张图片

 

|

SSM+PageHelper实现分页_第4张图片

 

转载于:https://www.cnblogs.com/Mrchengs/p/11441612.html

你可能感兴趣的:(SSM+PageHelper实现分页)