ASP.Net MVC+Ibaties架构
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架构的更多相关文章
- Asp.net mvc项目架构分享系列之架构概览
Asp.net mvc项目架构分享系列之架构概览 Contents 系列一[架构概览] 0.项目简介 1.项目解决方案分层方案 2.所用到的技术 3.项目引用关系 系列二[架构搭建初步] 4.项目架构 ...
- ASP.Net MVC+EF架构
ASP.Net MVC是UI层的框架,EF是数据访问的逻辑. 如果在Controller中using DbContext,把查询的结果的对象放到cshtml中显示,那么一旦在cshtml中访问关联属性 ...
- 全面解析ASP.NET MVC模块化架构方案
什么叫架构?揭开架构神秘的面纱,无非就是:分层+模块化.任意复杂的架构,你也会发现架构师也就做了这两件事. 本文将会全面的介绍我们团队在模块化设计方面取得的经验.之所以加了“全面”二字,是因为本文的内 ...
- Asp.net mvc项目架构分享系列之架构搭建初步
copy to:http://www.cnblogs.com/ben121011/p/5014795.html 项目架构各部分解析 Core Models IDAL MSSQLDAL IBLL BLL ...
- Asp.Net MVC三层架构之autofac使用教程
开发环境:vs2015..net4.5.2.mvc5.ef6 Autofac简介 IOC控制反转(Inversion of Control,缩写为IOC),Autofac是一个开源的依赖注入框架,Au ...
- asp.net mvc 项目架构解析
请先看框架图: 从上图可知: 1.Controller控制器只是充当了管道的作用.只做任务的分发,不做请求中的具体业务处理. 2.Views视图充当了展示数据的作用.不做任何取数逻辑的处理,只是展示逻 ...
- Asp.Net Mvc通用后台管理系统,bootstrap+easyui+权限管理+ORM
产品清单: 1.整站源码,非编译版,方便进行业务的二次开发 2.通用模块与用户等基础数据的数据库脚本 3.bootstrap3.3.1 AceAdmin模板源码 4.easyui1.3.5源码 5.F ...
- 《ASP.NET MVC 5 框架揭秘》
<ASP.NET MVC 5 框架揭秘> 基本信息 作者: 蒋金楠 出版社:电子工业出版社 ISBN:9787121237812 上架时间:2014-8-1 出版日期:2014 年8月 开 ...
- MVC模块化架构
全面解析ASP.NET MVC模块化架构方案 什么叫架构?揭开架构神秘的面纱,无非就是:分层+模块化.任意复杂的架构,你也会发现架构师也就做了这两件事. 本文将会全面的介绍我们团队在模块化设计方面取得 ...
随机推荐
- Qt——父对象、布局
设置父对象两个好处:(1)加入析构树(2)和父对象一起显示 设置布局后,子控件自动被设置父对象 设置父对象两个好处:(1)加入析构树(2)和父对象一起显示
- 第五节 Go数据结构之队列
一.什么是队列 数据结构里的队列就是模仿现实中的排队.如上图中狗狗排队上厕所,新来的狗狗排到队伍最后,最前面的狗狗撒完尿走开,后面的跟上.可以看出队列有两个特点: (1) 新来的都排在队尾: (2) ...
- 慎用静态类static class
偶然翻到一篇有趣的帖子: class - When to Use Static Classes in C# - Stack Overflowhttp://stackoverflow.com/quest ...
- 成都Uber优步司机奖励政策(1月23日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Calendar 实现日历实例
import java.text.ParseException; import java.util.Calendar; import java.util.GregorianCalendar; impo ...
- H5-基础-day01
类选择器和ID选择器 相同点:可以应用于任何元素不同点: 1.ID选择器只能在文档中使用一次.与类选择器不同,在一个HTML文档中,ID选择器只能使用一次,而且仅一次.而类选择器可以使用多次. 2 ...
- JavaWeb(二)——Tomcat服务器(一)
一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. 如果想修改Tomcat服务器的启动端口,则可以在server.xml ...
- GIt学习第一天之安装和版本库创建
搬运自 ‘廖雪峰的官方网站’ 1.git安装 官网下载地址:https://git-scm.com/download/win 百度网盘下载地址:https://pan.baidu.com/s/1k ...
- Git与远程仓库关联以及关联错误解决方法
假设你github的用户名是 helloworld ,你在上面创建了一个 名为 hello 的 repository. 一. 与本地仓库进行关联 1.1用原生ssh进行关联,速度快: git re ...
- python编程os、os.path 模块中关于文件、目录常用的函数使用方法
os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名( ...