PageHelper插件實(shí)現(xiàn)服務(wù)器端分頁(yè)功能
本文實(shí)例為大家分享了PageHelper插件實(shí)現(xiàn)服務(wù)器端分頁(yè)功能,供大家參考,具體內(nèi)容如下
一、添加依賴
在.pom文件中添加
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.5.1</version> </dependency>
二、添加配置/strong>
在spring-mybaits.xml文件中添加插件
<!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 自動(dòng)掃描mapping.xml文件 --> <property name="mapperLocations" value="classpath:mapping/*.xml"></property> <!--分頁(yè)插件--> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=mysql reasonable=true </value> </property> </bean> </array> </property> <!--分頁(yè)--> </bean>
三、controller代碼
//分頁(yè)展示用戶 @RequestMapping(value = "/user.do") public ModelAndView test1(HttpServletRequest request, HttpServletResponse response, ModelMap mode) throws Exception { ModelAndView modelAndView = new ModelAndView(); int pageNumber = 1; try{ if(request.getParameter("pageNow").equals(null)){ System.out.println("參數(shù)空"); }else { pageNumber = Integer.parseInt(request.getParameter("pageNow")); } }catch (Exception e){ //處理空針錯(cuò)誤 } System.out.println("請(qǐng)求參數(shù)"+request.getParameter("pageNow")); PageInfo<user> pageInfo = userService.selectUserByPage(pageNumber,5); // Map<String, Object> map = new HashMap<String, Object>(); // List<Object> data = new ArrayList<Object>(); // for(user admin : pageInfo.getList()){ // Map<String, Object> obj = new HashMap<String, Object>(); // obj.put("id", admin.getId()); // obj.put("username", admin.getUsername()); // obj.put("password", admin.getPassword()); // obj.put("enable", admin.getEnable()); // obj.put("id_card", admin.getIdCard()); // obj.put("phone", admin.getPhone()); // obj.put("address", admin.getAddress()); // obj.put("nick_name", admin.getAddress()); // data.add(obj); // } modelAndView.addObject("pageInfo",pageInfo); //jsp 根據(jù)users來(lái)傳遞信息 System.out.println("成功啦"); modelAndView.setViewName("/test/user"); return modelAndView; }
四、Service方法
//分頁(yè)獲取用戶信息 public PageInfo<User> selectUserByPage(Integer pageNum, Integer pageSize) { PageHelper.startPage(pageNum, pageSize); // 所有用戶信息 List<User> authAdmins = userMapper.selectUsers(); if(authAdmins == null){ return null; } PageInfo<User> pageInfo = new PageInfo<User>(authAdmins); return pageInfo; }
五、jsp顯示
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: Administrator Date: 2017/12/2 0002 Time: 11:10 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>user</title> </head> <body> <h1>User</h1> <table> <c:forEach items="${pageInfo.list}" var="u"> <%--屬性值映射mapper 中的property--%> <tr> <td>${u.id}</td> <td>${u.username}</td> <td>${u.password}</td> <td>${u.enable}</td> <td>${u.idCard}</td> <td>${u.phone}</td> <td>${u.address}</td> <td>${u.nickName}</td> </tr> </c:forEach> <tr> <td colspan="6"> <!-- 分頁(yè)功能 start --> <div align="center"> <font size="2">共 ${pageInfo.pageSize} 頁(yè)</font> <font size="2">第 ${pageInfo.pageNum} 頁(yè)</font> <a href="${pageContext.request.contextPath}/user.do?pageNow=1" rel="external nofollow" rel="external nofollow" >首頁(yè)</a> <c:choose> <c:when test="${pageInfo.pageNum - 1 > 0}"> <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageNum - 1}" rel="external nofollow" >上一頁(yè)</a> </c:when> <c:when test="${pageInfo.pageNum - 1 <= 0}"> <a href="${pageContext.request.contextPath}/user.do?pageNow=1" rel="external nofollow" rel="external nofollow" >上一頁(yè)</a> </c:when> </c:choose> <%--pageSize共 頁(yè)--%> <%--pageNum 第 頁(yè)--%> <%--pagetotal 共 條--%> <c:choose> <c:when test="${pageInfo.pageSize==0}"> <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageNum}" rel="external nofollow" >下一頁(yè)</a> </c:when> <c:when test="${pageInfo.pageNum + 1 < pageInfo.pageSize}"> <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageNum + 1}" rel="external nofollow" >下一頁(yè)</a> </c:when> <c:when test="${pageInfo.pageNum + 1 >= pageInfo.pageSize}"> <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageSize}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁(yè)</a> </c:when> </c:choose> <c:choose> <c:when test="${pageInfo.pageSize==0}"> <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageSize}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁(yè)</a> </c:when> <c:otherwise> <a href="${pageContext.request.contextPath}/user.do?pageNow=${pageInfo.pageSize}" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁(yè)</a> </c:otherwise> </c:choose> </div> <!-- 分頁(yè)功能 End --> </td> </tr> <%--<tr>--%> <%--<td>${data.username}</td>--%> <%--<td>${data.password}</td>--%> <%--</tr>--%> </table> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Mybatis分頁(yè)插件PageHelper的使用詳解
- SpringMvc+Mybatis+Pagehelper分頁(yè)詳解
- mybatis分頁(yè)插件pageHelper詳解及簡(jiǎn)單實(shí)例
- Springboot整合pagehelper分頁(yè)功能
- Spring Boot+Mybatis+Druid+PageHelper實(shí)現(xiàn)多數(shù)據(jù)源并分頁(yè)的方法
- Mybatis分頁(yè)插件PageHelper的配置和簡(jiǎn)單使用方法(推薦)
- SpringBoot集成MyBatis的分頁(yè)插件PageHelper實(shí)例代碼
- PageHelper插件實(shí)現(xiàn)一對(duì)多查詢時(shí)的分頁(yè)問(wèn)題
- mybatis使用pageHelper插件進(jìn)行查詢分頁(yè)
- mybatis插件pageHelper實(shí)現(xiàn)分頁(yè)效果
相關(guān)文章
使用StringRedisTemplate操作Redis方法詳解
這篇文章主要為大家介紹了使用StringRedisTemplate操作Redis方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Java8深入學(xué)習(xí)系列(二)函數(shù)式編程
函數(shù)式編程,這個(gè)詞語(yǔ)由兩個(gè)名詞構(gòu)成,函數(shù),編程。編程這個(gè)詞我就不用解釋了,大家都是做這個(gè)的。函數(shù),其實(shí)單獨(dú)抽離出來(lái)這個(gè)詞語(yǔ),也并不陌生,那二者組合后的到底是什么呢,下面這篇文章主要給大家介紹了關(guān)于Java8函數(shù)式編程的相關(guān)資料,需要的朋友可以參考下。2017-08-08Java實(shí)現(xiàn)數(shù)獨(dú)小游戲
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)數(shù)獨(dú)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05Spring?Boot如何在加載bean時(shí)優(yōu)先選擇我
這篇文章主要介紹了Spring?Boot如何在加載bean時(shí)優(yōu)先選擇我,在?Spring?Boot?應(yīng)用程序中,我們可以采取三種方式實(shí)現(xiàn)自己的?bean?優(yōu)先加載,本文通過(guò)實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2023-03-03Java實(shí)現(xiàn)簡(jiǎn)單臺(tái)球游戲
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單臺(tái)球游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07深入了解Java中String、Char和Int之間的相互轉(zhuǎn)換
這篇文章主要介紹了深入了解Java中String、Char和Int之間的相互轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06基于SpringBoot和Vue3的博客平臺(tái)文章詳情與評(píng)論功能實(shí)現(xiàn)
在前面的教程中,我們已經(jīng)實(shí)現(xiàn)了基于Spring Boot和Vue3的發(fā)布、編輯、刪除文章功能以及文章列表與分頁(yè)功能。本教程將引導(dǎo)您實(shí)現(xiàn)博客平臺(tái)的文章詳情與評(píng)論功能,需要的朋友可以參考一下2023-04-04