当前位置:天才代写 > tutorial > JAVA 教程 > Spring MVC中实现分页

Spring MVC中实现分页

2017-11-02 08:00 星期四 所属: JAVA 教程 浏览:628

副标题#e#

Spring MVC中今朝较常用的分页实现步伐有两种:

1.每次翻页都修改SQL,向SQL传入相关参数去数据库及时查出该页的数据并显示。

2.查出数据库某张表的全部数据,再通过在业务逻辑内里举办处理惩罚去取得某些数据并显示。

对付数据量并不大的简朴的打点系统而言,第一种实现要领相对来说容易利用较少的代码实现分页这一成果,本文也正是为各人先容这种要领:

一、MyBatis数据表设置文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapper 
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> 
<mapper namespace="ec.help.dao.UserDao">
       
    <resultMap type="ec.help.bean.User" id="userResult" >
            <id column="id" javaType="string"  />
            <result column="username" javaType="string"  />
            <result column="password" javaType="string"  />    
    </resultMap>
        
    <sql id="userColumn"> id, username, password</sql>
        
    <select id="getUser" parameterType="map" resultType="ec.help.bean.User" >
        select  * from User where username=#{0} and password=#{1}
    </select>
        
    <select id="getAllUser"  parameterType="map" resultType="ec.help.bean.User" >
        select  * from User 
    </select>
        
    <!-- 分页利用SQL -->
    <select id="getUserByPage"  resultType="ec.help.bean.User" >
        select * from user limit #{0},#{1} 
    </select>
        
    <insert id="addUser" parameterType="ec.help.bean.User">
        insert into User(id,username,password) values(#{id},#{username},#{password})
    </insert>
        
       
    <delete id="deleteUser" parameterType="String">
        delete from User where id=#{id}
    </delete>
        
    <select id="showUser" parameterType="String" resultType="ec.help.bean.User" >
        select  * from User where id=#{id}
    </select>
        
    <update id="updateUser" parameterType="map">
        update User set username=#{0},password=#{1} where id=#{2}
    </update>
</mapper>

SQL中传入的第一个参数为开始的行数,第二个参数为数据条数。

二、Controller中逻辑实现:

@Value("#{configProperties['userPageSize']}")
    private String userPageSize; 
        
    @RequestMapping("/listUser.do")
    public ModelAndView listUser(String page,Model model){    
            
        //每页显示的条数
        int pageSize = Integer.parseInt(userPageSize);
            
        List<User> users = new ArrayList<User>();        
        users = this.userService.getAllUser();
            
        //查到的总用户数
        model.addAttribute("userNum", users.size());
    
        //总页数
        int pageTimes;
        if(users.size()%pageSize == 0)
        {
            pageTimes = users.size()/pageSize;
        }else
        {
            pageTimes = users.size()/pageSize + 1;
        }
        model.addAttribute("pageTimes", pageTimes);
            
        //页面初始的时候page没有值
        if(null == page)
        {
            page = "1";
        }
            
        //每页开始的第几笔记录            
        int startRow = (Integer.parseInt(page)-1) * pageSize;
        users = this.userService.getUserByPage(startRow, pageSize);
            
        model.addAttribute("currentPage", Integer.parseInt(page));
        model.addAttribute("users", users);
            
        return new ModelAndView("user/listUser"); 
    }


#p#副标题#e#

三、分页页面文件:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<div class="pagging">
        <div class="left">共${userNum}笔记录</div>
        <div class="right">
        <c:if test="${currentPage == 1}">
            <span class="disabled"><< 前一页</span>        
        </c:if>
        <c:if test="${currentPage != 1}">
            <a href="listUser.do?page=${currentPage-1}"><< 前一页</a>
        </c:if>
        <c:if test="${currentPage == 1}">
        <span class="current">1</span>
        </c:if>
        <c:if test="${currentPage != 1}">
        <a href="listUser.do?page=1">1</a>
        </c:if>
        <%
                int pageTimes = (Integer)session.getAttribute("pageTimes");
                for(int i=1;i<pageTimes;i++)
                {
                    request.setAttribute("page", i+1);
        %>            
        <c:if test="${currentPage == page}">
            <span class="current"><%=i+1%></span>       
        </c:if>
        <c:if test="${currentPage != page}">
             <a href="listUser.do?page=<%=i+1%>"><%=i+1%></a>
        </c:if>
        <%} %>
            
        <c:if test="${currentPage == pageTimes}">
            <span class="disabled">后一页 >></span>        
        </c:if>
        <c:if test="${currentPage != pageTimes}">
        <a href="listUser.do?page=${currentPage+1}">后一页 >></a>
        </c:if>
        </div>
</div>

四、实现结果:

Spring MVC中实现分页

 

    关键字:

天才代写-代写联系方式