后台代码对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 ...
随机推荐
- 【转载】SAP的标准对话框函数
http://blog.sina.com.cn/s/blog_721b218c01012j0y.html 在用户设计sap的程序时,经常需要一些对话框,用户可以自己编写,但使用SAP系统中提供了的对话 ...
- Extjs 视频教程
---恢复内容开始--- 网易云课堂 <尚学堂_Ext视频教程> login.html <html> <head> <meta http-equiv=&quo ...
- for循环语句以及迭代法和穷举法
循环语句: 四要素:初始条件,循环条件,状态改变,循环体 for(初始条件;循环条件;状态改变){ //循环体} 案例1:打印等腰直角三角形和菱形 左上三角 static void Main(stri ...
- 《Play for Java》学习笔记(四)Controller
play的一大优势是可以将HTTP映射到JAVA API代码(Type-safe mapping from HTTP to an idiomatic Scala or Java API),完美的实现了 ...
- netty4 连通步骤
转载:http://xw-z1985.iteye.com/blog/1973205 服务端依次发生的步骤 建立服务端监听套接字ServerSocketChannel,以及对应的管道pipeline: ...
- Mysql安全配置【转】
相关学习资料 http://drops.wooyun.org/tips/2245 http://www.cnblogs.com/siqi/archive/2012/11/21/2780966.html ...
- FZU 2090 旅行社的烦恼 floyd 求无向图最小环
题目链接:旅行社的烦恼 题意是求无向图的最小环,如果有的话,输出个数,并且输出权值. 刚刚补了一发floyd 动态规划原理,用了滑动数组的思想.所以,这个题就是floyd思想的变形.在k从1到n的过程 ...
- spring中配置jdbc数据源
1.加入jdbc驱动器包,mysql-connector-java.jar 2.加入commons-dbcp.jar配置数据源 3.在classpath下新建文件jdbc.properties,配置j ...
- uva 1210
#include<iostream> #include<cstring> using namespace std; + ; bool notprime[MAXN];//值为fa ...
- C#启动一个外部程序(1)-WinExec
C#启动一个外部程序(1)-WinExec 调用Win32 API.1. using System.Runtime.InteropServices; 2. // //#define SW ...