后台代码对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 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;
        }
    }
}
时间: 2024-10-12 15:18:34

后台代码对iBatis配置文件中具体的sql语句的调用实现(被封装的增删改查)的相关文章

Ado.net中简单的DBHelper类(增删改查)

private static string connString = "server=.;database=hotel;uid=aa;pwd=123";//最好从配置文件中取出 public static SqlConnection conn = new SqlConnection(connString); //公用的增删改方法 public static bool ZhengShanGai(string sql) { bool flag = false; SqlCommand com

java程序设计课期中考试——数据库的增删改查和简单的js界面

首先是设计思路,对于数据库的增删改查,我们借助Ecilipse来进行前端和后端的编写.Ecilipse是可以进行java web项目的操作的. 前端,我们选择用使用jsp,所谓的jsp就是可以嵌入其他语言的html,各种标识语言和html语法一致,但为了在本界面检测各种信息的正确性,需要嵌入java的语句进行判断. 对于数据库的增删改查,我们使用sql语句,并且导入了相应的mysql的jar包,方便我们用java对数据库进行操作. 整体来说,我们编写使用了MVC模式(model-view-con

IOS9中联系人对象的使用及增删改查操作的函数封装

之前克服重重困难把IOS9中新的类联系人搞明白了,现在把增删改查封装成了函数,如下: 1 // 2 // ViewController.m 3 // IOS9中联系人CNContact的使用 4 // 5 // Created by mac on 16/5/6. 6 // Copyright © 2016年 mzw. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 //导入IOS9联系人模型 12 @impo

SQLite中的增删改查

虽然android提供了sql查询的封装方法,但是理解起来还是麻烦,所以我这里用sql语句来完成工作. 首先是建立一个类,继承SQLiteOpenHelper 这里面会建立一个数据库,并且初始化一个表.当然你可以建立多个数据库,等以后再自行建立表也可以 DatabaseHelper.java package com.kale.sql; import android.content.Context; import android.database.sqlite.SQLiteDatabase; im

用PHP向数据库中实现简单的增删改查(纯代码,待完善)

<?php $con = mysql_connect("localhost:3306","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $result = mysql_query("SELECT * FROM user"); echo "

asp.net中利用JSON进行增删改查中运用到的方法

//asp.net中 利用JSON进行操作, //增加: //当点击"增加链接的时候",弹出增加信息窗口,然后,在窗体中输入完整信息,点击提交按钮. //这里我们需要考虑这些:我会进行异步提交,使用jquery中的方法,$.post("网页名",JSON,callback); //JSON的写法:{"name":name,"id":id},那我们对其进行假设,比方说,表单中的textbox很多,需要我们填写的信息 //也很多,

TP框架中 数据库的增删改查

框架会用到数据库的内容,这一篇就是关于数据库的增删改查. 数据库的操作,无疑就是连接数据库,然后对数据库中的表进行各种查询,然后就是对数据的增删改的操作, 想要操作数据库,第一步必然是要:链接数据库 一.链接数据库 (1)找到模块文件夹中的Conf文件夹,然后进行编写config.php文件 我这里是这样的文件路径 (2)打开这个config.php文件,然后找到父类配置文件convention.php文件,将关于"数据库"的部分复制粘贴到config.php配置文件中(父类的conv

winform窗体(六)——DataGridView控件及通过此控件中实现增删改查

DataGridView:显示数据表,通过此控件中可以实现连接数据库,实现数据的增删改查 一.后台数据绑定:    List<xxx> list = new List<xxx>();      dataGridView1.DataSource = list;      //设置不自动生成列,此属性在属性面板中没有      dataGridView1.AutoGenerateColumns = false;      //取消加载默认选中第一行      dataGridView1

EF中的增删改查

在上一篇中简单的介绍了一下有关EF的一些概念,都是小编的一些个人理解,懂的不多,想深入学习的可以从网上查看一些其他资料. 下面紧接着上一篇所说,来从代码上看一下EF容器是如何实现与后台数据库之间的增删改查的. 1.EF包装类 什么是EF包装类呢?举个例子,我们平时用SQL语句写增删改查时,用的都是一些Insert.Update.Delete等语句来实现增删改查,所以我们把放到EF容器的东东也要做一个标签,来指明这个东东是要添加.更新.还是要删除呢. 正如上图所示就是这么简单的操作,就完成了对数据