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页面分页代码

  1. <div align="right">
  2. 当前第<label class="page" id="currentpage" ></label>页/<label class="page" id="allSize" ></label>
  3.  
  4. <label id="first" style="display: inline;">首页 前一页</label>
  5. <span id="first1" style="display: none;">
  6. <a style="display: inline" onclick="javascript:seachPage('first')" href="##" class="a1">首页</a>
  7. <a onclick="javascript:seachPage('previous')" href="##" class="a1">前一页</a>
  8. </span>
  9.  
  10. <label id="last" style="display: inline">后一页 末页</label>
  11. <span id="last1" style="display: none;">
  12. <a onclick="javascript:seachPage('next')" href="##" class="a1">后一页</a>
  13. <a onclick="javascript:seachPage('last')" href="##" class="a1">末页</a>
  14. </span>
  15. <input id="currentpagevalue" type="hidden" value="0">
  16. </div>

3、JavaScript函数提交分页查询请求

  1. function seachPage(pageDirection) {
  2. var currentpagevalue = $("#currentpagevalue").val();var custname = $("#custname").val();
  3. initLoading();// 启动加载动画
  4. $.post("DataAction.do?action=findAudiList",{
  5. pageDirection : pageDirection,
  6. currentPage : currentpagevalue,
  7. custName : encodeURI(custname)
  8. },
  9. function(data) {
  10. var arr = data[0].auditList;
  11. if (arr.length > 0) {
  12. var html = "";
  13. for ( var i = 0; i < arr.length; i++) {// 更新列表
  14. var ReportBean = arr[i];
  15. html += "<tr><td><input type='radio' name='Reportid' value='" + ReportBean.Reportid + "'/> </td>";
  16. html += "<td align='center'>" + ReportBean.Reportid + "&nbsp;</td>";
  17. html += "<td align='center'>" + ReportBean.custName + "&nbsp;</td>";
  18. html += "<td align='center'>" + ReportBean.sbType + "&nbsp;</td>";
  19. html += "<td align='center'>" + ReportBean.jyType + "&nbsp;</td>";
  20. html += "<td align='center'>" + ReportBean.branchCode + "&nbsp;</td>";
  21. html += "<td align='center'>" + ReportBean.branchName + "&nbsp;</td>";
  22. html += "<td align='center'>" + ReportBean.exchStamp + "&nbsp;<input type='hidden' name='desc_reason' id='desc_reason' value='"+ ReportBean.exchStamp +"' /></td>";
  23. html += "<td align='center'>" + ReportBean.exchType + "&nbsp;</td>";
  24. html += "<td align='center'>" + ReportBean.timestamp + "&nbsp;</td>";
  25. html += "<td align='center'>" + ReportBean.checkDate + "&nbsp;</td>";
  26. html += "<td align='center'><input type='button' value='查看' onclick=SearchAudit('DataAuditAction.do?action=searchAuditDetail&insurCode="+ReportBean.Reportid+"')>" + "&nbsp;</td></tr>";
  27. }
  28. //
  29. var allsize = data[0].allSize;
  30. var currentpage = data[0].currentpage;
  31. cleartable(0);// 清空表格
  32. $("#tablelist").append(html);
  33. hideLoading();// 取消动画
  34. $("#allSize").append(data[0].allSize);
  35. $("#currentpage").append((parseInt(data[0].currentpage) + parseInt(1)));
  36. changePage(allsize, currentpage);// 更新翻页
  37. $("#currentpagevalue").val(currentpage);
  38. } else
  39. cleartable(0);
  40. hideLoading();// 取消动画
  41.  
  42. }, "json");
  43. }

4.ibatis分页查询

Action层

  1. import org.biframework.common.Tools;
  2. import org.biframework.dao.ibatis.Paper;
  3. Paper paper = new Paper();
  4. String pageDirection = Tools.nulltostring((String) request.getParameter("pageDirection"));
  5. String currentPage = request.getParameter("currentPage");
  6. List auditList = dataAuditService.getAuditAllDataList(map, paper, currentPage, pageDirection, 10);
  7. map.put("orgLevels", userBean.getOrgLevels());
  8.  
  9. map.put("auditList", auditList);
  10. map.put("currentpage", String.valueOf(paper.getCurrentpage()));
  11. map.put("allSize", String.valueOf(paper.getAllSize()));
  12. response.getWriter().write(JSONArray.fromObject(map).toString());
  13. response.getWriter().flush();
  14. response.getWriter().close();

