后台代码对iBatis配置文件中具体的sql语句的调用实现(被封装的增删改查)
2015-01-27 16:07
716 查看
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 = 0; 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 != 0) { str2 = "Statement Id: [" + mappedStatement.Statement.Id + "] Parameters: [" + builder.ToString(0, builder.Length - 2) + "]"; str3 = "Statement Id: [" + mappedStatement.Statement.Id + "] Types: [" + builder2.ToString(0, builder2.Length - 2) + "]"; } 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 = 4; 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; } } }
相关文章推荐
- mybatis03(配置文件实现一对一动态sql 的增删改查)
- 配置文件之属性文件,sql漏洞的原因,sql注入的解决办法和原理,preparedStatement的增删改查语句,
- 在xcode项目中加入.sql文件,并用代码执行sql语句,省去在程序中调用sqlite3或者fmdb的接口来调用
- Apache Cordova 安卓android上传图片或者文件 调用后台服务实现java代码 ft.upload(imageURI, uri, that.uploadSuccess, that.
- mybatis01(配置文件实现一对一的增删改查)
- C# 调用配置文件SQL语句 真2B!
- ibatis配置文件写sql语句
- codeTool代码自动生成工具(参数类sql数据增删改查语句,实体类,接口代码)
- MyBatis Generator作为maven插件自动生成增删改查代码及配置文件例子
- ibatis中SQL映射配置文件中动态SQL语句语法
- ibatis中SQL映射配置文件中动态SQL语句语法
- lua(实现代码(1)):使用lua将excel中的配置数据生成sql文件
- 显示ibatis底层sql语句的log4j配置文件
- ibatis和myBatis打印sql语句的log4j配置文件
- mysql查询语句拼接ibatis的代码生产配置文件
- IT咨询顾问:一次吐血的项目救火 java或判断优化小技巧 asp.net core Session的测试使用心得 【.NET架构】BIM软件架构02:Web管控平台后台架构 NetCore入门篇:(十一)NetCore项目读取配置文件appsettings.json 使用LINQ生成Where的SQL语句 js_jquery_创建cookie有效期问题_时区问题
- ibatis和myBatis打印sql语句的log4j配置文件
- 命令行方式使用abator.jar生成ibatis相关代码和sql语句xml文件
- 利用sql语句调用bat(批处理)+ftp实现文件远程拷贝和注入