先在改成纯利用js进行分页,首先查询出所有记录,初始化通过jquery控制只知显示首页内容,创建页面切换功能的函数,每次显示固定的内容行并把其他内容行隐藏,这样只需要一次提交就可以实现分页,但是仍有缺点,就是如果数据量很多很多,会严重影响性能:-------合理的想法是每次先取出一部分的内容,比如先去除前30行的内容,等浏览到第31行时再进行一数据库的检索,先看看这个版本的

package com.mi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import com.mi.form.PageTableForm;
import com.mi.service.impl.UserInfoServiceImpl; @Controller
@RequestMapping("/user")
public class UserInfoCotroller { @Autowired
private UserInfoServiceImpl userInfoServiceImpl; private PageTableForm pageTableForm; @RequestMapping("/init")
public String init(Model model) {
pageTableForm = new PageTableForm();
return "redirect:/user/query.do";
} @RequestMapping("/query")
public String queryUserInfo(Model model, PageTableForm pageTableForm) {
pageTableForm = userInfoServiceImpl.queryUserInfo(pageTableForm);
model.addAttribute("pageTableForm", pageTableForm);
return "userInfo";
} public UserInfoServiceImpl getUserInfoServiceImpl() {
return userInfoServiceImpl;
} public void setUserInfoServiceImpl(UserInfoServiceImpl userInfoServiceImpl) {
this.userInfoServiceImpl = userInfoServiceImpl;
} public PageTableForm getPageTableForm() {
return pageTableForm;
} public void setPageTableForm(PageTableForm pageTableForm) {
this.pageTableForm = pageTableForm;
} }
package com.mi.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.mi.dao.UserInfoMapper;
import com.mi.entity.User;
import com.mi.form.PageTableForm;
import com.mi.service.UserInfoService; @Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService { @Autowired
private UserInfoMapper userInfoMapper; private List<User> userList;
@Override
public PageTableForm queryUserInfo(PageTableForm pageTableForm) {
userList = userInfoMapper.queryUserInfo();
pageTableForm.setUserList(userList);
return pageTableForm;
} public UserInfoMapper getUserInfoMapper() {
return userInfoMapper;
}
public void setUserInfoMapper(UserInfoMapper userInfoMapper) {
this.userInfoMapper = userInfoMapper;
} @Override
public int getCount() {
return userInfoMapper.getCount();
} public List<User> getUserList() {
return userList;
} public void setUserList(List<User> userList) {
this.userList = userList;
} }
package com.mi.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.mi.entity.User;

@Repository("userInfoMapper")
public interface UserInfoMapper { public List<User> queryUserInfo(); public int getCount();
}
package com.mi.form;

import java.util.List;

import com.mi.entity.User;

public class PageTableForm {

    private int currentPage;// 当前页
private int pageSize = 3;// 每页记录数
private int beginIndex;// 开始位置
private int endIndex;// 结束位置
private int pageCount;// 共多少页
private int userCount;// 共多少条记录 private List<User> userList; public int getCurrentPage() {
return currentPage;
} public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
} public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} public int getBeginIndex() {
return beginIndex;
} public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
} public int getEndIndex() {
return endIndex;
} public void setEndIndex(int endIndex) {
this.endIndex = endIndex;
} public int getPageCount() {
return pageCount;
} public void setPageCount(int pageCount) {
this.pageCount = pageCount;
} public int getUserCount() {
return userCount;
} public void setUserCount(int userCount) {
this.userCount = userCount;
} public List<User> getUserList() {
return userList;
} public void setUserList(List<User> userList) {
this.userList = userList;
} }
<?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.mi.dao.UserInfoMapper"> <select id="queryUserInfo" resultType="com.mi.entity.User">
SELECT id,user_name userName,age FROM user_t where 1=1
</select> <select id="getCount" resultType="int">
SELECT count(*) FROM user_t
</select>
<!-- <insert id="addUser" parameterType="com.mi.entity.User"
flushCache="true">
INSERT INTO user_t (id,user_name,password,age) VALUES
(#{id},#{userName},#{password},#{age})
</insert> <delete id="deleteUser" parameterType="com.mi.entity.User" flushCache="true">
DELETE FROM user_t where id=#{id}
</delete> <update id="updateUser" parameterType="com.mi.entity.User" flushCache="true">
UPDATE user_t SET user_name = 'zzxy' WHERE id=#{id}
</update> --> </mapper>

页面+js

<%@ 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>
<style type="text/css">
.page{
width:200px;
}
.page span{
margin-left:30px;
}
a{
text-decoration:none;
}
</style>
<script type="text/javascript"
src="<%=request.getContextPath()%>/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function() {
var $table = $("table");
var currentPage = 1;
var pageSize = 3;
var sumRows = $table.find("tbody tr").length;
var sumPages = Math.ceil(sumRows/pageSize); init();
paging(currentPage) $("#prev").click(function(){
currentPage--;
init();
paging(currentPage);
}) $("#next").click(function(){
currentPage++;
init();
paging(currentPage);
}) var $page = $("<div class='page'></div>");
for(var pageIndex=1;pageIndex<=sumPages;pageIndex++){
$("<a href='#'><span>["+(pageIndex)+"]</span></a>").bind("click",{"newPage":pageIndex},function(event){
currentPage=event.data["newPage"];//值得参考
init();
paging(currentPage);
}).appendTo($page);
}
$page.insertAfter($table); function paging(currentPage){
$table.find("tbody tr:not(.prevnext)").hide().slice((currentPage-1)*pageSize,(currentPage)*pageSize).show();
$("#currentPage").val(currentPage+1);
$("#currentPage").text(currentPage);
$("#sumPages").text(sumPages);
} function init(){
if(currentPage==1){
$("#prev").attr({"disabled":"disabled"});
}else{
$("#prev").removeAttr("disabled");
}
if(currentPage==sumPages){
$("#next").attr({"disabled":"disabled"});
}else{
$("#next").removeAttr("disabled");
}
} })
</script>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/query.do" id="form0" method="POST">
<table border="1">
<thead>
<tr>
<td width="60px">id</td>
<td width="120px">name</td>
<td width="60px">age</td>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${pageTableForm.userList}" varStatus="status">
<tr>
<td>${user.id}</td>
<td>${user.userName}</td>
<td>${user.age}</td>
</tr>
</c:forEach>
<tr class="prevnext">
<td>
<input id="prev" type="button" value="上一页">
</td>
<td>当前<label id="currentPage"></label>页/共<label id="sumPages"></label>页</td>
<td>
<input id="next" type="button" value="下一页">
</td>
</tr> </tbody>
</table>
<input id="currentPage" type="hidden" name="currentPage" value="${pageTableForm.currentPage}">
</form>
</body>
</html>