dao层

  1. public class DataAuditDao extends BaseDao{
  2. protected static Log log = LogFactory.getLog(DataAuditDao.class);
  3. public List getAuditAllDataList(Map map,Paper paper,String currentPage,String pageDirection,int line) throws DaoException{
  4. List list = super.getList("getAllAuditDataList", map);
  5. return paper.getPaperList(list, pageDirection, currentPage,line);
  6. }
  7. }

BaseDao

  1. package org.biframework.dao.ibatis;
  2.  
  3. import com.ibatis.common.util.PaginatedList;
  4. import java.io.PrintStream;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import org.biframework.exception.DaoException;
  10. import org.springframework.orm.ibatis.SqlMapClientTemplate;
  11. import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
  12.  
  13. public class BaseDao
  14. extends SqlMapClientDaoSupport
  15. implements IBaseDao
  16. {
  17. private static Log log = LogFactory.getLog(BaseDao.class);
  18. protected static final int PAGE_SIZE = 15;
  19.  
  20. public List getList(String statementName, Object parameterObject)
  21. throws DaoException
  22. {
  23. List list = getSqlMapClientTemplate().queryForList(statementName, parameterObject);
  24. return list;
  25. }
  26.  
  27. public List getListUseSameStmt(String statementName, Object[] objectParam)
  28. throws DaoException
  29. {
  30. List list = null;
  31. List temp = null;
  32. if ((statementName == null) || (objectParam == null) || (objectParam.length == 0)) {
  33. return list;
  34. }
  35. for (int i = 0; i < objectParam.length; i++)
  36. {
  37. if (list == null) {
  38. list = new ArrayList();
  39. }
  40. temp = getSqlMapClientTemplate().queryForList(statementName, objectParam[i]);
  41. if (temp != null) {
  42. list.addAll(temp);
  43. }
  44. }
  45. return list;
  46. }
  47.  
  48. public Object getObject(String statementName, Object parameterObject)
  49. throws DaoException
  50. {
  51. Object result = null;
  52.  
  53. List list = getSqlMapClientTemplate().queryForList(statementName, parameterObject);
  54. if ((list != null) && (list.size() > 0)) {
  55. result = list.get(0);
  56. }
  57. return result;
  58. }
  59.  
  60. public PaginatedList getPgntList(String statementName, Object parameterObject, String pageDirection)
  61. throws DaoException
  62. {
  63. PaginatedList list = getSqlMapClientTemplate().queryForPaginatedList(statementName, parameterObject, 15);
  64. if ("next".equals(pageDirection)) {
  65. list.nextPage();
  66. } else if ("previous".equals(pageDirection)) {
  67. list.previousPage();
  68. } else if ("first".equals(pageDirection)) {
  69. list.isFirstPage();
  70. } else if ("last".equals(pageDirection)) {
  71. list.isLastPage();
  72. }
  73. return list;
  74. }
  75.  
  76. public PaginatedList getPgntList(String statementName, Object parameterObject, String pageDirection, int pageSize)
  77. throws DaoException
  78. {
  79. PaginatedList list = getSqlMapClientTemplate().queryForPaginatedList(statementName, parameterObject, pageSize);
  80. if ("next".equals(pageDirection))
  81. {
  82. System.out.println("下一页");
  83. list.nextPage();
  84. }
  85. else if ("previous".equals(pageDirection))
  86. {
  87. System.out.println("上一页");
  88. list.previousPage();
  89. }
  90. else if ("first".equals(pageDirection))
  91. {
  92. System.out.println("首页");
  93. list.isFirstPage();
  94. }
  95. else if ("last".equals(pageDirection))
  96. {
  97. System.out.println("末页");
  98. list.isLastPage();
  99. }
  100. return list;
  101. }
  102.  
  103. public int transUpdate(Object[][] statementAndparameter)
  104. throws DaoException
  105. {
  106. Object[] statements = statementAndparameter[0];
  107. Object[] parameters = statementAndparameter[1];
  108. int result = 0;
  109. for (int i = 0; i < statements.length; i++)
  110. {
  111. String name = (String)statements[i];
  112. Object param = parameters[i];
  113. result += getSqlMapClientTemplate().update(name, param);
  114. }
  115. return result;
  116. }
  117.  
  118. public int transUpdateSameOpt(String statementName, Object[] objectParam)
  119. throws DaoException
  120. {
  121. int result = 0;
  122. if ((statementName == null) || (objectParam == null) || (objectParam.length == 0)) {
  123. return result;
  124. }
  125. for (int i = 0; i < objectParam.length; i++) {
  126. result += getSqlMapClientTemplate().update(statementName, objectParam[i]);
  127. }
  128. return result;
  129. }
  130.  
  131. public int update(String statementName, Object parameterObject)
  132. throws DaoException
  133. {
  134. int result = getSqlMapClientTemplate().update(statementName, parameterObject);
  135. return result;
  136. }
  137. }

