一、iBatis进行分页查询
1、ibatis理解:
iBatis属于半自动化的ORM框架,我们需要编写SQL语句,由iBatis进行数据库访问,返回结果。而iBatis可以为我们做的更多,比如对查询参数集合、结果、分页查询、事务管理的封装等。虽然不如全自动SQL方便,但是SQL的主动权却在我们开发人员的手中,对SQL优化的掌控则是很直接的。
备注:iBatis的版本发展到3.0时,其名称也更改为MyBatis。而Spring更新到3.1都没有对MyBatis进行支持,但是MyBatis团队已经自行开发了Spring的支持。我们以Spring为主,仍然使用对iBatis2的支持来进行说明。
ibatis在线API教程可以参看 https://www.w3cschool.cn/ibatis/
2、jsp页面分页代码
- <div align="right">
- 当前第<label class="page" id="currentpage" ></label>页/<label class="page" id="allSize" ></label>页
- <label id="first" style="display: inline;">首页 前一页</label>
- <span id="first1" style="display: none;">
- <a style="display: inline" onclick="javascript:seachPage('first')" href="##" class="a1">首页</a>
- <a onclick="javascript:seachPage('previous')" href="##" class="a1">前一页</a>
- </span>
- <label id="last" style="display: inline">后一页 末页</label>
- <span id="last1" style="display: none;">
- <a onclick="javascript:seachPage('next')" href="##" class="a1">后一页</a>
- <a onclick="javascript:seachPage('last')" href="##" class="a1">末页</a>
- </span>
- <input id="currentpagevalue" type="hidden" value="0">
- </div>
3、JavaScript函数提交分页查询请求
- function seachPage(pageDirection) {
- var currentpagevalue = $("#currentpagevalue").val();var custname = $("#custname").val();
- initLoading();// 启动加载动画
- $.post("DataAction.do?action=findAudiList",{
- pageDirection : pageDirection,
- currentPage : currentpagevalue,
- custName : encodeURI(custname)
- },
- function(data) {
- var arr = data[0].auditList;
- if (arr.length > 0) {
- var html = "";
- for ( var i = 0; i < arr.length; i++) {// 更新列表
- var ReportBean = arr[i];
- html += "<tr><td><input type='radio' name='Reportid' value='" + ReportBean.Reportid + "'/> </td>";
- html += "<td align='center'>" + ReportBean.Reportid + " </td>";
- html += "<td align='center'>" + ReportBean.custName + " </td>";
- html += "<td align='center'>" + ReportBean.sbType + " </td>";
- html += "<td align='center'>" + ReportBean.jyType + " </td>";
- html += "<td align='center'>" + ReportBean.branchCode + " </td>";
- html += "<td align='center'>" + ReportBean.branchName + " </td>";
- html += "<td align='center'>" + ReportBean.exchStamp + " <input type='hidden' name='desc_reason' id='desc_reason' value='"+ ReportBean.exchStamp +"' /></td>";
- html += "<td align='center'>" + ReportBean.exchType + " </td>";
- html += "<td align='center'>" + ReportBean.timestamp + " </td>";
- html += "<td align='center'>" + ReportBean.checkDate + " </td>";
- html += "<td align='center'><input type='button' value='查看' onclick=SearchAudit('DataAuditAction.do?action=searchAuditDetail&insurCode="+ReportBean.Reportid+"')>" + " </td></tr>";
- }
- //
- var allsize = data[0].allSize;
- var currentpage = data[0].currentpage;
- cleartable(0);// 清空表格
- $("#tablelist").append(html);
- hideLoading();// 取消动画
- $("#allSize").append(data[0].allSize);
- $("#currentpage").append((parseInt(data[0].currentpage) + parseInt(1)));
- changePage(allsize, currentpage);// 更新翻页
- $("#currentpagevalue").val(currentpage);
- } else
- cleartable(0);
- hideLoading();// 取消动画
- }, "json");
- }
4.ibatis分页查询
Action层
- import org.biframework.common.Tools;
- import org.biframework.dao.ibatis.Paper;
- Paper paper = new Paper();
- String pageDirection = Tools.nulltostring((String) request.getParameter("pageDirection"));
- String currentPage = request.getParameter("currentPage");
- List auditList = dataAuditService.getAuditAllDataList(map, paper, currentPage, pageDirection, 10);
- map.put("orgLevels", userBean.getOrgLevels());
- map.put("auditList", auditList);
- map.put("currentpage", String.valueOf(paper.getCurrentpage()));
- map.put("allSize", String.valueOf(paper.getAllSize()));
- response.getWriter().write(JSONArray.fromObject(map).toString());
- response.getWriter().flush();
- response.getWriter().close();
dao层
- public class DataAuditDao extends BaseDao{
- protected static Log log = LogFactory.getLog(DataAuditDao.class);
- public List getAuditAllDataList(Map map,Paper paper,String currentPage,String pageDirection,int line) throws DaoException{
- List list = super.getList("getAllAuditDataList", map);
- return paper.getPaperList(list, pageDirection, currentPage,line);
- }
- }
BaseDao
- package org.biframework.dao.ibatis;
- import com.ibatis.common.util.PaginatedList;
- import java.io.PrintStream;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.biframework.exception.DaoException;
- import org.springframework.orm.ibatis.SqlMapClientTemplate;
- import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
- public class BaseDao
- extends SqlMapClientDaoSupport
- implements IBaseDao
- {
- private static Log log = LogFactory.getLog(BaseDao.class);
- protected static final int PAGE_SIZE = 15;
- public List getList(String statementName, Object parameterObject)
- throws DaoException
- {
- List list = getSqlMapClientTemplate().queryForList(statementName, parameterObject);
- return list;
- }
- public List getListUseSameStmt(String statementName, Object[] objectParam)
- throws DaoException
- {
- List list = null;
- List temp = null;
- if ((statementName == null) || (objectParam == null) || (objectParam.length == 0)) {
- return list;
- }
- for (int i = 0; i < objectParam.length; i++)
- {
- if (list == null) {
- list = new ArrayList();
- }
- temp = getSqlMapClientTemplate().queryForList(statementName, objectParam[i]);
- if (temp != null) {
- list.addAll(temp);
- }
- }
- return list;
- }
- public Object getObject(String statementName, Object parameterObject)
- throws DaoException
- {
- Object result = null;
- List list = getSqlMapClientTemplate().queryForList(statementName, parameterObject);
- if ((list != null) && (list.size() > 0)) {
- result = list.get(0);
- }
- return result;
- }
- public PaginatedList getPgntList(String statementName, Object parameterObject, String pageDirection)
- throws DaoException
- {
- PaginatedList list = getSqlMapClientTemplate().queryForPaginatedList(statementName, parameterObject, 15);
- if ("next".equals(pageDirection)) {
- list.nextPage();
- } else if ("previous".equals(pageDirection)) {
- list.previousPage();
- } else if ("first".equals(pageDirection)) {
- list.isFirstPage();
- } else if ("last".equals(pageDirection)) {
- list.isLastPage();
- }
- return list;
- }
- public PaginatedList getPgntList(String statementName, Object parameterObject, String pageDirection, int pageSize)
- throws DaoException
- {
- PaginatedList list = getSqlMapClientTemplate().queryForPaginatedList(statementName, parameterObject, pageSize);
- if ("next".equals(pageDirection))
- {
- System.out.println("下一页");
- list.nextPage();
- }
- else if ("previous".equals(pageDirection))
- {
- System.out.println("上一页");
- list.previousPage();
- }
- else if ("first".equals(pageDirection))
- {
- System.out.println("首页");
- list.isFirstPage();
- }
- else if ("last".equals(pageDirection))
- {
- System.out.println("末页");
- list.isLastPage();
- }
- return list;
- }
- public int transUpdate(Object[][] statementAndparameter)
- throws DaoException
- {
- Object[] statements = statementAndparameter[0];
- Object[] parameters = statementAndparameter[1];
- int result = 0;
- for (int i = 0; i < statements.length; i++)
- {
- String name = (String)statements[i];
- Object param = parameters[i];
- result += getSqlMapClientTemplate().update(name, param);
- }
- return result;
- }
- public int transUpdateSameOpt(String statementName, Object[] objectParam)
- throws DaoException
- {
- int result = 0;
- if ((statementName == null) || (objectParam == null) || (objectParam.length == 0)) {
- return result;
- }
- for (int i = 0; i < objectParam.length; i++) {
- result += getSqlMapClientTemplate().update(statementName, objectParam[i]);
- }
- return result;
- }
- public int update(String statementName, Object parameterObject)
- throws DaoException
- {
- int result = getSqlMapClientTemplate().update(statementName, parameterObject);
- return result;
- }
- }
接口IBaseDao
- package org.biframework.dao.ibatis;
- import com.ibatis.common.util.PaginatedList;
- import java.util.List;
- import org.biframework.exception.DaoException;
- public abstract interface IBaseDao
- {
- public abstract Object getObject(String paramString, Object paramObject)
- throws DaoException;
- public abstract List getList(String paramString, Object paramObject)
- throws DaoException;
- public abstract PaginatedList getPgntList(String paramString1, Object paramObject, String paramString2)
- throws DaoException;
- public abstract PaginatedList getPgntList(String paramString1, Object paramObject, String paramString2, int paramInt)
- throws DaoException;
- public abstract List getListUseSameStmt(String paramString, Object[] paramArrayOfObject)
- throws DaoException;
- public abstract int update(String paramString, Object paramObject)
- throws DaoException;
- public abstract int transUpdateSameOpt(String paramString, Object[] paramArrayOfObject)
- throws DaoException;
- public abstract int transUpdate(Object[][] paramArrayOfObject)
- throws DaoException;
- }
DaoException
- package org.biframework.exception;
- public class DaoException
- extends Exception
- {
- public DaoException() {}
- public DaoException(Throwable cause)
- {
- super(cause);
- }
- public DaoException(String message)
- {
- super(message);
- }
- public DaoException(String message, Throwable cause)
- {
- super(message, cause);
- }
- }
Paper
- package org.biframework.dao.ibatis;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class Paper
- {
- public Paper()
- {
- this.currentpage = 0;
- this.allSize = 0;
- }
- public void getCurrentPageNum(List allList, String pageDirection, String _currentpage)
- {
- if ("next".equals(pageDirection))
- {
- if ((Integer.parseInt(_currentpage) + 1) * 20 < allList.size()) {
- this.currentpage = (Integer.parseInt(_currentpage) + 1);
- } else {
- this.currentpage = Integer.parseInt(_currentpage);
- }
- }
- else if ("previous".equals(pageDirection))
- {
- if (Integer.parseInt(_currentpage) - 1 <= 0) {
- this.currentpage = 0;
- } else {
- this.currentpage = (Integer.parseInt(_currentpage) - 1);
- }
- }
- else if ("first".equals(pageDirection)) {
- this.currentpage = 0;
- } else if ("last".equals(pageDirection))
- {
- if (allList.size() == 0) {
- this.currentpage = 0;
- } else if (allList.size() % 20 == 0) {
- this.currentpage = (allList.size() / 20 - 1);
- } else {
- this.currentpage = (allList.size() / 20);
- }
- }
- else {
- this.currentpage = 0;
- }
- }
- public void getCurrentPageNum(List allList, String pageDirection, String _currentpage, int pageSize)
- {
- if ("next".equals(pageDirection))
- {
- if ((Integer.parseInt(_currentpage) + 1) * pageSize < allList.size()) {
- this.currentpage = (Integer.parseInt(_currentpage) + 1);
- } else {
- this.currentpage = Integer.parseInt(_currentpage);
- }
- }
- else if ("previous".equals(pageDirection))
- {
- if (Integer.parseInt(_currentpage) - 1 <= 0) {
- this.currentpage = 0;
- } else {
- this.currentpage = (Integer.parseInt(_currentpage) - 1);
- }
- }
- else if ("first".equals(pageDirection)) {
- this.currentpage = 0;
- } else if ("last".equals(pageDirection))
- {
- if (allList.size() == 0) {
- this.currentpage = 0;
- } else if (allList.size() % pageSize == 0) {
- this.currentpage = (allList.size() / pageSize - 1);
- } else {
- this.currentpage = (allList.size() / pageSize);
- }
- }
- else {
- this.currentpage = 0;
- }
- }
- public List getPaperList(List allList, String pageDirection, String _currentpage)
- {
- getCurrentPageNum(allList, pageDirection, _currentpage);
- List rsList = new ArrayList();
- for (int i = this.currentpage * 20; i < (this.currentpage + 1) * 20; i++)
- {
- if (i == allList.size()) {
- break;
- }
- rsList.add(allList.get(i));
- }
- if (allList.size() % 20 == 0) {
- this.allSize = (allList.size() / 20);
- } else {
- this.allSize = (allList.size() / 20 + 1);
- }
- if (allList.size() == 0) {
- this.allSize = 1;
- }
- return rsList;
- }
- public List getPaperList(List allList, String pageDirection, String _currentpage, int pageSize)
- {
- getCurrentPageNum(allList, pageDirection, _currentpage, pageSize);
- List rsList = new ArrayList();
- for (int i = this.currentpage * pageSize; i < (this.currentpage + 1) * pageSize; i++)
- {
- if (i == allList.size()) {
- break;
- }
- rsList.add(allList.get(i));
- }
- if (allList.size() % pageSize == 0) {
- this.allSize = (allList.size() / pageSize);
- } else {
- this.allSize = (allList.size() / pageSize + 1);
- }
- if (allList.size() == 0) {
- this.allSize = 1;
- }
- return rsList;
- }
- public int getCurrentpage()
- {
- return this.currentpage;
- }
- public void setCurrentpage(int currentpage)
- {
- this.currentpage = currentpage;
- }
- private static Log log = LogFactory.getLog(Paper.class);
- protected static final int PAGE_SIZE = 20;
- private int currentpage;
- private int allSize;
- public int getAllSize()
- {
- return this.allSize;
- }
- public void setAllSize(int allSize)
- {
- this.allSize = allSize;
- }
- }
sql:中无需使用分页查询
- <select id="getAllAuditDataList" resultMap="auditAllDataResult" parameterClass="map">
- <![CDATA[
- select distinct
- a.report_id,
- case when count(distinct b.cust_name)>1 then
- max(b.cust_name) || ' 等'
- else
- max(b.cust_name)
- end as cust_name,
- max(decode(a.tran_type,
- 'N',
- '正常',
- 'C',
- '改错')) as jy_type,
- max(a.exch_stamp || ' ' || a.action_desc) as desc,
- max(to_char(a.TIMESTAMP,'yyyy-MM-dd')) TIMESTAMP
- from report a,customer b,exch c
- group by a.report_id,b.cust_name
- order by check_date desc NULLS LAST, cust_name
- ]]>
- </select>
如果还有不太清楚的地方可以参看另一篇博文: spring集成ibatis进行项目中dao层基类封装 https://www.cnblogs.com/jiarui-zjb/p/9534810.html
写的不清楚的地方,欢迎留言指出!!!
一、iBatis进行分页查询的更多相关文章
- ibatis实现分页查询
最近在做老项目改造,分享一个之前写的ibatis(这里特指ibatis2.x的版本)分页插件. 大致原理就是通过重写SqlExecutor的executeQuery方法,实现分页查询,支持mysql和 ...
- Ibatis的分页机制的缺陷
我们知道,Ibatis为我们提供了可以直接实现分页的方法 queryForList(String statementName, Object parameterObject, int skipResu ...
- IBatis按条件分页查询
XML中代码 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMap PUBLIC & ...
- Ibatis调用存储过程实现增删改以及分页查询
1.Ibatis实现增删改操作很简单了,通常我是将某一模块的增删改功能写在一个存储过程里,通过一个标识符去区分执行增加还是修改抑或删除操作. statement: <!-- 存储过程:实现学生的 ...
- springmvc+spring+mybatis分页查询实例版本1,ver1.0
无聊做做看看,几乎没有怎么仔细做过这方面的,总是以为很简单,想想就会,实际做起来结合工作经验感觉还是挺有收获的,可以用在自己的项目上 第一版本思路:框架使用ssm,这个无所谓,采用分页语句查询指定页面 ...
- MyBatis简单的增删改查以及简单的分页查询实现
MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...
- Mybatis包分页查询java公共类
Mybatis包分页查询java公共类 分页----对于数据量非常大的查询中.是不可缺少的. mybatis底层的分页sql语句因为须要我们自己去手动写.而实现分页显示的时候我们须要依据分页查询条 ...
- mysql分库 分页查询
Mysql海量数据分表分库如何列表分页? 1.现在使用ElasticSearch了.基于Lucene的解决方案 2.必须将mysql里的数据写入到类似hbase这样的分布式数据库,查询快.但分页.查询 ...
- mybatis 框架 的应用之二(批量添加、实现分页查询)
lf-driver=com.mysql.jdbc.Driver lf-url=jdbc:mysql://localhost:3306/test lf-user=LF lf-password=LF &l ...
随机推荐
- vCPU 和 CPU 的关系
vCPU 和 pCPU 的关系不是数量,当被底层虚拟化之后,任何一个 vCPU 都是用到所有的 pCPU 核心总体的百分比,不是某一个核心这么去看的,并没有对应的关系,也不是一个很绝对的分配到具体某个 ...
- mysql 单表批量备份sh文件
#!/bin/bashDBS=$(mysql -u root -padmin -e 'use database; show tables;' | awk '{ print $1 }');for tab ...
- python opencv:摄像头捕获图像
- python2.7环境下升级pip3,及出错解决办法
执行 pip3 install --upgrade pip 进行升级 升级后若出现, Import Error:cannot import name main 是因为将pip更新为10.0.0后库里面 ...
- 杭电 1772 cake
Cake Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- LeetCode 234. Palindrome Linked List(判断是否为回文链表)
题意:判断是否为回文链表,要求时间复杂度O(n),空间复杂度O(1). 分析: (1)利用快慢指针找到链表的中心 (2)进行步骤(1)的过程中,对前半部分链表进行反转 (3)如果链表长是偶数,首先比较 ...
- Educational Codeforces Round 77 (Rated for Div. 2) - D. A Game with Traps(二分)
题意:$m$个士兵,每个士兵都有一个灵敏度$a[i]$,起点为$0$,终点为$n + 1$,在路上有$k$个陷阱,每个陷阱有三个属性$l[i],r[i],d[i]$,$l[i]$表示陷阱的位置,如果你 ...
- CSS三列自适应布局(两边宽度固定,中间自适应)
https://blog.csdn.net/cinderella_hou/article/details/52156333 https://blog.csdn.net/wangchengiii/art ...
- 「SDOI2005」区间
「SDOI2005」区间 传送门 记录每一个位置作为左端点和右端点的出现次数,然后直接考虑差分即可. 参考代码: #include <cstdio> #define rg register ...
- nginx 重写 隐藏index.php
修改 nginx.conf 文件location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break ...