iBATIS.NET多數(shù)據(jù)庫支持淺析
談到iBATIS.NET多數(shù)據(jù)庫支持我們首先來看看它本身的幫助文檔,在iBATIS.NET的幫助文檔中有介紹多數(shù)據(jù)庫支持,但是沒有寫全代碼,后來查看其源碼,并結合幫助文檔,找到了解決方法,其實道理就是另行實現(xiàn)一個Mapper.
iBATIS.NET多數(shù)據(jù)庫支持實例如AnthorMapper:
- Apache Notice#region Apache Notice
- #endregion
- using IBatisNet.Common.Utilities;
- using IBatisNet.DataMapper;
- using IBatisNet.DataMapper.Configuration;
- namespace IBatisNet.DataMapper
- {
- /**//// ﹤summary﹥
- /// A singleton class to access the default SqlMapper defined by the SqlMap.Config
- /// ﹤/summary﹥
- public sealed class AnthorMapper
- {
- Fields#region Fields
- private static volatile ISqlMapper _mapper = null;
- #endregion
- /**//// ﹤summary﹥
- ///
- /// ﹤/summary﹥
- /// ﹤param name="obj">﹤/param﹥
- public static void Configure (object obj)
- {
- _mapper = null;
- }
- /**//// ﹤summary﹥
- /// Init the 'default' SqlMapper defined by the SqlMap.Config file.
- /// ﹤/summary﹥
- public static void InitMapper()
- {
- ConfigureHandler handler = new ConfigureHandler (Configure);
- DomSqlMapBuilder builder = new DomSqlMapBuilder();
- _mapper = builder.ConfigureAndWatch ("AnthorMap.config",handler); }
- /**//// ﹤summary﹥
- /// Get the instance of the SqlMapper defined by the SqlMap.Config file.
- /// ﹤/summary﹥
- /// ﹤returns>A SqlMapper initalized via the SqlMap.Config file.﹤/returns﹥
- public static ISqlMapper Instance()
- {
- if (_mapper == null)
- {
- lock (typeof (SqlMapper))
- {
- if (_mapper == null) // double-check
- {
- InitMapper();
- }
- }
- }
- return _mapper;
- }
- /**//// ﹤summary﹥
- /// Get the instance of the SqlMapper defined by the SqlMap.Config file. (Convenience form of Instance method.)
- /// ﹤/summary﹥
- /// ﹤returns>A SqlMapper initalized via the SqlMap.Config file.﹤/returns﹥
- public static ISqlMapper Get()
- {
- return Instance();
- }
- }
- }
以上代碼只是修改了iBATIS.NET中的Mapper的代碼,將_mapper = builder.ConfigureAndWatch (handler);修改為_mapper = builder.ConfigureAndWatch ("AnthorMap.config",handler),就是根據(jù)另一個AnthorMap.config文件來生成SqlMapper。
AnthorMap.config和默認的SqlMap.config一樣,只是根據(jù)你的數(shù)據(jù)不同設置不同而已,測試AnthorMap.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﹥
- ﹤setting useStatementNamespaces="true"/﹥
- ﹤/settings﹥
- ﹤providers resource="ServerConfig/providers.config"/﹥
- ﹤!-- Database connection information --﹥
- ﹤database﹥
- ﹤provider name="sqlServer2.0"/﹥
- ﹤dataSource name="CrmSystem" connectionString="server=.;database=TestDB;uid=sa;pwd="/﹥
- ﹤/database﹥
- ﹤sqlMaps﹥
- ﹤sqlMap embedded="Test.Domain.Weather.xml,Test.Domain" /﹥
- ﹤/sqlMaps﹥
- ﹤/sqlMapConfig﹥
iBATIS.NET多數(shù)據(jù)庫支持之使用AntherMapper來創(chuàng)建ISqlMapper了。如下:
- public IList﹤Weather﹥GetWeather()
- {
- ISqlMapper map = AnthorMapper.Instance();
- return map.QueryForList﹤Weather>("Weather.Select", null);
- }
那么iBATIS.NET多數(shù)據(jù)庫支持就介紹到這里,希望這樣的介紹對你有幫助。
【編輯推薦】