2017/2/22 基于SpringMVC和Mybatis的分页实现(2)

本文介绍了一个基于Spring MVC框架的分页查询实现方案,包括控制器、服务层、MyBatis映射文件及前端页面的具体实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BaseController:

package com.chinook5.controller.base;


import javax.servlet.http.HttpServletRequest;

import com.chinook5.controller.converters.CustomDateConverter;
import com.chinook5.util.PageData;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

public class BaseController extends CustomDateConverter {
	


	private static final long serialVersionUID = 6357869213649815390L;
	
	/**
	 * 得到PageData
	 */
	public PageData getPageData(){
		return new PageData(this.getRequest());
	}
	
	/**
	 * 得到ModelAndView
	 */
	public ModelAndView getModelAndView(){
		return new ModelAndView();
	}
	
	/**
	 * 得到request对象
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}
}

普通controller需要继承basecontroller:

    @RequestMapping(value = "get_user")
    public @ResponseBody
    Map<String, Object> world(@RequestParam(value = "page", defaultValue = "1", required = true) Integer currentPage) throws Exception {
        Map<String,Object> map = new HashedMap();
        PageData pd = this.getPageData();
        Page p = new Page();
        p.setPd(pd);
        p.setCurrentPage(currentPage);

        List<PageData> data = userInfoService.getUserInfoListPage(p);
        Page.PageStr(p);
        map.put("pageData",data);
        map.put("pageInfo",p);
        return map;
    }

service:

    public List<PageData> getUserInfoListPage(Page p) throws Exception {
        return (List<PageData>)dao.findForList("UserInfoMapper.getUserInfoListPage",p);
    }

<?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="UserInfoMapper">
    <select id="getUserInfoListPage" parameterType="page" resultType="pd">
        SELECT * from userinfo
    </select>
</mapper>

user_list.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head>
    <title>Title</title>
    <script src="<%=basePath%>system/js/jquery-1.9.1/jquery.min.js" type="text/javascript"></script>
    <style>
        li{
            list-style-type: none;
            float: left;
            padding-left: 20px;
        }
        th{
            padding-left: 50px;
        }
        td{
            text-align: center;
        }
    </style>
</head>
<body>
<input id="currentPage" type="hidden" value="${pd.page}"/>
<table id="t1">
    <thead>
    <tr>
        <th>UserID</th>
        <th>UserName</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="tablelist">

    </tbody>
</table>
<div style="text-align:center;" id="pagination">

</div>
</body>
<script>
    $(function () {
        GetList();
    });
    function GetList() {
        var index = location.hash.indexOf("#!page=");
        var page = 1;
        if (index != -1) {
            page = location.hash.substring(index + 7);
        }
        if ($("#currentPage").val().length > 0) {
            page = $("#currentPage").val();
        }
        toPage(page);
        $("#search").click(function () {
            toPage(1);
            location.hash = "!page=1";
        });
    }
    function toPage(page) {
        $.ajax({
            url: '/user/get_user',
            type: 'post',
            data: {
                "page": page
            },
            async: false,
            dataType: 'json',
            success: function (data) {
                var divList = $('#tablelist');
                divList.empty();
                if (data.pageData.length > 0) {
                    //更新页码
                    location.hash = "!page=" + data.pageInfo.currentPage;
                    //更新列表显示的数据
                    $("#currentPage").val(data.pageInfo.currentPage);
                }
                $.each(data.pageData, function (i, item) {
                    var divContent = divContent + "<tr><td>" + item.UserID + "</td><td>" + item.UserName + "</td><td><a href='/user/" + item.UserID + "'>编辑</a></td></tr>";
                    divList.append(divContent);
                });
                //更新分页字符串
                $("#pagination").html(data.pageInfo.pageStr);
            }
        });

    }
</script>
</html>

效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值