先在改成纯利用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 ListuserList; @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 getUserList() { return userList; } public void setUserList(List 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 ListqueryUserInfo(); 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 ListuserList; 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 getUserList() { return userList; } public void setUserList(List userList) { this.userList = userList; }}
页面+js
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>Insert title here
最终效果:
总结一下,发现任何代码都极有可能重复,任何功能都极有可能一句话完成!