接口IBaseDao

  1. package org.biframework.dao.ibatis;
  2.  
  3. import com.ibatis.common.util.PaginatedList;
  4. import java.util.List;
  5. import org.biframework.exception.DaoException;
  6.  
  7. public abstract interface IBaseDao
  8. {
  9. public abstract Object getObject(String paramString, Object paramObject)
  10. throws DaoException;
  11.  
  12. public abstract List getList(String paramString, Object paramObject)
  13. throws DaoException;
  14.  
  15. public abstract PaginatedList getPgntList(String paramString1, Object paramObject, String paramString2)
  16. throws DaoException;
  17.  
  18. public abstract PaginatedList getPgntList(String paramString1, Object paramObject, String paramString2, int paramInt)
  19. throws DaoException;
  20.  
  21. public abstract List getListUseSameStmt(String paramString, Object[] paramArrayOfObject)
  22. throws DaoException;
  23.  
  24. public abstract int update(String paramString, Object paramObject)
  25. throws DaoException;
  26.  
  27. public abstract int transUpdateSameOpt(String paramString, Object[] paramArrayOfObject)
  28. throws DaoException;
  29.  
  30. public abstract int transUpdate(Object[][] paramArrayOfObject)
  31. throws DaoException;
  32. }

DaoException

  1. package org.biframework.exception;
  2.  
  3. public class DaoException
  4. extends Exception
  5. {
  6. public DaoException() {}
  7.  
  8. public DaoException(Throwable cause)
  9. {
  10. super(cause);
  11. }
  12.  
  13. public DaoException(String message)
  14. {
  15. super(message);
  16. }
  17.  
  18. public DaoException(String message, Throwable cause)
  19. {
  20. super(message, cause);
  21. }
  22. }

