IBatis.Net使用总结(三)-- IBatis实现分页返回数据和总数
IBatis 分页,这里没有使用其他插件,只使用最原始的方法。
输入参数:
int currentPage 当前页
int pageSize 每页大小
Hashtable findCondition 查询条件
out int total 返回总数
输出:
DataTable 或者 IList<T>
使用了三种分页方式,根据实际情况使用。
我在实际应用中,
第一种返回DataTable,在使用过程中,需要注意它所映射的实体对象名称字段。
第二种方法返回泛型集合,使用的比较顺手,也是习惯使用的方法。
第三种方法也是返回泛型集合。但是,它使用的两个参数,偏移量和页面大小,我平常用的概率小点。
1:在一个statements中,使用了两条语句,一个是返回所需的列,一个是返回总数。。
<select id="Article_FindPageByCond" parameterClass="HashTable" resultClass="System.Data.DataSet" >
<![CDATA[select
T.[PK_Article]
,T.[ArticleTitle]
,T.[ArticleAuthor]
,T.[ArticleSummary]
,T.[ArticleContent]
,T.[Sort]
,T.[EditTime]
,T.[Dr]
,T.[Ts]
from
( select A.*, ROW_NUMBER() OVER ( ORDER BY
(A.[PK_Article] )
) rn
from
(select * from [dbo].[Article]]]>
<dynamic prepend="WHERE">
<isNotEmpty prepend="and" property="PKArticle">
PKArticle LIKE '%'+#PKArticle#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleTitle">
ArticleTitle LIKE '%'+#ArticleTitle#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleAuthor">
ArticleAuthor LIKE '%'+#ArticleAuthor#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleSummary">
ArticleSummary LIKE '%'+#ArticleSummary#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleContent">
ArticleContent LIKE '%'+#ArticleContent#+'%'
</isNotEmpty>
<isNotNull property="Sort">
<isNotEmpty property="Sort">
<isNotEqual prepend="and" property="Sort" compareValue="0">
Sort LIKE '%'+#Sort#+'%'
</isNotEqual>
</isNotEmpty>
</isNotNull>
<isNotEmpty prepend="and" property="EditTime">
EditTime LIKE '%'+#EditTime#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="Dr">
Dr LIKE '%'+#Dr#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="Ts">
Ts LIKE '%'+#Ts#+'%'
</isNotEmpty>
</dynamic>
) A
) T
where 1=1 and
<![CDATA[ rn <= #currentPage# * #pageSize# ]]>
and <![CDATA[ rn >(#currentPage# - 1) * #pageSize# ]]>
<![CDATA[
select count(*) as total
from
( select A.*, ROW_NUMBER() OVER ( ORDER BY
(A.[PK_Article] )
) rn
from
(select * from [dbo].[Article]]]>
<dynamic prepend="WHERE">
<isNotEmpty prepend="and" property="PKArticle">
PKArticle LIKE '%'+#PKArticle#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleTitle">
ArticleTitle LIKE '%'+#ArticleTitle#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleAuthor">
ArticleAuthor LIKE '%'+#ArticleAuthor#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleSummary">
ArticleSummary LIKE '%'+#ArticleSummary#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleContent">
ArticleContent LIKE '%'+#ArticleContent#+'%'
</isNotEmpty>
<isNotNull property="Sort">
<isNotEmpty property="Sort">
<isNotEqual prepend="and" property="Sort" compareValue="0">
Sort LIKE '%'+#Sort#+'%'
</isNotEqual>
</isNotEmpty>
</isNotNull>
<isNotEmpty prepend="and" property="EditTime">
EditTime LIKE '%'+#EditTime#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="Dr">
Dr LIKE '%'+#Dr#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="Ts">
Ts LIKE '%'+#Ts#+'%'
</isNotEmpty>
</dynamic>
) A
) T
</select>
返回所需列,返回总数
这种方式,调用前面所说的返回DataTable的方法,完成分页
/// <summary>
/// 按条件获取分页数据,返回DataTable对象
/// </summary>
/// <param name="currentPage"></param>
/// <param name="pageSize"></param>
/// <param name="findCondtion"></param>
/// <param name="total"></param>
/// <returns></returns>
public DataTable FindPageByCondition(int currentPage, int pageSize, Hashtable findCondition, out int total)
{
String stmtId = "Article_FindPageByCond";
total = ;
findCondition.Add("currentPage", currentPage);
findCondition.Add("pageSize", pageSize);
BaseDao bd = new BaseDao();
DataSet ds = bd.QueryForDataSet(stmtId, findCondition);
DataTable dt = ds.Tables[];
total = Convert.ToInt32(ds.Tables[].Rows[][].ToString());
return dt;
}
使用BaseDao.cs返回分页之后的DataTable
2:如果想要返回泛型集合IList<T>,则使用两个statements。一个select返回实体映射,一个select返回总数。
<select id="Article_GetPageByCond" parameterClass="HashTable" resultMap="FullResultMap" >
<![CDATA[select
T.[PK_Article]
,T.[ArticleTitle]
,T.[ArticleAuthor]
,T.[ArticleSummary]
,T.[ArticleContent]
,T.[Sort]
,T.[EditTime]
,T.[Dr]
,T.[Ts]
from
( select A.*, ROW_NUMBER() OVER ( ORDER BY
(A.[PK_Article] )
) rn
from
(select * from [dbo].[Article]]]>
<dynamic prepend="WHERE">
<isNotEmpty prepend="and" property="PKArticle">
PKArticle LIKE '%'+#PKArticle#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleTitle">
ArticleTitle LIKE '%'+#ArticleTitle#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleAuthor">
ArticleAuthor LIKE '%'+#ArticleAuthor#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleSummary">
ArticleSummary LIKE '%'+#ArticleSummary#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleContent">
ArticleContent LIKE '%'+#ArticleContent#+'%'
</isNotEmpty>
<isNotNull property="Sort">
<isNotEmpty property="Sort">
<isNotEqual prepend="and" property="Sort" compareValue="0">
Sort LIKE '%'+#Sort#+'%'
</isNotEqual>
</isNotEmpty>
</isNotNull>
<isNotEmpty prepend="and" property="EditTime">
EditTime LIKE '%'+#EditTime#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="Dr">
Dr LIKE '%'+#Dr#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="Ts">
Ts LIKE '%'+#Ts#+'%'
</isNotEmpty>
</dynamic>
) A
) T
where 1=1 and
<![CDATA[ rn <= #currentPage# * #pageSize# ]]>
and <![CDATA[ rn >(#currentPage# - 1) * #pageSize# ]]>
</select>
返回映射FullResultMap
<select id="Article_GetCountByCond" resultClass="System.Int32">
<![CDATA[
SELECT count(*) as total
FROM
( select A.*, ROW_NUMBER() OVER ( ORDER BY
(A.[PK_Article] )
) rn
from
(SELECT * FROM [dbo].[Article]]]>
<dynamic prepend="WHERE">
<isNotEmpty prepend="and" property="PKArticle">
PKArticle LIKE '%'+#PKArticle#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleTitle">
ArticleTitle LIKE '%'+#ArticleTitle#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleAuthor">
ArticleAuthor LIKE '%'+#ArticleAuthor#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleSummary">
ArticleSummary LIKE '%'+#ArticleSummary#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="ArticleContent">
ArticleContent LIKE '%'+#ArticleContent#+'%'
</isNotEmpty>
<isNotNull property="Sort">
<isNotEmpty property="Sort">
<isNotEqual prepend="and" property="Sort" compareValue="0">
Sort LIKE '%'+#Sort#+'%'
</isNotEqual>
</isNotEmpty>
</isNotNull>
<isNotEmpty prepend="and" property="EditTime">
EditTime LIKE '%'+#EditTime#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="Dr">
Dr LIKE '%'+#Dr#+'%'
</isNotEmpty>
<isNotEmpty prepend="and" property="Ts">
Ts LIKE '%'+#Ts#+'%'
</isNotEmpty>
</dynamic>
) A
) T
</select>
返回总数
使用IList<T> QueryForList<T>(string statementName, object parameterObject);
/// <summary>
/// 按条件获取分页数据,返回IList对象
/// </summary>
/// <param name="currentPage"></param>
/// <param name="pageSize"></param>
/// <param name="findCondtion"></param>
/// <param name="total"></param>
/// <returns></returns>
public IList<Article> GetPageByCondition(int currentPage, int pageSize, Hashtable findCondition)
{
IList<Article> list=new List<Article>();
String stmtId = "Article_GetPageByCond";
findCondition.Add("currentPage", currentPage);
findCondition.Add("pageSize", pageSize);
IList<Article> result = SqlMap.QueryForList<Article>(stmtId,findCondition);
return result;
}
分页返回IList对象
3:使用ibatis.net本身自带的分页功能。
int skipResults 偏移量
int maxResults 每页大小(偏移量之后的页面大小)
statements还是使用了第2种方法,两个statements
IList<T> QueryForList<T>(string statementName, object parameterObject, int skipResults, int maxResults);
IList QueryForList(string statementName, object parameterObject, int skipResults, int maxResults);
public IList GetPage(int skipResults, int maxResults, Hashtable findCondition)
{
String stmtId = "Article_GetPageByCond";
IList result = SqlMap.QueryForList(stmtId, findCondition, skipResults, maxResults);
return result;
}
使用自带的分页功能
IBatis.Net使用总结(三)-- IBatis实现分页返回数据和总数的更多相关文章
- sql 排序函数ROW_NUMBER分页返回数据
分页从数据库返回一张表的某些条数据 假设我需要查询 系统表 sys.all_columns中的数据,每次查询10条 第一次查询第1-10条数据 第二次查询第11-20条数据 第三次查询第21-30条数 ...
- easyui-datagrid连接数据库实现分页查询数据
一.利用MVC思想建立底层数据库: package com.hanqi.dao; import java.util.ArrayList; import java.util.List; import o ...
- MySQL+Service+Servlet+Jsp实现Table表格分页展示数据
下面以一个示例讲解如何使用MySQL+Service+Servlet+Jsp实现Table表格分页展示数据: eg:请假管理系统 要求如下: 一.打开首页页面, 访问查询请假记录的 servlet , ...
- tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加
tp5 使用paginate分页获取数据对象之后 如何对对象进行数据添加 大家都知道,在使用tp5的paginate获取分页数据之后,得到的是一个数据对象,但有时会碰到要对数据对象进行二次加工的情况, ...
- ASP.NET(五):ASP.net实现真分页显示数据
导读:在上篇文章中,介绍了用假分页实现数据的分页显示 ,而避免了去拖动滚动条.但,假分页在分页的同时,其实是拖垮了查询效率的.每一次分页都得重新查询一遍数据,那么有没有方法可以同时兼顾效率和分页呢,那 ...
- Oracle数据库排序后分页查询数据错误问题解决
一.问题描述:根据更新时间倒序排序然后分页查询数据,但是点击分页操作的时候,会出现数据重复看似没有操作的情况 二.问题错误原因分析 分页查询的SQL语句: select * FROM (select ...
- SqlServer存储过程应用二:分页查询数据并动态拼接where条件
前言 开发中查询功能是贯穿全文的,我们来盘一盘使用存储过程分页查询,并且支持动态拼接where条件. 划重点:支持动态拼接where条件 对存储过程的使用有疑问的同学去[SqlServer存储过程的创 ...
- Activity详解三 启动activity并返回结果
首先看演示: 1 简介 .如果想在Activity中得到新打开Activity 关闭后返回的数据,需要使用系统提供的startActivityForResult(Intent intent, int ...
- android中listview分页载入数据
前段时间做的新浪微博项目一直想实现listview分页载入数据,今天最终实现了,哈哈!感觉挺好的,今天又写了个demo给大家分享下. 首先说下listview的优化方案,这也是面试中常考的题目.优化方 ...
随机推荐
- GitHub 上 57 款最流行的开源深度学习项目
转载:https://www.oschina.net/news/79500/57-most-popular-deep-learning-project-at-github GitHub 上 57 款最 ...
- [C#] 使用NPOI将Datatable保存到Excel
using (table) { IWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(); IRow ...
- UOJ#67. 新年的毒瘤
传送门 练习一下Tarjan的模板. 求一下割点,然后加个约束条件判一下特殊点,剩下的就是所求点. //UOJ 67 //by Cydiater //2016.10.27 #include <i ...
- linux系统的学习
通过<鸟哥的linux私房菜>的学习,自己得到的收获! 关机与重启 shutdown -k now "message" 用以发送所有信息,并不是真的关机.还可以登录新的 ...
- apk 破解
Apk破解工具:AndroidCrackTool for Mac http://www.52pojie.cn/thread-452617-1-1.html 如何防止Unity3D代码被反编译 http ...
- 蘑菇街TeamTalk编译连接过程中遇到的问题及解决方法(iOS)
今天浏览博文的时候,“蘑菇街开源的即时通讯框架,包括iOS.Android.Mac.Windows客户端和后台 Github源码下载地址:https://github.com/mogujie/Team ...
- 2012 Multi-University Training Contest 9 / hdu4389
2012 Multi-University Training Contest 9 / hdu4389 打巨表,实为数位dp 还不太懂 先这样放着.. 对于打表,当然我们不能直接打,这里有技巧.我们可以 ...
- Linux文件查找命令 find 详解
关于find命令 由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下.即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权 ...
- BlockingQueue 阻塞队列,很有用的一种
BlockingQueue的核心方法:放入数据: offer(anObject):表示如果可能的话,将anObject加到BlockingQueue里,即如果BlockingQueue可以容纳, 则返 ...
- C#高级编程笔记 2016年10月8日运算符和类型强制转换
1.checked和unchecked 运算符 C#提供了checked 和uncheckde 运算符.如果把一个代码块标记为checked, CLR就会执行溢出检查,如果发生溢出,就抛出overfl ...