iBatis.Net实现返回DataTable和DataSet对象
如题。要返回一个ADO.NET对象好像没有使用ORM的必要,而且从编程的角度看这样的实现一点也不OO,但是实际的开发场景中还是会碰到这种需求的。下面我就借鉴前人的经验,结合实际的示例,再总结一下。如果您认真看完,应该可以体会得到我的一些尝试,而不是人云亦云的照搬代码。
1、获得DbCommand对象
对于SQL语句,方法如下:

/// <summary>
/// SQL语?句?,?获?取?DbCommand
/// </summary>
/// <param name="sqlMapper"></param>
/// <param name="statementName"></param>
/// <param name="paramObject"></param>
/// <returns></returns>
protected virtual IDbCommand GetDbCommand(ISqlMapper sqlMapper, string statementName, object paramObject)
{
IStatement statement = sqlMapper.GetMappedStatement(statementName).Statement;
IMappedStatement mapStatement = sqlMapper.GetMappedStatement(statementName);
ISqlMapSession session = new SqlMapSession(sqlMapper); if (sqlMapper.LocalSession != null)
{
session = sqlMapper.LocalSession;
}
else
{
session = sqlMapper.OpenConnection();
} RequestScope request = statement.Sql.GetRequestScope(mapStatement, paramObject, session);
mapStatement.PreparedCommand.Create(request, session as ISqlMapSession, statement, paramObject);
IDbCommand cmd = session.CreateCommand(CommandType.Text);
cmd.CommandText = request.IDbCommand.CommandText;
//return request.IDbCommand;
return cmd;
}
对于存储过程,因为对于参数类型的不同,需要多几步处理(因为需要多维护一个参数字典和其对应的ParameterDirection字典):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
/// <summary> /// 获取DbCommand,主要是针对存储过程 /// </summary> /// <param name="sqlMapper"></param> /// <param name="statementName"></param> /// <param name="paramObject">参数</param> /// <param name="dictParam">参数字段</param> /// <param name="dictParmDirection">ParameterDirection字典</param> /// <param name="cmdType"></param> /// <returns></returns> protected virtual IDbCommand GetDbCommand(ISqlMapper sqlMapper, string statementName, object paramObject, IDictionary dictParam, IDictionary< string , ParameterDirection> dictParmDirection, CommandType cmdType) { if (cmdType == CommandType.Text) { return GetDbCommand(sqlMapper, statementName, paramObject); } IStatement statement = sqlMapper.GetMappedStatement(statementName).Statement; IMappedStatement mapStatement = sqlMapper.GetMappedStatement(statementName); ISqlMapSession session = new SqlMapSession(sqlMapper); if (sqlMapper.LocalSession != null ) { session = sqlMapper.LocalSession; } else { session = sqlMapper.OpenConnection(); } RequestScope request = statement.Sql.GetRequestScope(mapStatement, paramObject, session); mapStatement.PreparedCommand.Create(request, session as ISqlMapSession, statement, paramObject); IDbCommand cmd = session.CreateCommand(cmdType); cmd.CommandText = request.IDbCommand.CommandText; if (cmdType != CommandType.StoredProcedure || dictParam == null ) { return cmd; } foreach (DictionaryEntry de in dictParam) //存储过程 { string key = de.Key.ToString(); IDbDataParameter dbParam = cmd.CreateParameter(); dbParam.ParameterName = key; dbParam.Value = de.Value; if (dictParmDirection != null && dictParmDirection.ContainsKey(key)) { dbParam.Direction = dictParmDirection[key]; //ParameterDirection } cmd.Parameters.Add(dbParam); } return cmd; } |
代码写得可能还有改进的必要,有需要从事这方面开发的童鞋,如果您看着有更好的办法请不吝赐教。
备注:
a、对于1.6.1之前的版本,获得命令的方式可以通过RequestScope的IDbCommand属性,但是1.6.1版本的IDbCommand属性返回的是IBatisNet.DataMapper.Commands.DbCommandDecorator对象,您可以注释代码验证一下。
b、网上有些文章贴的方法返回的DbCommand对象都是对于拼接SQL语句而言,没有实现获取存储过程的DbCommand(有参数无参数的都要考虑)。本文在原有资料的基础上,尝试着做出改进,目前支持SQL语句和存储过程。
2、返回DataSet对象
通过SQL语句,获取DataSet:
1
2
3
4
5
|
public DataSet GetDSPerson( int id) { string sql = this .GetRuntimeSql( this .SqlMapper, this .GetStatementName( "GetDSPerson" ), id); return this .QueryForDataSet( this .SqlMapper, this .GetStatementName( "GetDSPerson" ), id); } |
XML配置:
1
2
3
4
|
< select id="GetDSPerson" parameterClass="int" resultClass="System.Data.DataSet"> < include refid="CommonPersonColumns4Select"></ include > WHERE 1=1 AND Id=$id$ </ select > |
客户端的调用:
1
2
3
|
int id = 1; DataSet ds = ServiceFactory.CreatePersonService().GetDSPerson(id); Console.WriteLine(ds.GetXml()); |
执行结果返回如下:
3、返回DataTable对象
a、通过SQL语句