Paper

  1. package org.biframework.dao.ibatis;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.apache.commons.logging.Log;
  6. import org.apache.commons.logging.LogFactory;
  7.  
  8. public class Paper
  9. {
  10. public Paper()
  11. {
  12. this.currentpage = 0;
  13. this.allSize = 0;
  14. }
  15.  
  16. public void getCurrentPageNum(List allList, String pageDirection, String _currentpage)
  17. {
  18. if ("next".equals(pageDirection))
  19. {
  20. if ((Integer.parseInt(_currentpage) + 1) * 20 < allList.size()) {
  21. this.currentpage = (Integer.parseInt(_currentpage) + 1);
  22. } else {
  23. this.currentpage = Integer.parseInt(_currentpage);
  24. }
  25. }
  26. else if ("previous".equals(pageDirection))
  27. {
  28. if (Integer.parseInt(_currentpage) - 1 <= 0) {
  29. this.currentpage = 0;
  30. } else {
  31. this.currentpage = (Integer.parseInt(_currentpage) - 1);
  32. }
  33. }
  34. else if ("first".equals(pageDirection)) {
  35. this.currentpage = 0;
  36. } else if ("last".equals(pageDirection))
  37. {
  38. if (allList.size() == 0) {
  39. this.currentpage = 0;
  40. } else if (allList.size() % 20 == 0) {
  41. this.currentpage = (allList.size() / 20 - 1);
  42. } else {
  43. this.currentpage = (allList.size() / 20);
  44. }
  45. }
  46. else {
  47. this.currentpage = 0;
  48. }
  49. }
  50.  
  51. public void getCurrentPageNum(List allList, String pageDirection, String _currentpage, int pageSize)
  52. {
  53. if ("next".equals(pageDirection))
  54. {
  55. if ((Integer.parseInt(_currentpage) + 1) * pageSize < allList.size()) {
  56. this.currentpage = (Integer.parseInt(_currentpage) + 1);
  57. } else {
  58. this.currentpage = Integer.parseInt(_currentpage);
  59. }
  60. }
  61. else if ("previous".equals(pageDirection))
  62. {
  63. if (Integer.parseInt(_currentpage) - 1 <= 0) {
  64. this.currentpage = 0;
  65. } else {
  66. this.currentpage = (Integer.parseInt(_currentpage) - 1);
  67. }
  68. }
  69. else if ("first".equals(pageDirection)) {
  70. this.currentpage = 0;
  71. } else if ("last".equals(pageDirection))
  72. {
  73. if (allList.size() == 0) {
  74. this.currentpage = 0;
  75. } else if (allList.size() % pageSize == 0) {
  76. this.currentpage = (allList.size() / pageSize - 1);
  77. } else {
  78. this.currentpage = (allList.size() / pageSize);
  79. }
  80. }
  81. else {
  82. this.currentpage = 0;
  83. }
  84. }
  85.  
  86. public List getPaperList(List allList, String pageDirection, String _currentpage)
  87. {
  88. getCurrentPageNum(allList, pageDirection, _currentpage);
  89. List rsList = new ArrayList();
  90. for (int i = this.currentpage * 20; i < (this.currentpage + 1) * 20; i++)
  91. {
  92. if (i == allList.size()) {
  93. break;
  94. }
  95. rsList.add(allList.get(i));
  96. }
  97. if (allList.size() % 20 == 0) {
  98. this.allSize = (allList.size() / 20);
  99. } else {
  100. this.allSize = (allList.size() / 20 + 1);
  101. }
  102. if (allList.size() == 0) {
  103. this.allSize = 1;
  104. }
  105. return rsList;
  106. }
  107.  
  108. public List getPaperList(List allList, String pageDirection, String _currentpage, int pageSize)
  109. {
  110. getCurrentPageNum(allList, pageDirection, _currentpage, pageSize);
  111. List rsList = new ArrayList();
  112. for (int i = this.currentpage * pageSize; i < (this.currentpage + 1) * pageSize; i++)
  113. {
  114. if (i == allList.size()) {
  115. break;
  116. }
  117. rsList.add(allList.get(i));
  118. }
  119. if (allList.size() % pageSize == 0) {
  120. this.allSize = (allList.size() / pageSize);
  121. } else {
  122. this.allSize = (allList.size() / pageSize + 1);
  123. }
  124. if (allList.size() == 0) {
  125. this.allSize = 1;
  126. }
  127. return rsList;
  128. }
  129.  
  130. public int getCurrentpage()
  131. {
  132. return this.currentpage;
  133. }
  134.  
  135. public void setCurrentpage(int currentpage)
  136. {
  137. this.currentpage = currentpage;
  138. }
  139.  
  140. private static Log log = LogFactory.getLog(Paper.class);
  141. protected static final int PAGE_SIZE = 20;
  142. private int currentpage;
  143. private int allSize;
  144.  
  145. public int getAllSize()
  146. {
  147. return this.allSize;
  148. }
  149.  
  150. public void setAllSize(int allSize)
  151. {
  152. this.allSize = allSize;
  153. }
  154. }

sql:中无需使用分页查询

  1. <select id="getAllAuditDataList" resultMap="auditAllDataResult" parameterClass="map">
  2. <![CDATA[
  3. select distinct
  4. a.report_id,
  5. case when count(distinct b.cust_name)>1 then
  6. max(b.cust_name) || ' 等'
  7. else
  8. max(b.cust_name)
  9. end as cust_name,
  10. max(decode(a.tran_type,
  11. 'N',
  12. '正常',
  13. 'C',
  14. '改错')) as jy_type,
  15. max(a.exch_stamp || ' ' || a.action_desc) as desc,
  16. max(to_char(a.TIMESTAMP,'yyyy-MM-dd')) TIMESTAMP
  17. from report a,customer b,exch c
  18. group by a.report_id,b.cust_name
  19. order by check_date desc NULLS LAST, cust_name
  20. ]]>
  21. </select>

