1.配置Ibaties首先在DLL引用中添加Ibaties相关引用:IBatisNet.Common.dll;IBatisNet.Common.Logging.Log4Net.dll;IBatisNet.DataMapper.dll

2.添加providers.config、sqlmap.config配置文件

providers.config主要为数据库驱动

sqlmap.config主要为数据库配置和路由映射,配置信息如下

<?xml version="1.0" encoding="utf-8"?>
<sqlMapConfig
xmlns="http://ibatis.apache.org/dataMapper"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <settings>
<!--Mapping是否启用namespace-->
<setting useStatementNamespaces="true"/>
<!--Mapping是否缓存-->
<setting cacheModelsEnabled="false"/>
</settings> <providers resource="providers.config"/> <!-- Database connection information -->
<database>
<provider name="sqlServer2005"/>
<dataSource name="iBatisNet" connectionString="Max Pool Size = 512;Data Source=.;
Initial Catalog=test;
User ID=sa;password=!test;Max Pool Size = 512;connect timeout = 20; "/>
</database> <sqlMaps>
<sqlMap resource="Mappings/LocalDB/TestMapping.xml"></sqlMap>
</sqlMaps> </sqlMapConfig>

3.在Mappings/LocalDB文件夹下建立映射XML文件TestMapping.xml,相关信息如下:

<sqlMap namespace="TestMapping" xmlns="http://ibatis.apache.org/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <statements>
<select id="GetByid" resultClass="System.Data.DataSet">
select * from test where id=#value#
</select> <update id="UpdateNum" parameterClass="Hashtable">
update test
set num = #num#,
where id = #id#
</update> <insert id="CreatePurchaseOrder" parameterClass="Hashtable" >
INSERT INTO test
(
Num
)
VALUES
(
#Num#
)
<selectKey resultClass="int" type="post" property="id" >
select @@IDENTITY as value
</selectKey>
</insert> </statements> </sqlMap>

4.实例化SqlMap,对数据库进行操作

IBaseDAL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections; public interface IBaseDAL
{
/// <summary>
/// 插入
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="statementName"></param>
/// <param name="t"></param>
/// <returns></returns>
int Insert<T>(string statementName, T t); int Update<T>(string statementName, T t); int Delete(string statementName, int primaryKeyId);
int Delete(string statementName, string primaryKeyId);
int Delete(string statementName, object parameterObject); T Get<T>(string statementName, int primaryKeyId) where T : class; T Get<T>(string statementName, string primaryKeyId) where T : class; T Get<T>(string statementName, object parameterObject) where T : class; object Get(string statementName, object parameterObject); IList<T> QueryForList<T>(string statementName, object parameterObject = null); int Add<T>(string statementName, T t); /// <summary>
/// 获取DataTable主要针对存储过程
/// </summary>
/// <param name="statementName"></param>
/// <param name="paramObject"></param>
/// <param name="dictParam"></param>
/// <param name="dictParamDirection"></param>
/// <param name="htOutPutParameter"></param>
/// <returns></returns>
DataTable QueryForDataTable(string statementName, object paramObject, IDictionary dictParam, IDictionary<string, ParameterDirection> dictParamDirection, out Hashtable htOutPutParameter); /// <summary>
/// 获取DataTable
/// </summary>
/// <param name="statementName"></param>
/// <param name="paramObject"></param>
/// <returns></returns>
DataTable QueryForDataTable(string statementName, object paramObject); }

