后台代码对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 ...
随机推荐
- Android画一条横线
<View android:layout_width="match_parent" android:layout_height="1px" android ...
- springMvc配置编码过滤器
在web.xml中配置 <!-- 编码过滤器 --> <filter> <filter-name>characterEncodingFilter</filte ...
- 采购订单限价(包含阶梯价)ME_PROCESS_PO_CUST
为了能管控到钱财的每个环节,公司要求采购订单需要使用物料限价,当有报价低于先前的价格时,在采购订单保存时,更新最低限价 BADI:ME_PROCESS_PO_CUST process~item里做限价 ...
- IDOC创建、发送、接收及程序代码[转]
什么是IDOC,以及IDOC的步骤 创建IDOC: 第一步:WE31 创建IDOC所包含的字段. 第二步:WE30 创建IDOC 把Segment分配给IDOC 第三步:WE81 创 ...
- Codeforces Round #313 (Div. 2) C. Gerald's Hexagon
C. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- jQuery_pager.js分页
在做前端项目中,总是需要自己手写类似于这样的分页效果: 这就需要使用jQuery.pager.js文件,其使用方法为:在html中引入三个文件,分别为: <link rel="styl ...
- PDF 补丁丁 0.4.1.839 测试版发布:调整页面留白
新的测试版的补丁功能实现了调节页面留白的功能(之前的820版尚未实现该功能),页面合并功能支持从资源管理器拖放文件或目录到列表,还修正了一些问题. 欢迎下载测试.
- OpenLDAP使用疑惑解答及使用Java完成LDAP身份认证
导读 LDAP(轻量级目录访问协议,Lightweight Directory Access Protocol)是实现提供被称为目录服务的信息服务.目录服务是一种特殊的数据库系统,其专门针对读取,浏览 ...
- 关于职位的解释---转CSDN的文章
摘要我在IT职场打滚超过15年了,从小小的程序员做到常务副总.相对于其它行业,IT职场应该算比较光明的了,但也陷阱重重,本文说说我的亲身体会,希望大家能在IT职场上战无不胜! 通用法则 法则1:忍耐是 ...
- JS引用类型之——数组
前言 数组作为JS中非常常用的引用类型,其功能是非常强大滴,今天小猪就彻底的看了下它.为了防止猪脑子不够用所以记录在案呐 1.数组的创建 var arrayObj = new Array(); //创 ...