ssm+PageHelper插件實現(xiàn)分頁查詢

在springmvc+mybatis表單增刪改查的基礎上,然后通過mybatis的分頁插件pagehelp進行分頁查詢镇草。

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

項目結(jié)構(gòu):

項目結(jié)構(gòu)

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>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <uriEncoding>UTF-8</uriEncoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

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

 <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper</artifactId>
          <version>5.1.2</version>
  </dependency>    

jar包這里就引入完了尉咕。下面引入配置文件

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">    
   <!-- 掃描注解的包 -->
   <context:component-scan base-package="com.jiangfx.service"/>
   <!-- 配置數(shù)據(jù)庫 -->
   <!-- 加載配置文件 -->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
       <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/pagehelper"></property>
       <property name="user" value="root"></property>
       <property name="password" value="1234"></property>
   </bean>                 
   <!-- 配置sqlSessionFactory, 并將數(shù)據(jù)源注入 -->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!-- 引入數(shù)據(jù)源 -->
       <property name="dataSource" ref="dataSource"></property>
       <!--載入mybatis配置文件-->
       <property name="configLocation" value="classpath:mybatis-config.xml"/>
       <!--載入配置mapper映射的xml-->
        <property name="mapperLocations" value="classpath:com/jiangfx/mapper/*.xml"/>
   </bean> 
   <!-- 配置映射接口 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.jiangfx.mapper"/>
       <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
   </bean>
   <!-- 配置聲明式事務 -->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"></property>
   </bean>  
   <tx:annotation-driven transaction-manager="transactionManager"/>
   
   <!-- 配置aop -->
   
   
</beans>

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">
        <!-- 掃描注解的包 -->
        <context:component-scan base-package="com.jiangfx.controller"/>
        <!-- 開啟注解 -->
        <mvc:annotation-driven/>
        <!--靜態(tài)資源訪問-->
        <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-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <!-- 引入 pageHelper插件 -->
    <plugins>  
        <plugin interceptor="com.github.pagehelper.PageInterceptor">  
            <!--reasonable:分頁合理化參數(shù),默認值為false。
                當該參數(shù)設置為 true 時尺迂,pageNum<=0 時會查詢第一頁剥哑,
                pageNum>pages(超過總數(shù)時)硅则,會查詢最后一頁。
                默認false 時株婴,直接根據(jù)參數(shù)進行查詢怎虫。-->  
            <property name="reasonable" value="true"/>  
        </plugin>  
    </plugins> 
</configuration>

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

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>

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;//每頁顯示記錄數(shù)
        //分頁查詢
        PageHelper.startPage(pageNo, pageSize);
        List<User> userList = pageService.list();//獲取所有用戶信息
        PageInfo<User> pageInfo=new PageInfo<User>(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<User> 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<User> getAllUser();
}

mapper.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.jiangfx.mapper.UserMapper">
    <!-- 查詢博客 -->
    <select id="getAllUser" resultType="com.jiangfx.entity.User">
        select * from user
    </select>
</mapper>

下面是jsp

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="list">分頁查詢</a>
</body>
</html>

返回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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <center>
        <table width="200" border="1">
          <tr>
            <th scope="col">ID</th>
            <th scope="col">姓名</th>
            <th scope="col">性別</th>
            <th scope="col">城市</th>
          </tr>
          <c:forEach items="${pageInfo.list}" var="user">
          <tr>
            <td>${user.id}</td>
            <td>${user.username}</td>
            <td>${user.sex}</td>
            <td>${user.city}</td>
          </tr>
          </c:forEach>
        </table>
        <p>當前 ${pageInfo.pageNum }頁,總${pageInfo.pages }
                頁,總 ${pageInfo.total } 條記錄</div></p>
        <a href="list?pageNo=${pageInfo.firstPage}">第一頁</a>
        <c:if test="${pageInfo.hasPreviousPage }">
            <a href="list?pageNo=${pageInfo.pageNum-1}">上一頁</a>
        </c:if>
      
        <c:if test="${pageInfo.hasNextPage }">
            <a href="list?pageNo=${pageInfo.pageNum+1}">下一頁</a>
        </c:if>
        
        <a href="list?pageNo=${pageInfo.lastPage}">最后頁</a>
    </center>
  </body>
</html>

訪問localhost:8080/pagehelper


分頁查詢

直接點擊分頁查詢即可看到效果如下:


效果
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末大审,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子座哩,更是在濱河造成了極大的恐慌徒扶,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件八回,死亡現(xiàn)場離奇詭異酷愧,居然都是意外死亡,警方通過查閱死者的電腦和手機缠诅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進店門溶浴,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人管引,你說我怎么就攤上這事士败。” “怎么了褥伴?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵谅将,是天一觀的道長。 經(jīng)常有香客問我重慢,道長饥臂,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任似踱,我火速辦了婚禮隅熙,結(jié)果婚禮上稽煤,老公的妹妹穿的比我還像新娘。我一直安慰自己囚戚,他們只是感情好酵熙,可當我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著驰坊,像睡著了一般匾二。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上拳芙,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天察藐,我揣著相機與錄音,去河邊找鬼态鳖。 笑死转培,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的浆竭。 我是一名探鬼主播浸须,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼邦泄!你這毒婦竟也來了删窒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤顺囊,失蹤者是張志新(化名)和其女友劉穎肌索,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體特碳,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡诚亚,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了午乓。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片站宗。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖益愈,靈堂內(nèi)的尸體忽然破棺而出梢灭,到底是詐尸還是另有隱情,我是刑警寧澤蒸其,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布敏释,位于F島的核電站,受9級特大地震影響摸袁,放射性物質(zhì)發(fā)生泄漏钥顽。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一靠汁、第九天 我趴在偏房一處隱蔽的房頂上張望耳鸯。 院中可真熱鬧湿蛔,春花似錦膀曾、人聲如沸县爬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽财喳。三九已至,卻和暖如春斩狱,著一層夾襖步出監(jiān)牢的瞬間耳高,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工所踊, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留泌枪,地道東北人。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓秕岛,卻偏偏與公主長得像碌燕,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子继薛,可洞房花燭夜當晚...
    茶點故事閱讀 44,933評論 2 355

推薦閱讀更多精彩內(nèi)容