BaseDAL:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBatisNet.DataMapper;
using System.Data;
using IBatisNet.Common;
using IBatisNet.DataMapper.Configuration.Statements;
using IBatisNet.DataMapper.MappedStatements;
using IBatisNet.DataMapper.Scope;
using System.Collections; public class BaseDAL : IBaseDAL
{
ISqlMapper sqlMapper; public BaseDAL()
{ sqlMapper = Mapper.Instance();
} public BaseDAL(ISqlMapper map)
{
sqlMapper = map;
} public int Insert<T>(string statementName, T t)
{
if (sqlMapper != null)
{
IDbCommand cmd = GetDbCommand(statementName, t);//SQL text command
object obj = sqlMapper.Insert(statementName, t);
if (obj != null)
{
return (int)obj;
}
else
return ;
}
return ;
} public int Add<T>(string statementName, T t)
{
if (sqlMapper != null)
{
return sqlMapper.Update(statementName, t);
}
return ;
} public int Update<T>(string statementName, T t)
{
if (sqlMapper != null)
{
IDbCommand cmd = GetDbCommand(statementName, t);//SQL text command
return sqlMapper.Update(statementName, t);
}
return ;
} public int Delete(string statementName, int primaryKeyId)
{
if (sqlMapper != null)
{
return sqlMapper.Delete(statementName, primaryKeyId);
}
return ;
} public int Delete(string statementName, string primaryKeyId)
{
if (sqlMapper != null)
{
return sqlMapper.Delete(statementName, primaryKeyId);
}
return ;
} public int Delete(string statementName, object parameterObject)
{
if (sqlMapper != null)
{ return sqlMapper.Delete(statementName, parameterObject);
}
return ;
} public T Get<T>(string statementName, int primaryKeyId) where T : class
{
if (sqlMapper != null)
{
return sqlMapper.QueryForObject<T>(statementName, primaryKeyId);
}
return null;
} public T Get<T>(string statementName, string primaryKeyId) where T : class
{
if (sqlMapper != null)
{
return sqlMapper.QueryForObject<T>(statementName, primaryKeyId);
}
return null;
} public T Get<T>(string statementName, object parameterObject) where T : class
{
if (sqlMapper != null)
{
IDbCommand cmd = GetDbCommand(statementName, parameterObject);//SQL text command
return sqlMapper.QueryForObject<T>(statementName, parameterObject);
}
return null;
} public object Get(string statementName, object parameterObject)
{
if (sqlMapper != null)
{
IDbCommand cmd = GetDbCommand(statementName, parameterObject);//SQL text command
return sqlMapper.QueryForObject(statementName, parameterObject);
}
return null;
} public IList<T> QueryForList<T>(string statementName, object parameterObject = null)
{
if (sqlMapper != null)
{
IDbCommand cmd = GetDbCommand(statementName, parameterObject);//SQL text command
return sqlMapper.QueryForList<T>(statementName, parameterObject);
}
return null;
} /// <summary>
/// 通用的以DataTable的方式得到Select的结果(xml文件中参数要使用$标记的占位参数)
/// </summary>
/// <param name="statementName">语句ID</param>
/// <param name="paramObject">语句所需要的参数</param>
/// <returns>得到的DataTable</returns>
public DataTable QueryForDataTable(string statementName, object paramObject)
{
DataSet ds = new DataSet();
bool isSessionLocal = false; IDalSession session = sqlMapper.LocalSession;
if (session == null)
{
session = new IBatisNet.DataMapper.SqlMapSession(sqlMapper);
session.OpenConnection();
isSessionLocal = true;
} IDbCommand cmd = GetDbCommand(statementName, paramObject);//SQL text command try
{
cmd.Connection = session.Connection;
IDbDataAdapter adapter = session.CreateDataAdapter(cmd);
adapter.Fill(ds);
}
finally
{
if (isSessionLocal)
{
session.CloseConnection();
}
} return ds.Tables[]; } private IDbCommand GetDbCommand(string statementName, object paramObject)
{
IStatement statement = sqlMapper.GetMappedStatement(statementName).Statement; IMappedStatement mapStatement = sqlMapper.GetMappedStatement(statementName); ISqlMapSession session = new SqlMapSession(sqlMapper);
if (sqlMapper.LocalSession != null)
{
session = sqlMapper.LocalSession;
}
else
{
session = sqlMapper.OpenConnection();
} RequestScope request = statement.Sql.GetRequestScope(mapStatement, paramObject, session); mapStatement.PreparedCommand.Create(request, session as ISqlMapSession, statement, paramObject);
IDbCommand cmd = session.CreateCommand(CommandType.Text);
cmd.CommandText = request.IDbCommand.CommandText;
return cmd; } /// <summary>
/// 获取DbCommand,主要是针对存储过程
/// </summary>
/// <param name="sqlMapper"></param>
/// <param name="statementName"></param>
/// <param name="paramObject">参数</param>
/// <param name="dictParam">参数字段</param>
/// <param name="dictParmDirection">ParameterDirection字典</param>
/// <param name="cmdType"></param>
/// <returns></returns>
protected virtual IDbCommand GetDbCommand(string statementName, object paramObject, IDictionary dictParam, IDictionary<string, ParameterDirection> dictParmDirection, CommandType cmdType)
{
if (cmdType == CommandType.Text)
{
return GetDbCommand(statementName, paramObject);
} IStatement statement = sqlMapper.GetMappedStatement(statementName).Statement;
IMappedStatement mapStatement = sqlMapper.GetMappedStatement(statementName);
ISqlMapSession session = new SqlMapSession(sqlMapper); if (sqlMapper.LocalSession != null)
{
session = sqlMapper.LocalSession;
}
else
{
session = sqlMapper.OpenConnection();
} RequestScope request = statement.Sql.GetRequestScope(mapStatement, paramObject, session);
mapStatement.PreparedCommand.Create(request, session as ISqlMapSession, statement, paramObject);
IDbCommand cmd = session.CreateCommand(cmdType);
cmd.CommandText = request.IDbCommand.CommandText;
if (cmdType != CommandType.StoredProcedure || dictParam == null)
{
return cmd;
}
foreach (DictionaryEntry de in dictParam) //存储过程
{
string key = de.Key.ToString();
IDbDataParameter dbParam = cmd.CreateParameter();
dbParam.ParameterName = key;
dbParam.Value = de.Value; if (dictParmDirection != null && dictParmDirection.ContainsKey(key))
{
dbParam.Direction = dictParmDirection[key]; //ParameterDirection
}
cmd.Parameters.Add(dbParam);
}
return cmd;
} /// <summary>
/// 查询返回DataTable,对于包括OUTPUT参数的存储过程同样适用
/// </summary>
/// <param name="sqlMapper"></param>
/// <param name="statementName"></param>
/// <param name="paramObject">参数</param>
/// <param name="dictParam">参数字典</param>
/// <param name="dictParamDirection">ParameterDirection字典</param>
/// <param name="htOutPutParameter">返回的Output参数值哈希表</param>
/// <returns></returns>
public DataTable QueryForDataTable(string statementName, object paramObject, IDictionary dictParam, IDictionary<string, ParameterDirection> dictParamDirection, out Hashtable htOutPutParameter)
{ DataSet ds = new DataSet();
bool isSessionLocal = false;
ISqlMapSession session = sqlMapper.LocalSession;
if (session == null)
{
session = new SqlMapSession(sqlMapper);
session.OpenConnection();
isSessionLocal = true;
} IDbCommand cmd = GetDbCommand(statementName, paramObject, dictParam, dictParamDirection, CommandType.StoredProcedure); //存储过程 try
{
cmd.Connection = session.Connection;
IDbDataAdapter adapter = session.CreateDataAdapter(cmd);
adapter.Fill(ds);
}
finally
{
if (isSessionLocal)
{
session.CloseConnection();
}
}
htOutPutParameter = new Hashtable();
foreach (IDataParameter parameter in cmd.Parameters)
{
if (parameter.Direction == ParameterDirection.Output)
{
htOutPutParameter[parameter.ParameterName] = parameter.Value;
}
}
return ds.Tables[];
} }

