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. python3.5 安装twisted

    https://blog.csdn.net/caimouse/article/details/77647952 下载地址:http://www.lfd.uci.edu/~gohlke/pythonli ...

  2. 常用前端UI框架

  3. html中iframe根据子页面内容动态修改高度

    JavaScript var browserVersion = window.navigator.userAgent.toUpperCase(); var isOpera = browserVersi ...

  4. Hough变换-理解篇

    Hough变换-理解篇 霍夫变换(Hough Transform)是图像处理中的一种特征提取技术,它通过一种投票算法检测具有特定形状的物体.该过程在一个参数空间中通过计算累计结果的局部最大值得到一个符 ...

  5. 4946: [Noi2017]蔬菜

    4946: [Noi2017]蔬菜 http://www.lydsy.com/JudgeOnline/upload/Noi2017D2.pdf 分析: 贪心. 首先可以将一个蔬菜拆成两个,一个是有加成 ...

  6. VINS(八)初始化

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

  7. JS日期转换

    用js将从后台得到的时间戳(毫秒数)转换为想要的日期格式 得到后台从数据库中拿到的数据我们希望格式是 2016年10月25日 17时37分30秒 或者 2016/10/25 17:37:30 然而我们 ...

  8. centos7系统配置系统用户基于ssh的google身份验证

    最近也是服务器各种被入侵,所以在安全上,要万分注意,特此记录,借助google的身份验证插件,获取动态验证码完成ssh登陆. OS: centos7 安装配置: 1. 安装epel源 yum -y i ...

  9. 「专题训练」Boredom(CodeForces Round #260 Div.1 A)

    题意(Codeforces-455A) 给你\(n\)个数,你每次可以选择删除去一个数\(x\)获得\(x\)分,但是所有为\(x+1\)和\(x-1\)的数都得删去.问最大获得分数. 分析 这是一条 ...

  10. 测试开发的成长之路 - 自动化一站式平台(UI、接口)

    前言 在自动化测试过程中,随着对接的自动化需求不断增加,测试用例数量显著上升,参与自动化测试的人也越来越多,多人协作就会碰到很多问题,包括脚本.数据.版本.项目整合.持续集成等,而且也增加了后期维护的 ...