如果还有不太清楚的地方可以参看另一篇博文: spring集成ibatis进行项目中dao层基类封装 https://www.cnblogs.com/jiarui-zjb/p/9534810.html

写的不清楚的地方,欢迎留言指出!!!

一、iBatis进行分页查询的更多相关文章

  1. ibatis实现分页查询

    最近在做老项目改造,分享一个之前写的ibatis(这里特指ibatis2.x的版本)分页插件. 大致原理就是通过重写SqlExecutor的executeQuery方法,实现分页查询,支持mysql和 ...

  2. Ibatis的分页机制的缺陷

    我们知道,Ibatis为我们提供了可以直接实现分页的方法 queryForList(String statementName, Object parameterObject, int skipResu ...

  3. IBatis按条件分页查询

    XML中代码  <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMap PUBLIC & ...

  4. Ibatis调用存储过程实现增删改以及分页查询

    1.Ibatis实现增删改操作很简单了,通常我是将某一模块的增删改功能写在一个存储过程里,通过一个标识符去区分执行增加还是修改抑或删除操作. statement: <!-- 存储过程:实现学生的 ...

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

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

  6. MyBatis简单的增删改查以及简单的分页查询实现

    MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...

  7. Mybatis包分页查询java公共类

    Mybatis包分页查询java公共类   分页----对于数据量非常大的查询中.是不可缺少的. mybatis底层的分页sql语句因为须要我们自己去手动写.而实现分页显示的时候我们须要依据分页查询条 ...

  8. mysql分库 分页查询

    Mysql海量数据分表分库如何列表分页? 1.现在使用ElasticSearch了.基于Lucene的解决方案 2.必须将mysql里的数据写入到类似hbase这样的分布式数据库,查询快.但分页.查询 ...

  9. mybatis 框架 的应用之二(批量添加、实现分页查询)

    lf-driver=com.mysql.jdbc.Driver lf-url=jdbc:mysql://localhost:3306/test lf-user=LF lf-password=LF &l ...

随机推荐

  1. vCPU 和 CPU 的关系

    vCPU 和 pCPU 的关系不是数量,当被底层虚拟化之后,任何一个 vCPU 都是用到所有的 pCPU 核心总体的百分比,不是某一个核心这么去看的,并没有对应的关系,也不是一个很绝对的分配到具体某个 ...

  2. mysql 单表批量备份sh文件

    #!/bin/bashDBS=$(mysql -u root -padmin -e 'use database; show tables;' | awk '{ print $1 }');for tab ...

  3. python opencv:摄像头捕获图像

  4. python2.7环境下升级pip3,及出错解决办法

    执行 pip3 install --upgrade pip 进行升级 升级后若出现, Import Error:cannot import name main 是因为将pip更新为10.0.0后库里面 ...

  5. 杭电 1772 cake

    Cake Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. LeetCode 234. Palindrome Linked List(判断是否为回文链表)

    题意:判断是否为回文链表,要求时间复杂度O(n),空间复杂度O(1). 分析: (1)利用快慢指针找到链表的中心 (2)进行步骤(1)的过程中,对前半部分链表进行反转 (3)如果链表长是偶数,首先比较 ...

  7. 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]$表示陷阱的位置,如果你 ...

  8. CSS三列自适应布局(两边宽度固定,中间自适应)

    https://blog.csdn.net/cinderella_hou/article/details/52156333 https://blog.csdn.net/wangchengiii/art ...

  9. 「SDOI2005」区间

    「SDOI2005」区间 传送门 记录每一个位置作为左端点和右端点的出现次数,然后直接考虑差分即可. 参考代码: #include <cstdio> #define rg register ...

  10. nginx 重写 隐藏index.php

    修改 nginx.conf 文件location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break ...