BLL调用:

public DataTable GetTest(Hashtable hs)
{
DataTable dt = new DataTable();
IBaseDAL dal = new BaseDAL();
dt = dal.QueryForDataTable("TestMapping.GetByid", hs); return dt;
     }

至此,整个流程结束。

ASP.Net MVC+Ibaties架构的更多相关文章

  1. Asp.net mvc项目架构分享系列之架构概览

    Asp.net mvc项目架构分享系列之架构概览 Contents 系列一[架构概览] 0.项目简介 1.项目解决方案分层方案 2.所用到的技术 3.项目引用关系 系列二[架构搭建初步] 4.项目架构 ...

  2. ASP.Net MVC+EF架构

    ASP.Net MVC是UI层的框架,EF是数据访问的逻辑. 如果在Controller中using DbContext,把查询的结果的对象放到cshtml中显示,那么一旦在cshtml中访问关联属性 ...

  3. 全面解析ASP.NET MVC模块化架构方案

    什么叫架构?揭开架构神秘的面纱,无非就是:分层+模块化.任意复杂的架构,你也会发现架构师也就做了这两件事. 本文将会全面的介绍我们团队在模块化设计方面取得的经验.之所以加了“全面”二字,是因为本文的内 ...

  4. Asp.net mvc项目架构分享系列之架构搭建初步

    copy to:http://www.cnblogs.com/ben121011/p/5014795.html 项目架构各部分解析 Core Models IDAL MSSQLDAL IBLL BLL ...

  5. Asp.Net MVC三层架构之autofac使用教程

    开发环境:vs2015..net4.5.2.mvc5.ef6 Autofac简介 IOC控制反转(Inversion of Control,缩写为IOC),Autofac是一个开源的依赖注入框架,Au ...

  6. asp.net mvc 项目架构解析

    请先看框架图: 从上图可知: 1.Controller控制器只是充当了管道的作用.只做任务的分发,不做请求中的具体业务处理. 2.Views视图充当了展示数据的作用.不做任何取数逻辑的处理,只是展示逻 ...

  7. Asp.Net Mvc通用后台管理系统,bootstrap+easyui+权限管理+ORM

    产品清单: 1.整站源码,非编译版,方便进行业务的二次开发 2.通用模块与用户等基础数据的数据库脚本 3.bootstrap3.3.1 AceAdmin模板源码 4.easyui1.3.5源码 5.F ...

  8. 《ASP.NET MVC 5 框架揭秘》

    <ASP.NET MVC 5 框架揭秘> 基本信息 作者: 蒋金楠 出版社:电子工业出版社 ISBN:9787121237812 上架时间:2014-8-1 出版日期:2014 年8月 开 ...

  9. MVC模块化架构

    全面解析ASP.NET MVC模块化架构方案 什么叫架构?揭开架构神秘的面纱,无非就是:分层+模块化.任意复杂的架构,你也会发现架构师也就做了这两件事. 本文将会全面的介绍我们团队在模块化设计方面取得 ...

