惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - quitgame

IBM 、M$ 、Google & Apple Java 程序员 和 .NET 程序员 Chrome 必将战胜 Firefox。 一特性让IE8难以望Firefox3项背 一个女人的一天,牛逼! - quitgame - 博客园 Meizu M8 Preview IE 已死 很WEB很2.0---谷歌金山糍粑 惊天大发现:WindowsXP SP3带来的新功能! 很WEB很2.0---ThunderBird 谈恋爱是一个项目 爱上 UBUNTU UBUNTU 图两个 我的一些项目管理经验 并行开发版本管理之路(四) --- 流动的基线 并行开发版本管理之路(三) --- 版本的强制控制和版本合并 流氓软件,你装了吗? 并行开发版本管理之路(二) --- 典型的版本管理难题 并行开发版本管理之路(一) --- 版本管理危机
IBatis.net 获取记录数之道 -- 迂回
quitgame · 2008-05-22 · via 博客园 - quitgame

欲练此功,必先自宫。
欲分页,必先获取记录数。

IBatis简单却又强大,然而用过IBatis的人都之道,在IBatis里想通过查询列表的SQL去获取记录数却不是件容易的事情,我在网上搜了N次没有搜到。

思路:
要想获取记录数(不特意写SQL),必须先获取到所执行的最终的SQL语句,并在外面包一层 select count(*)  from (原始sql) 来做;
研究了一下SDK,发现获取最终执行的SQL语句是不可能的,但可以从里面得到 IDbCommand 和 IDbConnection,IDbCommand 的参数都已经准备好,我们只要串改一下CommandText,就豁然开朗了:

public static int QueryForCount(string statementName, object paramObject) {
        ISqlMapper mapper 
= Mapper.Instance();
        IMappedStatement statement 
= mapper.GetMappedStatement(statementName);
        IDbCommand cmd 
= null;
        
try {
            
if (!mapper.IsSessionStarted) {
                mapper.OpenConnection();
            }

            RequestScope scope 
= statement.Statement.Sql.GetRequestScope(statement, paramObject, mapper.LocalSession);
            statement.PreparedCommand.Create(scope, mapper.LocalSession, statement.Statement, paramObject);
            cmd 
= scope.IDbCommand;
            cmd.Connection 
= scope.Session.Connection;

            cmd.CommandText 
= string.Format("select count(*) c from ({0}) t ", cmd.CommandText);
            
using (IDataReader dr = cmd.ExecuteReader()) {
                dr.Read();
                
return dr.GetInt32(0);
            }

        }
 finally {
            
if( cmd != null && cmd.Connection != null && cmd.Connection.State != ConnectionState.Closed)
                cmd.Connection.Close();
        }
 
    }

希望对大家有用。