一、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 ...
随机推荐
- Ubuntu系统中创建虚拟环境
1.虚拟环境产生的原因:如果在一台电脑上, 想开发多个不同的项目, 需要用到同一个包的不同版本, 如果使用上面的命令, 在同一个目录下安装或者更新, 新版本会覆盖以前的版本, 其它的项目就无法运行了. ...
- iOS 上通过 802.11k、802.11r 和 802.11v 实现 Wi-Fi 网络漫游
在 iOS 上通过 802.11k.802.11r 和 802.11v 实现 Wi-Fi 网络漫游 了解 iOS 如何使用 Wi-Fi 网络标准提升客户端漫游性能. iOS 支持在企业级 Wi-Fi ...
- 【Python】【Django】用户注册功能
GET方法前置步骤做完 stu.models.py 再其中创建需要用到的字段及对应数据库的表 # -*- coding: utf-8 -*- from __future__ import unicod ...
- CentOS上安装elasticsearch
1.安装docker yum install docker 2.启动docker systemctl start docker 查看docker版本: docker -v 之后只要启动机器就会自动开启 ...
- how to analyze jmeter results
https://octoperf.com/blog/2017/10/19/how-to-analyze-jmeter-results/
- Python 爬取 热词并进行分类数据分析-[简单准备] (2020年寒假小目标05)
日期:2020.01.27 博客期:135 星期一 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备](本期博客) b.[云图制作+数据导入] ...
- HDU 2680 最短路 迪杰斯特拉算法 添加超级源点
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- vue学习笔记:Hello Vue
编写简单例子,了解下基本语法 <!DOCTYPE html> <html> <head> <meta charset="utf-8 "&g ...
- 10 JavaScript对象&类&for循环
JavaScript对象 JavaScript中所有事物都是对象:字符串.数值.数组.函数.数学和正则表达式 JavaScript有些类型可以是字面量而非对象:如字符串.数值.布尔值 JavaScri ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:地址(Address)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...