最终效果:

总结一下,发现任何代码都极有可能重复,任何功能都极有可能一句话完成!

springmvc+spring+mybatis分页查询实例版本2.0的更多相关文章

  1. springmvc+spring+mybatis分页查询实例版本1,ver1.0

    无聊做做看看,几乎没有怎么仔细做过这方面的,总是以为很简单,想想就会,实际做起来结合工作经验感觉还是挺有收获的,可以用在自己的项目上 第一版本思路:框架使用ssm,这个无所谓,采用分页语句查询指定页面 ...

  2. springmvc+spring+mybatis分页查询实例版本3,添加条件检索

    在第二个版本上添加了姓名模糊查询,年龄区间查询;自以为easy,结果发现mybatis的各种参数写法基本搞混或是忘了,zuo啊,直接上代码,然后赶紧把mybatis整理一遍再研究自己的项目,应该还会有 ...

  3. 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)

    前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...

  4. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第二天】

    淘淘商城(SpringMVC+Spring+Mybatis)  是传智播客在2015年9月份录制的,几年过去了.由于视频里课上老师敲的代码和项目笔记有些细节上存在出入,只有根据日志报错信息作出适当的调 ...

  5. springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来

    自己从事java开发工作也有一年多了,自己却没有亲手搭建一个完整的框架.于是今天自己动手搭建一个,过程中遇到一些问题,倒腾了大半天终于搞定了. 现在给大家分享一下过程,自己也记录下来,以后学习参考使用 ...

  6. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第八天】(solr服务器搭建、搜索功能实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  7. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第六天】

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  8. Idea SpringMVC+Spring+MyBatis+Maven调整【转】

    Idea SpringMVC+Spring+MyBatis+Maven整合   创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...

  9. SpringMVC+Spring+MyBatis+Maven调整【转】

    Idea SpringMVC+Spring+MyBatis+Maven整合   创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...

随机推荐

  1. net use

    net use * \\ipaddr\share "password" /user:"username" /persistent:yes 在运行中输入“\\”+ ...

  2. new char[]和new char()的区别

    new char[1];分配一块连续的内存,也就是一个数组,里面有1个元素new   char(1);分配一块内存,有一个元素,该元素被初始化为1

  3. Magento添加一个下拉登陆菜单Create Magento Dropdown Login in a few minutes

    Dropdown login forms are not a feature many online stores use, but in some cases they could be quite ...

  4. The include feature of SQL Server Index

    1. Why we need the index 'include' feature? For SQLServer , the length of all the index key have a l ...

  5. JavaScript:画廊案例

    经常会在网上看到这样的操作:有几个按钮.可以控制器图片的上一张.下一张.那么现在就使用按钮的click单击事件来切换img的图片. 此时由于只是简单的演示,将所有的图片保存在images目录中.所有的 ...

  6. 30天,O2O速成攻略【8.30南京站】

    活动概况 时间:2015年8月30日13:30-16:30 地点:啡咖啡·孵化器(南京市玄武大道699-22号江苏软件园22栋) 主办:APICloud.Udesk.人为峰 网址:www.apiclo ...

  7. 端口转发后执行putty连接------------------》VirtualBox+ubuntu_server

    login as: fleam fleam@127.0.0.1's password: Welcome to Ubuntu LTS (GNU/Linux --generic i686) * Docum ...

  8. windows下制作PHP扩展

    一.编译PHP 转自:http://demon.tw/software/compile-php-on-windows.html 编译PHP扩展必需的一些头文件需要从php源码中获取,其中有一些配置性的 ...

  9. [BS-25] IOS中手势UIGestureRecognizer概述

    IOS中手势UIGestureRecognizer概述 一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touches ...

  10. C# WebSocket 服务端示例代码 + HTML5客户端示例代码

    WebSocket服务端 C#示例代码 using System; using System.Collections.Generic; using System.Linq; using System. ...