随机推荐

  1. linux 下的torrent下载器qBitTorrent

    BT下载利器--Qbittorrent完全攻 Ubuntu使用命令安装qBittorrent的方法 源码下载

  2. (数据科学学习手札43)Plotly基础内容介绍

    一.简介 Plotly是一个非常著名且强大的开源数据可视化框架,它通过构建基于浏览器显示的web形式的可交互图表来展示信息,可创建多达数十种精美的图表和地图,本文就将以jupyter notebook ...

  3. 【LG1527】[国家集训队]矩阵乘法

    [LG1527][国家集训队]矩阵乘法 题面 洛谷 题解 我也不知道为什么取个这样的名字... 其实就是区间\(kth\)扩展到二维 还是用整体二分搞啦,把树状数组换成二维的 其他的基本没有什么差别 ...

  4. Python:numpy中的tile函数

    在学习机器学习实教程时,实现KNN算法的代码中用到了numpy的tile函数,因此对该函数进行了一番学习: tile函数位于python模块 numpy.lib.shape_base中,他的功能是重复 ...

  5. VINS(八)初始化

    首先通过imu预积分陀螺仪和视觉特征匹配分解Fundamental矩阵获取rotationMatrix之间的约束关系,联立方程组可以求得外参旋转矩阵: 接下来会检测当前frame_count是否达到W ...

  6. python之saltstack二次开发

    一.salt的概念 salt是一个配置管理系统,能够维护预定义状态的远程节点(比如,确保指定的报被安装,指定的服务在运行).一个分布式远程执行系统,用来在远程节点(可以是单个节点,也可以是任意规则挑选 ...

  7. IO 转换流

    package TestIo; import java.io.*; /** * 转换流 */ public class TestConvertStream { public static void m ...

  8. JDBC 工具类模板c3p0

    JDBC 工具类模板 package com.itheima.sh.utils; import com.mchange.v2.c3p0.ComboPooledDataSource; import ja ...

  9. linux命令(实用!)

    本文转载自网络 1.1 shell家族 shell:命令解释器,根据输入的命令执行相应命令. 察看当前系统下有哪些shell: cat /etc/shells 察看当前系统正在使用的shell ech ...

  10. Linux命令应用大词典-第10章 Shell相关命令

    10.1 commond:抑制正常的Shell函数查找 10.2 exec:使用执行命令替换当前的shell进程 10.3 bash:GNU的Bourne-Again Shell解释器 10.4 bu ...