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的优化方案,这也是面试中常考的题目.优化方 ...
随机推荐
- VMware安装ubuntu虚拟机
创建虚拟机完成. 安装操作系统. 中文版界面: 中文版界面: 稍等在重启 重新启动虚拟机. 安装VMtools 双击该图标 安装完成,重新启动系统. 如果在安装的时候,主机名没有修改 看着太长 如何修 ...
- COGS729. [网络流24题] 圆桌聚餐
«问题描述:假设有来自m 个不同单位的代表参加一次国际会议.每个单位的代表数分别为ri(i=1,2,3...m), .会议餐厅共有n张餐桌,每张餐桌可容纳c i(i=1,2...n) 个代表就餐.为了 ...
- 【Beta版本】七天冲刺集结令
031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬涛 [Bet ...
- JQuery事件之鼠标事件
鼠标事件是在用户移动鼠标光标或者使用任意鼠标键点击时触发的. ():click事件:click事件于用户在元素敲击鼠标左键,并在相同元素上松开左键时触发. $('p').click(function( ...
- 【原创】风讯DotNetCMS V1.0~V2.0 SQL注入漏洞
文章作者:rebeyond 注:文章首发I.S.T.O信息安全团队,后由原创作者友情提交到乌云-漏洞报告平台.I.S.T.O版权所有,转载需注明作者. 受影响版本:貌似都受影响. 漏洞文件:use ...
- 自然语言26_perplexity信息
http://www.ithao123.cn/content-296918.html 首页 > 技术 > 编程 > Python > Python 文本挖掘:简单的自然语言统计 ...
- margin双边距的问题
margin:20px;height:20px;float:left margin:20px;height:20px;float:left
- morse code
morse code,摩斯电码,是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母.数字和标点符号. 摩斯电码,是一种早期的数字化通信形式,但是它不同于现代只使用0和1两种状态的二进制代 ...
- filterHTML
function filterHTML(source) { return !source ? "" : source.replace(/]*>/g, "" ...
- iOS抓包利器Charles
iOS抓包利器Charles http://wonderffee.github.io/blog/2013/07/13/best-packet-capture-tool-charles-in-ios/ ...