SSM + PageHelper implement paging

 Reference Address: https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md

First, prepare environmental engineering structures

Environment: IDEA, JDK1.8, MAVEN

 note:

Due to the different IDEA and Eclipse file parsing

Xml configuration file so that resources in the location of the file coincides with the position in the package java

 

Second, the integration of Spring + MyBatis

pom.xml

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

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.2.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>

  

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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"> <display-name>pagehelper</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

  

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">
    <! - package scanning annotation -> 
    <context: Scan-Component Base -package = " com.mrchengs " /> 
    <! - Configuration Database -> 
    <context: Property-placeholder LOCATION = " CLASSPATH: DB. Properties " /> 
    <-! load profile -> 
    <the bean ID = " the dataSource "  class = " org.apache.commons.dbcp.BasicDataSource " > 
        <Property name = " driverClassName " value = " $ {jdbc.driver } " > </ Property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 配置sqlSessionFactory, 并将数据源注入 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <Property name =
        <! - the introduction of data sources ->"dataSource" ref="dataSource"></property>
        <!--载入mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
        <!--载入配置mapper映射的xml-->
        <!--<property name="mapperLocations" value="classpath:com/mrchengs/mapper/*.xml"/>-->

        <property name="typeAliasesPackage" value="com.mrchengs"/>
    </bean>

    <!--配置mapperFactory -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.mrchengs.mapper.UserMapper"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
</beans>

 

 

db.properties

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

 

 

log4j.properties

Logging the Configuration #global 
# logging level in the development environment to be set up to 
log4j.rootLogger = DEBUG, stdout 
# Console the 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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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 "> 
    <- scan annotation package ->! 
    <context: the Component-scan Base - = Package Penalty for " com.mrchengs.controller " /> 
    <- open comments ->! 
    <MVC: annotation-Driven /> 
    ! <- static resource access -> 
    <MVC: default -servlet-Handler />

    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

 

 

Mybatis profile

SqlMapConfig.xml

The introduction of plug-pageHelper 
attention here to write PageInterceptor, prior to version 5.0 are written PageHelper, to be replaced after 5.0 PageInterceptor
<? xml Version = " 1.0 " encoding = " UTF-8 " ?> 
<! DOCTYPE the Configuration 
        the PUBLIC " - // mybatis.org//DTD Config 3.0 // EN " 
        " http://mybatis.org/dtd/mybatis config.dtd--3 " > 
<Configuration>     <- page ->! 
    <plugins> 
        <= plugin Interceptor" com.github.pagehelper.PageInterceptor "> 
            <- Reasonable:! tab rationalization parameters, default value is false . 
                When this parameter is set to true, pageNum <= queries the first page 0, 
                pageNum> pages (more than the total number), queries the last page. 
                Default false, according to a direct query parameters. ->
    

            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>


    <!-- 加载 映射文件 -->
    <mappers>
       <package name="com.mrchengs.mapper"/>
    </mappers>
</configuration>

 

 

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<User>  allUser() throws  Exception;
}

 

 

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper 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>
</mapper>

 

 

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
<User> userList = userMapper.allUser(); PageInfo<User> pageInfo = new PageInfo<>(userList); request.addAttribute("pageInfo",pageInfo); return "test"; } }

 

 

Three, jsp page display

index.jps

<html>
<body>
<h2>Hello World!</h2>
</body>

<a href="<%=request.getContextPath()%>/test">test</a>
</html>

 

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" %><html>
<head>
    <title>Title</title>
</head>
<body>
<center>
    <table width="200" border="1">
        <tr>
            <th scope="col">name</th>
            <th scope="col">age</th>
        </tr>
        <c:forEach items="${pageInfo.list}" var="user">
            <tr>
                <td>${user.user}</td>
                <td>${user.password}</td>
            </tr>
        </c:forEach>
    </table>
    <p>当前 ${pageInfo.pageNum }页,总${pageInfo.pages }
        页,总 ${pageInfo.total } 条记录</div></p>
    <a href="test?pageNo=${pageInfo.firstPage}">第一页</a>


    <c:if test="${pageInfo.hasPreviousPage }">
        <a href="test?pageNo=${pageInfo.pageNum-1}">上一页</a>
    </c:if>

    <c:if test="${pageInfo.hasNextPage }">
        <a href="test?pageNo=${pageInfo.pageNum+1}">下一页</a>
    </c:if>

    <a href="test?pageNo=${pageInfo.lastPage}">最后页</a>
</center>
</body>
</html>

 

 

Fourth, the test

 

 

|

 

|

 

Guess you like

Origin www.cnblogs.com/Mrchengs/p/11441612.html
Recommended