无聊做做看看,几乎没有怎么仔细做过这方面的,总是以为很简单,想想就会,实际做起来结合工作经验感觉还是挺有收获的,可以用在自己的项目上

第一版本思路:框架使用ssm,这个无所谓,采用分页语句查询指定页面的数据,根据数据记录总数,页面显示记录数计算有多少页,每次提交后计算当前页显示的上标和下标,然后进行分页查询,功能实现了,效果不满意,代码贴上,接口就不贴了,直接上接口的实现类,数据根据sql语句自己造吧

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) {
int currentPage = pageTableForm.getCurrentPage() == 0 ? 1 : pageTableForm.getCurrentPage();
pageTableForm.setCurrentPage(currentPage);
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) {
pageTableForm = getOperation(pageTableForm);
int beginIndex = pageTableForm.getBeginIndex();
//int endIndex = pageTableForm.getEndIndex();
userList = userInfoMapper.queryUserInfo(beginIndex);
pageTableForm.setUserList(userList);
return pageTableForm;
} public PageTableForm getOperation(PageTableForm pageTableForm) {
pageTableForm.setUserCount(getCount());
if (pageTableForm.getUserCount() % 3 == 0) {
pageTableForm.setPageCount(pageTableForm.getUserCount() / pageTableForm.getPageSize());
} else {
pageTableForm.setPageCount(pageTableForm.getUserCount() / pageTableForm.getPageSize() + 1);
} pageTableForm.setBeginIndex(pageTableForm.getCurrentPage() * 3 - 3);
// pageTableForm.setEndIndex(pageTableForm.getCurrentPage() * 3); 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.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository; import com.mi.entity.User; @Repository("userInfoMapper")
public interface UserInfoMapper { public List<User> queryUserInfo(@Param("beginIndex") int beginIndex); public int getCount();
}
<?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 limit #{beginIndex},3
</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>
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; ...省略get set
}
<%@ 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>
<script type="text/javascript"
src="<%=request.getContextPath()%>/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(function() {
$("#prev").click(
function() {
var currentPage = parseInt($("#currentPage").val()) - 1;
$("#currentPage").val(currentPage);
$("#form0").submit();
}) $("#next").click(
function() {
var currentPage = parseInt($("#currentPage").val()) + 1;
$("#currentPage").val(currentPage);
$("#form0").submit();
})
})
</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>
<td><c:if test="${pageTableForm.currentPage > 1}">
<input id="prev" type="button" value="上一页">
</c:if></td>
<td>当前<label>${pageTableForm.currentPage}</label>页/共<label>${pageTableForm.pageCount}</label>页</td>
<td><c:if test="${pageTableForm.currentPage < pageTableForm.pageCount}">
<input id="next" type="button" value="下一页">
</c:if></td>
</tr> </tbody>
</table>
<input id="currentPage" type="hidden" name="currentPage" value="${pageTableForm.currentPage}">
</form>
</body>
</html>

最终效果:

每次都会提交一次请求,比较差强人意,下个版本计划用js实现,拒绝垃圾代码

springmvc+spring+mybatis分页查询实例版本1,ver1.0的更多相关文章

  1. springmvc+spring+mybatis分页查询实例版本2.0

    先在改成纯利用js进行分页,首先查询出所有记录,初始化通过jquery控制只知显示首页内容,创建页面切换功能的函数,每次显示固定的内容行并把其他内容行隐藏,这样只需要一次提交就可以实现分页,但是仍有缺 ...

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

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

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

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

  4. Springmvc+Spring+Mybatis整合开发(架构搭建)

    Springmvc+Spring+Mybatis整合开发(架构搭建) 0.项目结构 Springmvc:web层 Spring:对象的容器 Mybatis:数据库持久化操作 1.导入所有需要的jar包 ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. IdTcpClient简单示例

    procedure TForm1.btnHttpGetClick(Sender: TObject); begin idtcpclnt1.Host := '192.168.10.88'; idtcpcl ...

  2. CentOS 7.0下面安装并配置Spark

    安装环境: 虚拟机:VMware® Workstation 8.0.1(网络桥接) OS:CentOS 7 JDK版本:jdk-7u79-linux-x64.tar Scala版本:scala-2.1 ...

  3. window平台安装MongoDB

    官网:www.mongodb.org 安装-->设置环境变量-->启动 1.下载: 根据系统下载 32 位或 64 位的 .msi 文件,下载后双击该文件,按提示安装即可, 2.设置安装目 ...

  4. Aptana Studio 3的汉化

    Aptana Studio 3(下面简称Aptana 3)的汉化方法 1.找到这个网站 http://aptana.com/support 2.单击下面的链接 view documentation 在 ...

  5. git还原成某个点

    1.本地 git  reset  --hard    commit值 git  push  -f   // 强制同步到git库(git服务器) 2.项目所在线上服务器 git  reset  --ha ...

  6. 面试&笔试常见题,你了解多少?

    HTML:1.  什么是语义化的HTML?有何意义?为什么要做到语义化?(高频率考题)2.  行内元素和块元素分别有哪些?(高频率)3.  严格模式与混杂模式的区分?如何触发这两种模式?(高频率)4. ...

  7. 号外号外:9月13号《Speed-BI云平台案例实操--十分钟做报表》开讲了

    引言:如何快速分析纷繁复杂的数据?如何快速做出老板满意的报表?如何快速将Speed-BI云平台运用到实际场景中?         本课程将通过各行各业案例背景,将Speed-BI云平台运用到实际场景中 ...

  8. Spring Boot 2 Swagger2

    本文将介绍RESTful API的重磅好伙伴Swagger2,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档. 它既可以减少我们创建文 ...

  9. 第七篇 SQL Server安全跨数据库所有权链接

    本篇文章是SQL Server安全系列的第七篇,详细内容请参考原文. Relational databases are used in an amazing variety of applicatio ...

  10. jquery-mockjax初试

    1. 原理 jquery-mockjax是用于mock 前台ajax向后台请求的返回数据. 原理很简单 在你js代码要发送ajax请求的地方断点一下,然后比较在[引入jquery-mockjax] 和 ...