/// <summary>
/// 通?用?的?执′行DSQL语?句?以?DataTable的?方?式?得?到?返う?回?的?结á果?(xml文?件t中D参?数簓要癮使?用?$标括?记?的?占?位?参?数簓)
/// </summary>
/// <param name="sqlMapper"></param>
/// <param name="statementName"></param>
/// <param name="paramObject"></param>
/// <returns></returns>
protected virtual DataTable QueryForDataTable(ISqlMapper sqlMapper, string statementName, object paramObject)
{
DataSet ds = new DataSet();
bool isSessionLocal = false;
IDalSession session = sqlMapper.LocalSession;
if (session == null)
{
session = new SqlMapSession(sqlMapper);
session.OpenConnection();
isSessionLocal = true;
} IDbCommand cmd = GetDbCommand(sqlMapper, statementName, paramObject);//SQL text command try
{
cmd.Connection = session.Connection;
IDbDataAdapter adapter = session.CreateDataAdapter(cmd);
adapter.Fill(ds);
}
finally
{
if (isSessionLocal)
{
session.CloseConnection();
}
} return ds.Tables[0];
}
这个相对简单,因为前面2中已经得到了DataSet,DataTable的提取就轻而易举了。
b、通过含OUTPUT参数的存储过程
这个地方主要就是改进后的GetDbCommand重载方法的使用,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
/// <summary> /// 查询返回DataTable,对于包括OUTPUT参数的存储过程同样适用 /// </summary> /// <param name="sqlMapper"></param> /// <param name="statementName"></param> /// <param name="paramObject">参数</param> /// <param name="dictParam">参数字典</param> /// <param name="dictParamDirection">ParameterDirection字典</param> /// <param name="htOutPutParameter">返回的Output参数值哈希表</param> /// <returns></returns> protected virtual DataTable QueryForDataTable(ISqlMapper sqlMapper, string statementName, object paramObject, IDictionary dictParam, IDictionary< string , ParameterDirection> dictParamDirection, out Hashtable htOutPutParameter) { DataSet ds = new DataSet(); bool isSessionLocal = false ; ISqlMapSession session = sqlMapper.LocalSession; if (session == null ) { session = new SqlMapSession(sqlMapper); session.OpenConnection(); isSessionLocal = true ; } IDbCommand cmd = GetDbCommand(sqlMapper, statementName, paramObject, dictParam, dictParamDirection, CommandType.StoredProcedure); //存储过程 try { cmd.Connection = session.Connection; IDbDataAdapter adapter = session.CreateDataAdapter(cmd); adapter.Fill(ds); } finally { if (isSessionLocal) { session.CloseConnection(); } } htOutPutParameter = new Hashtable(); foreach (IDataParameter parameter in cmd.Parameters) { if (parameter.Direction == ParameterDirection.Output) { htOutPutParameter[parameter.ParameterName] = parameter.Value; } } return ds.Tables[0]; } |
测试的存储过程如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
USE [TestDb] GO --根据id查询某人 并返回所有人中,最大体重,最小身高 CREATE PROCEDURE [dbo].[usp_GetPersonById] @MaxWeight float output , @MinHeight float output , @Id int AS BEGIN SELECT Id, FirstName, LastName, Weight, Height FROM Person WHERE Id=@Id SET @MaxWeight= ( SELECT MAX (Weight) FROM Person) SET @MinHeight= ( SELECT MIN (Height) FROM Person) END |
本文的示例测试通过,返回的结果如下:
从上图中,我们可以看到最大体重是200,最矮身高是177。
4、小结和注意点
a、返回ADO.NET对象的方法,iBatis拼接的SQL语句必须通过而不是熟悉的#符号连接,这个会有众所周知的SQL注入的风险。
b、我还没有实际尝试过最新版本的iBatis,对于较新的版本,不知道本文的方法还适不适用。
c、我参考这篇文章的时候,发现说有一个无法返回output参数的问题。我尝试着重现这个问题。功夫不负有心人,本文示例中我已经实现好了,给自己鼓励一下。
iBatis.Net实现返回DataTable和DataSet对象的更多相关文章
- IBatis.Net使用总结(二)-- IBatis返回DataTable/DataSet(网上例子的集合)
IBatis返回DataTable,DataSet ibatis.net QueryForDataTable 完整的为ibatis.net 引入datatable支持要改动很多地方,所以描述的是最小化 ...
- LINQ返回DataTable类型 list转dataset 转换为JSON对象
using System.Web.Script.Serialization; using System.Collections.Generic; using System.Reflection; us ...
- IBatis.Net使用总结(三)-- IBatis实现分页返回数据和总数
IBatis 分页,这里没有使用其他插件,只使用最原始的方法. 输入参数: int currentPage 当前页 int pageSize 每页大小 Hashtable findCondition ...
- C#与数据库访问技术总结(十六)之 DataSet对象
DataSet对象 DataSet对象可以用来存储从数据库查询到的数据结果,由于它在获得数据或更新数据后立即与数据库断开,所以程序员能用此高效地访问和操作数据库. 并且,由于DataSet对象具有离线 ...
- 浅谈ASP.net中的DataSet对象
在我们对数据库进行操作的时候,总是先把数据从数据库取出来,然后放到一个"容器"中,再通过这个"容器"取出数据显示在前台,而充当这种容器的角色中当属DataSet ...
- C# 实现DataTable、DataSet与XML互相转换
/**//// <summary> /// 把DataSet.DataTable.DataView格式转换成XML字符串.XML文件 /// </summary> public ...
- DataTable与DTO对象的简易转换类
在web开发过程中,有时候为了数据传输的方便,比如:后台需要更新前端的ViewModel,此时我们定义一个与前端ViewModel结构一样的DTO对象,从数据层获取数据后,将数据封装成DTO然后序列化 ...
- .Net中List<T> 泛型转成DataTable、DataSet
在开发过程过程中有时候需要将List<T>泛型转换成DataTable.DataSet,可以利用反射机制将DataTable的字段与自定义类型的公开属性互相赋值. 1.List<T& ...
- 将Json数据转换为ADO.NET DataSet对象
Json数据转换为ADO.NET DataSet其实方法有很多,Newtonsoft.Json也提供了DataSet的Converter用以转换Json数据.但是有些情况下DataSet Conver ...
随机推荐
- 系统自带的NSJSONSerialization解析json文件
#import "ViewController.h" #import "Student.h" #import "GDataXMLNode.h" ...
- 【原】就IOS发布app时如何保护文本资源的一个方法
近期的一个app是本地的,数据源来自于本地的一个.json文件,里面的数据是这个app的灵魂.近期快发布该app了,很担心发布后的.ipa包被竞争者解开然后信息发生泄漏.我的处理策略是:打包的时候放的 ...
- iOS 编译报错
Undefined symbols for architecture i386: “_OBJC_CLASS_$_XXX”, referenced from: objc-class-ref in XXX ...
- iOS多线程-03-NSOperation与NSOperationQueue
简介 通过NSOperation与NSOperationQueue的组合也能实现多线程 通常将任务封装成NSOperation对象,并将对象添加到NSOperationQueue中实现 NSOpera ...
- iOS网络-02-数据解析(JSON与XML)
数据交互格式 服务器返回给用户的数据,通常是以下两种方式: JSON XML JSON 一种轻量级的数据数据格式,体积比XML小,是服务器返回给移动端通常采用的格式 用使用JSON文件中的数据,需要对 ...
- C语言-03-流程控制
一.选择结构 1> if语句 使用注意 ① if语句中的条件语句,不要把==和=弄混,可以把常量作为左值, 这样的话,在无用=的情况下,编译时会报错 ② if语句后若要定义新的变量或者有多条语句 ...
- 第一次开发PHP网页Hello PHP
打开安装好的XAMPP的三个服务: 然后打开phpStorm,在Open选项选择文件目录(最后一个目录是htdocs)打开: 3.有时候可能无法修改php文件,会弹出一些提示窗口.那么就打开Finde ...
- HTTP/HTTPs要点
Http(超文本传输协议)是一个属于应用层的面向对象的协议. HTTP结构 HTTP请求: 请求行:Method-Request URI HTTP-Version(CRLF),eg:GET /form ...
- poj邮局1160
题目是给出V个村庄,取出P个做为邮局,要求每个村庄到邮局的距离和最小. 先考虑只有一个邮局的情况,不管你怎么放邮局和最小的情况总是在中点的位置. 再来考虑P>1的情况: 假设P-1个邮局已经选好 ...
- ExtJS提交到服务器端的方式以及简单的登录实现
ExtJS平台已经搭建好了,那么接下来要做网站的登录页面,当然还是在jsp页面里加载extjs的,首先我们先了解一下关于extjs是如何提交到服务器端的: 1.EXT的form表单ajax提交(默认提 ...