Programming with iBATIS DataMapper: The .NET API

DataMapper API 提供四个核心方法

    1. build a SqlMapper instance from a configuration file

    2. execute an update query (including insert and delete).

    3. execute a select query for a single object

    4. execute a select query for a list of objects

4.4.1. Building a SqlMapper Instance

[C#]
using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;

namespace IBatisNet.DataMapper
{
 public class Mapper
 {
  private static volatile ISqlMapper _mapper = null;

  protected static void Configure (object obj)
  {
   _mapper = null;
  }

  protected static void InitMapper()
  {
   ConfigureHandler handler = new ConfigureHandler(Configure);
   DomSqlMapBuilder builder = new DomSqlMapBuilder();
   _mapper = builder.ConfigureAndWatch(handler);
  }

  public static ISqlMapper Instance()
  {
   if (_mapper == null)
   {
    lock (typeof (SqlMapper))
    {
     if (_mapper == null) // double-check
     { 
      InitMapper();
     }
    }
   }
   return _mapper;
  }
  
  public static ISqlMapper Get()
  {
   return Instance();
  }
 }
}
View Code

程序中使用 ISqlMapper实例时,只需调用ISqlMapper mapper = Mapper.Instance();

DomSqlMapBuilder 加载SqlMap.config创建SqlMapper实例。DomSqlMapBuilder.ConfigureAndWatch()方法监视配置文件,有修改自动重新加载配置,而不需要编译,重启程序。(动态重载需要配置文件能被文件系统访问)

4.4.1.1. Multiple Databases

如果一个程序中需要访问多个数据库,创建新的SqlMapp.config和Mapper 类,用来创建另一个ISqlMapper实例。

ISqlMapper sqlServer = SqlServerMapper.Get();
ISqlMapper access = AccessMapper.Get();

4.4.1.2. DomSqlMapBuilder Configuration Options

配置文件设置相对路径或绝对路径

1. SqlMapper Configuration through an absolute or relative file path
/* Configure a SqlMapper from a file path.
   Uses a relative resource path from your application root 
   or an absolute file path such as "file:\\c:\dir\a.config" */
ISqlMapper mapper = builder.Configure(strPath);

mapper = builder.Configure(ConfigurationSettings.AppSettings["rootPath"]+"SqlMap.config");

2. SqlMapper Configuration with a FileInfo or Uri instance
/* Configure a SqlMapper with FileInfo. */
FileInfo aFileInfo = someSupportClass.GetDynamicFileInfo();
ISqlMapper mapper = builder.Configure(aFileInfo);

/* Configure a SqlMapper through a Uri. */
Uri aUri = someSupportClass.GetDynamicUri();
ISqlMapper anotherMapper = builder.Configure(aUri);

3.SqlMapper Configuration with an XmlDocument or Stream
/* Configure a SqlMapper with an XmlDocument */
XmlDocument anXmlDoc = someSupportClass.GetDynamicXmlDocument();
ISqlMapper mapper = builder.Configure(anXmlDoc);

/* Configure a SqlMapper from a stream. */
Stream aStream = someSupportClass.GetDynamicStream();
ISqlMapper anotherMapper = builder.Configure(aStream);
View Code

Mapper ConfigureHandler delegate

...
  protected static void Configure (object obj)
  {
   _mapper = null;
  }

  protected static void InitMapper()
  {
   ConfigureHandler handler = new ConfigureHandler(Configure);
   DomSqlMapBuilder builder = new DomSqlMapBuilder();
   _mapper = builder.ConfigureAndWatch(handler);
  }
...
View Code

DomSqlMapBuilder 传递一个委托ConfigureHandler 用来重置应用程序的SqlMapper实例。

4.4.1.3. DomSqlMapBuilder : Advanced settings

4.4.2. Exploring the DataMapper API through the SqlMapper

4.4.2.1. Insert, Update, Delete

public object Insert(string statementName,
                     object parameterObject);
public int Update(string statementName,
                  object parameterObject);
public int Delete(string statementName,
                  object parameterObject);
View Code

<insert> 通过嵌套<selectKey>返回自动生成的主键,不设置<selectKey>返回null

update和insert 返回受影响行数

4.4.2.2. QueryForObject

public object QueryForObject(string statementName,
                             object parameterObject);
public object QueryForObject(string statementName, 
                             object parameterObject,
                             object resultObject);

public T QueryForObject<T>(string statementName, object parameterObject);
public T QueryForObject<T>(string statementName, object parameterObject, T resultObject);
View Code

4.4.2.3. QueryForList

public IList QueryForList(string statementName,
                          object parameterObject);
public void QueryForList(string statementName,
                         object parameterObject,
                         IList resultObject);
public IList QueryForList(string statementName,
                          object parameterObject,
                          int skipResults,
                          int maxResults);

public IList<T> QueryForList<T>(string statementName, object parameterObject);
public void QueryForList<T>(string statementName, 
                                  object parameterObject, 
                                  IList<T> resultObject);
public IList<T> QueryForList<T>(string statementName, 
                                            object parameterObject,
                                            int skipResults, int maxResults);                          
                          
View Code

4.4.2.4. QueryWithRowDelegate

public delegate void RowDelegate(object obj,
                                 IList list);

public IList QueryWithRowDelegate(string statementName,
                                  object parameterObject, 
                                  RowDelegate rowDelegate);

public IList<T> QueryWithRowDelegate<T>(string statementName, object parameterObject, 
                                SqlMapper.RowDelegate<T> rowDelegate);    
View Code

public delegate void RowDelegate(object obj, object parameterObject, IList list); //obj结果集的每一行,object传入参数,list结果集,在遍历每一行的时候将结果添加到list,不添加就不返回

4.4.2.5.  QueryForMapWithRowDelegate

public delegate void DictionaryRowDelegate(object key, 
                                           object value, 
                                           object parameterObject, 
                                           IDictionary dictionary);

public IDictionary QueryForMapWithRowDelegate(string statementName,
                                              object parameterObject, 
                                              string keyProperty, 
                                              string valueProperty, 
                                              DictionaryRowDelegate rowDelegate);
View Code

4.4.2.6. QueryForPaginatedList

The method PaginatedList has been made as obsolete and will not be supported in future version.

4.4.2.7. QueryForDictionary, QueryForMap

public IDictionary QueryForDictionary(string statementName,
                                      object parameterObject,
                                      string keyProperty)
public IDictionary<K, V> QueryForDictionary<K, V>(string statementName, 
                                      object parameterObject, 
                                      string keyProperty);
public IDictionary QueryForDictionary(string statementName,
                                      object parameterObject,
                                      string keyProperty,
                                      string valueProperty)
public IDictionary<K, V> QueryForDictionary<K, V>(string statementName, 
                                     object parameterObject, 
                                      string keyProperty, 
                                      string valueProperty);

public IDictionary QueryForMap(string statementName,
                               object parameterObject, 
                               string keyProperty)
public IDictionary QueryForMap(string statementName, 
                               object parameterObject, 
                               string keyProperty, 
                               string valueProperty)
View Code

keyProperty:指定该列值为字典的key,指定了valueProperty则字典value为valueProperty对应列的值,如果不指定valueProperty则字典value为行实体对象。

 

posted @ 2020-03-29 02:01  vvf  阅读(158)  评论(0编辑  收藏  举报