IBatis.net做用是把数据库查询与对象的属性间创建映射关系。但它并非一个实体关系映射工具,仅用于帮助程序人员创建实体和SQL语句或者存储过程间的映射。所以只能叫半自动OR/M工具。咱们的开票系统也用到了这个工具。sql
IBatis.net的配置:数据库
1、引用几个DLL,注意在数据层引用便可。apache
单独使用映射的状况下,只须要引用IBatisNet.DataMapper.dll就能够了缓存
其中IBatisNet.Common.dll是必须的,Entities是项目本身的,不用管它app
2、完成对组件的添加后,还须要添加三个XML文档ide
1 providers.config ----DataMapper根据这个肯定是什么类型数据库(放在数据层)工具
2 SqlMap.xml ----数据映射文档,里面包含了SQL语句(放在数据层)ui
3 SqlMap.config ----DataMapper的配置文档,它详细描述了工程中SqlMap.XML和providers.config文档的位置,以及其余配置项(必须放在Web跟目录下)。spa
先看SqlMap.config(这个文件的做用主要是指定db链接串,告诉系统providers.config在哪? 以及db与entity的映射文件在哪?):.net
<?xml version="1.0" encoding="utf-8"?> <sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <settings> <setting useStatementNamespaces="true"/> //若是是true,那么写数据查询时,查询语句的名称前要添加它的完整命名空间 <setting cacheModelsEnabled="true"/> //全局化配置DataMapper客户可否启用cache,默认是true <setting validateSqlMap="false"/> </settings> <providers embedded="ICSON.InvoicePrinter.SqlProviderImpl.Config.providers.config,ICSON.InvoicePrinter.SqlProviderImpl"/>
//指定providers.config的位置
<database> <provider name="sqlServer2.0" /> //若是使用默认的数据提供者,这句能够不要,若是系统中使用多个数据库,须要配置provider的内容 <dataSource name="SqlServer" connectionString="${ConnectionString}" /> //数据库连接字符串,咱们的项目在IninMapper时,会给这个变量赋值 </database> <sqlMaps> //程序的数据映射文件的位置,若是有多个XML,就写多行,若是比较多,也能够当一个单独的XML中去写,好比<sqlMap resource=”Maps/All.XML”/>,而后在ALL.xml再添加数据映射文件,这样就 实现了加载一组数据映射文件
<sqlMap resource="Maps/KeyValueEntity.xml"/> <sqlMap resource="Maps/InvoiceCompany.xml"/> <sqlMap resource="Maps/InvoiceTemplate.xml"/> <sqlMap resource="Maps/InvoiceDistribute.xml"/> <sqlMap resource="Maps/BlankInvoiceInventory.xml"/> <sqlMap resource="Maps/InvoiceMachine.xml" /> <sqlMap resource="Maps/Order.xml"/> <sqlMap resource="Maps/SoPrint.xml"/> <sqlMap resource="Maps/SoPrintItem.xml"/> <sqlMap resource="Maps/Invoice.xml"/> <sqlMap resource="Maps/Log.xml"/> <sqlMap resource="Maps/InvoiceUser.xml"/> <sqlMap resource="Maps/OrderOutStockMaster.xml"/> <sqlMap resource="Maps/Batch.xml"/> <sqlMap resource="Maps/OrderBatch.xml"/> </sqlMaps> </sqlMapConfig>
3、建立SqlMapper实例,这个是单例模式,文件名ProviderBase.cs,放在数据层中。
using System; using IBatisNet.Common.Utilities; using IBatisNet.DataMapper; using IBatisNet.DataMapper.Configuration; using ICSON.Utility; using System.Configuration; using System.Collections.Specialized; namespace ICSON.InvoicePrinter.SqlProviderImpl { /// <summary> /// 数据访问配置类。 /// </summary> public class ProviderBase { private static volatile ISqlMapper _mapper; private ProviderBase() { } public static ISqlMapper Instance() { if (_mapper == null) { lock (typeof(SqlMapper)) { if (_mapper == null) // double-check { InitMapper(); } } } return _mapper; } /// <summary> /// Init the 'default' SqlMapper defined by the SqlMap.Config file. /// </summary> public static void InitMapper() { try { var handler = new ConfigureHandler(Configure); var builder = new DomSqlMapBuilder(); var connection = ConfigurationManager.ConnectionStrings[Config.DefaultConnectionName]; if (connection == null) throw new ConfigurationErrorsException("缺乏数据库链接配置(" + Config.DefaultConnectionName + ")"); var properties = new NameValueCollection { {"ConnectionString",connection.ConnectionString} }; builder.Properties = properties; _mapper = builder.ConfigureAndWatch(Config.BasePath + "sqlmap.config", handler); } catch (Exception e) { Log.Error("sqlmap.config配置错误:"+e.ToString()); throw; } } public static void Configure(object obj) { _mapper = null; } public static ISqlMapper Get() { return Instance(); } } }
外界调用方法:
IList list = SqlMapper.Instance().QueryForObject<list>("UserInfo.GetCount", param);
由于是单例的,第一次调用时,DomSqlMapBuilder对象会经过查询SqlMap.config来建立一个sqlMapper实例,之后调用就直接从缓存读取了。
DomSqlMapBuilder.ConfigureAndWatch()方法负责监视配置文件的更新状况,若是配置或者映射文件有更新,SqlMapper对象会从新载入并不重启系统。
咱们的项目创建了一个SqlMap访问层基类,文件名是 BaseSqlMapDao.cs
那么这个基类完成什么工做呢,见下一篇博客吧。