后台代码对iBatis配置文件中具体的sql语句的调用实现(被封装的增删改查)
using IBatisNet.Common.Exceptions; using IBatisNet.DataAccess;
using IBatisNet.DataAccess.DaoSessionHandlers;
using IBatisNet.DataAccess.Interfaces; using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Commands;
using IBatisNet.DataMapper.Configuration.ParameterMapping;
using IBatisNet.DataMapper.MappedStatements;
using IBatisNet.DataMapper.Scope; using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Text; namespace YTO.WeiXin.Core
{
public class IbatisDaoExceptionHelper
{
// Methods
public IbatisDaoExceptionHelper()
{ }
public static string GetStatementDetail(ISqlMapper sqlMap, string statementName, object parameterObject)
{
try
{
StringBuilder builder = new StringBuilder();
StringBuilder builder2 = new StringBuilder();
MappedStatement mappedStatement = (MappedStatement)sqlMap.MappedStatements[statementName];
ISqlMapSession localSession = sqlMap.LocalSession;
RequestScope request = mappedStatement.Statement.Sql.GetRequestScope(mappedStatement, parameterObject, localSession);
IDbCommand command = new DbCommandDecorator(localSession.CreateCommand(mappedStatement.Statement.CommandType), request);
string preparedSql = request.PreparedStatement.PreparedSql;
StringCollection dbParametersName = request.PreparedStatement.DbParametersName;
IDbDataParameter[] dbParameters = request.PreparedStatement.DbParameters;
int count = dbParametersName.Count;
for (int i = ; i < count; i++)
{
IDbDataParameter parameter = dbParameters[i];
ParameterProperty mapping = request.ParameterMap.GetProperty(i);
builder.Append(parameter.ParameterName);
builder.Append("=[");
builder2.Append(parameter.ParameterName);
builder2.Append("=[");
builder.Append(mapping.PropertyName);
builder.Append(",");
IDbDataParameter dataParameter = command.CreateParameter();
request.ParameterMap.SetParameter(mapping, dataParameter, parameterObject);
if (dataParameter.Value == DBNull.Value)
{
builder.Append("null");
builder.Append("], ");
builder2.Append("System.DBNull, null");
builder2.Append("], ");
}
else
{
builder.Append(dataParameter.Value.ToString());
builder.Append("], ");
builder2.Append(dataParameter.DbType.ToString());
builder2.Append(", ");
builder2.Append(dataParameter.Value.GetType().ToString());
builder2.Append("], ");
}
}
string str2 = "Statement Id: [" + mappedStatement.Statement.Id + "] Parameters: [0]";
string str3 = "";
if (builder2.Length != )
{
str2 = "Statement Id: [" + mappedStatement.Statement.Id + "] Parameters: [" + builder.ToString(, builder.Length - ) + "]";
str3 = "Statement Id: [" + mappedStatement.Statement.Id + "] Types: [" + builder2.ToString(, builder2.Length - ) + "]";
}
return (preparedSql + Environment.NewLine + str2 + (!string.IsNullOrEmpty(str3) ? (Environment.NewLine + str3) : ""));
}
catch
{
}
return "can't get the sql error info";
} }
[Serializable]//可序列化
public class BaseSqlMapDao1 : IDao
{
protected const int PAGE_SIZE = ; protected string sqlMapNamespace; public BaseSqlMapDao1()
{ } protected string WrapStatementName(string statementName)
{
return (this.sqlMapNamespace + "." + statementName);
}
protected ISqlMapper GetLocalSqlMap()
{
SqlMapDaoSession localDaoSession = (SqlMapDaoSession)this.DaoManager.LocalDaoSession;
return localDaoSession.SqlMap;
} public IDaoManager DaoManager { get; set; }
protected object ExecuteDelete(string statementName, object parameterObject)
{
object obj2;
ISqlMapper localSqlMap = this.GetLocalSqlMap();
try
{
obj2 = localSqlMap.Delete(statementName, parameterObject);
}
catch (Exception exception)
{
string str = IbatisDaoExceptionHelper.GetStatementDetail(localSqlMap, statementName, parameterObject);
IBatisNetException exception2 = new IBatisNetException("Error executing delete '" + statementName + "'. Cause: " + exception.Message, exception);
exception2.Data.Add("sqlTrace", str);
throw exception2;
}
return obj2;
} protected object ExecuteInsert(string statementName, object parameterObject)
{
object obj2;
ISqlMapper localSqlMap = this.GetLocalSqlMap();
try
{
obj2 = localSqlMap.Insert(statementName, parameterObject);
}
catch (Exception exception)
{
string str = IbatisDaoExceptionHelper.GetStatementDetail(localSqlMap, statementName, parameterObject);
IBatisNetException exception2 = new IBatisNetException("Error executing insert '" + statementName + "'. Cause: " + exception.Message, exception);
exception2.Data.Add("sqlTrace", str);
throw exception2;
}
return obj2;
} protected int ExecuteUpdate(string statementName, object parameterObject)
{
int num;
ISqlMapper localSqlMap = this.GetLocalSqlMap();
try
{
num = localSqlMap.Update(statementName, parameterObject);
}
catch (Exception exception)
{
string str = IbatisDaoExceptionHelper.GetStatementDetail(localSqlMap, statementName, parameterObject);
IBatisNetException exception2 = new IBatisNetException("Error update '" + statementName + "'. Cause: " + exception.Message, exception);
exception2.Data.Add("sqlTrace", str);
throw exception2;
}
return num;
} protected int ExecuteUpdate(string statementName, object parameterObject)
{
int num;
ISqlMapper localSqlMap = this.GetLocalSqlMap();
try
{
num = localSqlMap.Update(statementName, parameterObject);
}
catch (Exception exception)
{
string str = IbatisDaoExceptionHelper.GetStatementDetail(localSqlMap, statementName, parameterObject);
IBatisNetException exception2 = new IBatisNetException("Error update '" + statementName + "'. Cause: " + exception.Message, exception);
exception2.Data.Add("sqlTrace", str);
throw exception2;
}
return num;
} protected IList<T> ExecuteQueryForList<T>(string statementName, object parameterObject)
{
IList<T> list;
ISqlMapper localSqlMap = this.GetLocalSqlMap();
try
{
list = localSqlMap.QueryForList<T>(statementName, parameterObject);
}
catch (Exception exception)
{
string str = IbatisDaoExceptionHelper.GetStatementDetail(localSqlMap, statementName, parameterObject);
IBatisNetException exception2 = new IBatisNetException("Error executing query '" + statementName + "' for list. Cause: " + exception.Message, exception);
exception2.Data.Add("sqlTrace", str);
throw exception2;
}
return list;
}
}
}
后台代码对iBatis配置文件中具体的sql语句的调用实现(被封装的增删改查)的更多相关文章
- 我来讲讲在c#中怎么进行xml文件操作吧,主要是讲解增删改查!
我把我写的四种方法代码贴上来吧,照着写没啥问题. 注: <bookstore> <book> <Id>1</Id> <tate>2010-1 ...
- “造轮运动”之 ORM框架系列(一)~谈谈我在实际业务中的增删改查
想想毕业已经快一年了,也就是大约两年以前,怀着满腔的热血正式跨入程序员的世界,那时候的自己想象着所热爱的技术生涯会是多么的丰富多彩,每天可以与大佬们坐在一起讨论解决各种牛逼的技术问题,喝着咖啡,翘着二 ...
- java程序设计课期中考试——数据库的增删改查和简单的js界面
首先是设计思路,对于数据库的增删改查,我们借助Ecilipse来进行前端和后端的编写.Ecilipse是可以进行java web项目的操作的. 前端,我们选择用使用jsp,所谓的jsp就是可以嵌入其他 ...
- IOS9中联系人对象的使用及增删改查操作的函数封装
之前克服重重困难把IOS9中新的类联系人搞明白了,现在把增删改查封装成了函数,如下: // // ViewController.m // IOS9中联系人CNContact的使用 // // Crea ...
- SQLite中的增删改查
虽然android提供了sql查询的封装方法,但是理解起来还是麻烦,所以我这里用sql语句来完成工作. 首先是建立一个类,继承SQLiteOpenHelper 这里面会建立一个数据库,并且初始化一个表 ...
- [译]聊聊C#中的泛型的使用(新手勿入) Seaching TreeVIew WPF 可编辑树Ztree的使用(包括对后台数据库的增删改查) 字段和属性的区别 C# 遍历Dictionary并修改其中的Value 学习笔记——异步 程序员常说的「哈希表」是个什么鬼?
[译]聊聊C#中的泛型的使用(新手勿入) 写在前面 今天忙里偷闲在浏览外文的时候看到一篇讲C#中泛型的使用的文章,因此加上本人的理解以及四级没过的英语水平斗胆给大伙进行了翻译,当然在翻译的过程中发 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(5)-EF增删改查by糟糕的代码
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(5)-EF增删改查by糟糕的代码 上一讲我们创建了一系列的解决方案,我们通过一个例子来看看层与层之间的关系 ...
- PHP程序中使用PDO对象实现对数据库的增删改查操作的示例代码
PHP程序中使用PDO对象实现对数据库的增删改查操作(PHP+smarty) dbconn.php <?php //------------------------使用PDO方式连接数据库文件- ...
- Mybatis框架基于映射文件和配置文件的方式,实现增删改查,可以打印日志信息
首先在lib下导入: 与打印日志信息有关的架包 log4j-1.2.16.jar mybatis架包:mybatis-3.1.1.jar 连接数据库的架包:mysql-connector-java-5 ...
随机推荐
- Swift和Objective-C混编注意
前言 Swift已推出数年,与Objective-C相比Swift的语言机制及使用简易程度上更接地气,大大降低了iOS入门门槛.当然这对新入行的童鞋们来讲,的确算是福音,但对于整个iOS编程从业者来讲 ...
- 服务订单SO创建
FUNCTION Z_SD_SALESORDER_CREATE. *"------------------------------------------------------------ ...
- NSDateFormatter 根据时间戳求出时间
NSDateFormatter 根据时间戳求出时间 - (void)detailWithStyle:(NSString*)style time:(NSInteger)time { // NSStrin ...
- xml数据解析调研
XML数据解析http://www.tuicool.com/articles/Nraau2(必用) http://www.cnblogs.com/pengyingh/articles/2342699. ...
- pthreads 0.1.0 测试报告
1 可以说已经稳定了 2 发现一个算是技巧的东西吧:在线程之间传递的类的实例,要保证能正常工作,需要类本身extends Stackable,所有方法都弄成public--我原来了写了一个数据库操作类 ...
- 最长公共上升子序列(LICS) 模板
void LICS() { ;i<=n;i++) { ; ;j<=n;j++) { if (a[i]==b[j]) f[i][j]=ma+; ][j]; ][j]>ma) ma=f[ ...
- 解决JavaScript中使用$.ajax方式提交数组参数
一般的,可能有些人在一个参数有多个值的情况下,可能以某个字符分隔的形式传递,比如页面上有多个checkbox: $.ajax{ url:"xxxx", data:{ p: &quo ...
- [转载]Android 异步加载解决方案
2013-12-25 11:15:47 Android 异步加载解决方案,转载自: http://www.open-open.com/lib/view/open1345017746897.html 请 ...
- 面试题目-c和c++的区别
在很大程度上,标准C++是标准C的超集.实际上,所有C程序也是C++程序,然而,两者之间有少量区别.下面简要介绍一下最重要的区别. 1. 在C++中,局部变量可以在一个程序块内在任何地方声明,在 ...
- 各种注释--------html,jsp
1. <!--到梦之都XHTML教程的链接--> <a href="http://www.dreamdu.com/xhtml/"> 学习XHTML < ...