最近想学习 ibatisnet + castle ,并想在项目中加以应用,看了网上许多资料,本以为瞒简单的,
谁想自己动手时还是费了不少功夫的 :) ,先将自己的学习心得和自己建的项目贴出来,供大家分享和研究,
我也是初学,写的有不对的地方还望大家多多指教哈.
1. 下载castle 和 ibatisnet 的 dll文件,
注意:目前为止,castle版本最高支持 ibatisnet 的 datamapper的版本是1.5,dataaccess 版本1.8
所以需下载castle 1.0 和 ibatyisnet的 1.5 版本
2. 框架 模仿 npetshop2 的架构 并且稍做了简化,去掉了其页面采用castle控制的部分,具体框架如下:
ATS.Domain 系统领域,所有数据库实体的对象,数据载体,在其他各个层都要用到
ATS.Persistence 持久层,持久化数据到数据库,其中包括 interface(接口) 和 mapperDao(实现)
ATS.Service 服务层,系统用到的所有业务方法,调用 ATS.Persistence层,其中包括 Implement(实现) 和 Interface(接口)
ATS.Test 测试层 ,用 NUnit 测试
ATS.Web 页面层(最终与用户交互)
ATS.Common 系统公共层,公用方法和类

3. 首先需要配置 web.config 文件, 在<configuration>节点下新增 如下节点供castle使用
<?
xml version="1.0"
?>
<!--
注意: 除了手动编辑此文件以外,您还可以使用
Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
“网站”->“Asp.Net 配置”选项。
设置和注释的完整列表在
machine.config.comments 中,该文件通常位于
WindowsMicrosoft.NetFrameworkv2.xConfig 中
-->
<
configuration
>
<
configSections
>
<
section
name
="castle"
type
="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"
/>
</
configSections
>

<
castle
>
<!--
扩展castle 将 ibatisnet 当作插件和 castle 相结合
-->
<
include
uri
="file://facilities.config"
/>
<!--
此文件告诉castel 哪个接口 由 哪个类 实现
-->
<
include
uri
="file://services.config"
/>
</
castle
>
<
appSettings
/>
<
connectionStrings
/>
<
system
.web
>
<!--
设置 compilation debug="true" 将调试符号插入
已编译的页面中。但由于这会
影响性能,因此只在开发过程中将此值
设置为 true。
-->
<
compilation
debug
="true"
/>
<!--
通过 <authentication> 节可以配置 ASP.NET 使用的
安全身份验证模式,
以标识传入的用户。
-->
<
authentication
mode
="Windows"
/>
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
开发人员通过该节可以配置
要显示的 html 错误页
以代替错误堆栈跟踪。

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</
system.web
>
</
configuration
>
使用以上配置时,需要将所有配置文件默认放在web应用程序的根目录下,castle 会自动从 web.config中读取
4. 配置 facilities.config 文件 (程序根目录下)
<?
xml version="1.0" encoding="utf-8"
?>
<
configuration
>

<
facilities
>
<!--
为Castle 扩展 ibatisnet
-->
<
facility
id
="ibatis"
type
="Castle.Facilities.IBatisNetIntegration.IBatisNetFacility, Castle.Facilities.IBatisNetIntegration"
>
<
sqlMap
id
="sqlServerSqlMap"
config
="sqlMap.config"
/>
</
facility
>
<!--
为Castle 扩展 事务处理功能
-->
<
facility
id
="transaction"
type
="Castle.Facilities.AutomaticTransactionManagement.TransactionFacility, Castle.Facilities.AutomaticTransactionManagement"
/>

</
facilities
>
</
configuration
>
这样 castle 就可以自动将 ibatisnet 管理起来了,需要ibatisnet的时候会自动运行 ibatisnet
5. 配置 services.config 文件
<?
xml version="1.0" encoding="utf-8"
?>
<
configuration
>
<
components
>
<
component
id
="Employees"
service
="ATS.Service.Interface.IEmployeesService, ATS.Service"
type
="ATS.Service.Implement.EmployeesService, ATS.Service"
/>

</
components
>
</
configuration
>
可以发现: 服务(接口) IEmployeesService 由 EmployeesService 类实现 ,castle 会自动装配,
即在请求需要调用IEmployeesService 接口中的方法时,会自动实例化EmployeesService类,并调用其中相应的方法
6. 编写ibatisnet 的配置文件 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
>
<
setting
useStatementNamespaces
="false"
/>
</
settings
>

<!--
database providers
-->
<
providers
resource
="providers.config"
/>

<!--
Database connection information
-->
<
database
>
<
provider
name
="sqlServer2.0"
/>
<
dataSource
name
="DocumentSystem"
connectionString
="server=.SQLExpress;database=Northwind;uid=sa;pwd="
/>
</
database
>

<
sqlMaps
>
<!--
设置sqlmap,所有实体的数据库操作
-->
<
sqlMap
resource
="../ATS.Persistence/Maps/Employees.xml"
/>
</
sqlMaps
>

</
sqlMapConfig
>
以上设置 ibatisnet 对实体进行数据库操作的 sqlmap.
7. 设置 dao.config 文件
<?
xml version="1.0" encoding="utf-8"
?>
<
daoConfig
xmlns
="http://ibatis.apache.org/dataAccess"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
>
<!--
Optional if resource
-->

<
providers
resource
="providers.config"
/>

<
context
id
="SqlMapDao"
default
="true"
>


<!--
==== Sql Server : SqlClient configuration =========
-->


<
database
>
<
provider
name
="sqlServer2.0"
/>
<
dataSource
name
="Airfares"
connectionString
="server=.SQLExpress;database=Northwind;user id=sa;password=;"
/>
</
database
>

<
daoSessionHandler
id
="SqlMap"
>
<
property
name
="resource"
value
="sqlMap.config"
/>
</
daoSessionHandler
>

<
daoFactory
>
<
dao
interface
="ATS.Persistence.Interface.IEmployeesDao, ATS.Persistence"
implementation
="ATS.Persistence.MapperDao.EmployeesSqlMapDao, ATS.Persistence"
/>
</
daoFactory
>
</
context
>

</
daoConfig
>
主要是 <daoFactory> 节点, 设置 dao 运行时的接口对应的实现类,即:IEmployeesDao 由 EmployeesSqlMapDao实现
8. providers.config 可以从ibatisnet的帮助文档里找到,或去网站下载
编写 Employees.xml 文件 ,主要是编写sql语句
<?
xml version="1.0" encoding="UTF-8"
?>
<
sqlMap
namespace
="Employees"
xmlns
="http://ibatis.apache.org/mapping"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
>
<
alias
>
<
typeAlias
alias
="Employees"
type
="ATS.Domain.Employees, ATS.Domain"
/>
</
alias
>
<
resultMaps
>
<
resultMap
id
="SelectResult"
class
="Employees"
>
<
result
property
="EmployeeID"
column
="employeeid"
/>
<
result
property
="LastName"
column
="lastname"
/>
<
result
property
="FirstName"
column
="firstname"
/>
<
result
property
="Title"
column
="title"
/>
<
result
property
="TitleOfCourtesy"
column
="titleofcourtesy"
/>
<
result
property
="BirthDate"
column
="birthdate"
/>
<
result
property
="HireDate"
column
="hiredate"
/>
<
result
property
="Address"
column
="address"
/>
<
result
property
="City"
column
="city"
/>
<
result
property
="Region"
column
="region"
/>
<
result
property
="PostalCode"
column
="postalcode"
/>
<
result
property
="Country"
column
="country"
/>
<
result
property
="HomePhone"
column
="homephone"
/>
<
result
property
="Extension"
column
="extension"
/>
<
result
property
="Photo"
column
="photo"
/>
<
result
property
="Notes"
column
="notes"
/>
<
result
property
="ReportsTo"
column
="reportsto"
/>
<
result
property
="PhotoPath"
column
="photopath"
/>
</
resultMap
>
</
resultMaps
>
<
statements
>
<
select
id
="SelectEmployees"
parameterClass
="int"
resultMap
="SelectResult"
>
Select
employeeid,
lastname,
firstname,
title,
titleofcourtesy,
birthdate,
hiredate,
address,
city,
region,
postalcode,
country,
homephone,
extension,
photo,
notes,
reportsto,
photopath
From Employees
<
dynamic
prepend
="WHERE"
>
<
isParameterPresent
>
employeeid=#EmployeeID#
</
isParameterPresent
>
</
dynamic
>
</
select
>
<
select
id
="BaseSelectEmployees"
parameterClass
="int"
resultMap
="SelectResult"
>
Select
employeeid,
lastname,
firstname,
title,
titleofcourtesy,
birthdate,
hiredate,
address,
city,
region,
postalcode,
country,
homephone,
extension,
photo,
notes,
reportsto,
photopath
From Employees
</
select
>
<
select
id
="ChildSelectEmployees"
parameterClass
="int"
resultMap
="SelectResult"
extends
="BaseSelectEmployees"
>
</
select
>
<
insert
id
="InsertEmployees"
parameterClass
="Employees"
>
Insert Into Employees (
employeeid,
lastname,
firstname,
title,
titleofcourtesy,
birthdate,
hiredate,
address,
city,
region,
postalcode,
country,
homephone,
extension,
photo,
notes,
reportsto,
photopath
)Values(
#EmployeeID#,
#LastName#,
#FirstName#,
#Title#,
#TitleOfCourtesy#,
#BirthDate#,
#HireDate#,
#Address#,
#City#,
#Region#,
#PostalCode#,
#Country#,
#HomePhone#,
#Extension#,
#Photo#,
#Notes#,
#ReportsTo#,
#PhotoPath#
)
</
insert
>
<
update
id
="UpdateEmployees"
parameterClass
="Employees"
>
Update Employees Set
employeeid=#EmployeeID#,
lastname=#LastName#,
firstname=#FirstName#,
title=#Title#,
titleofcourtesy=#TitleOfCourtesy#,
birthdate=#BirthDate#,
hiredate=#HireDate#,
address=#Address#,
city=#City#,
region=#Region#,
postalcode=#PostalCode#,
country=#Country#,
homephone=#HomePhone#,
extension=#Extension#,
photo=#Photo#,
notes=#Notes#,
reportsto=#ReportsTo#,
photopath=#PhotoPath#
<
dynamic
prepend
="WHERE"
>
<
isParameterPresent
>
employeeid=#EmployeeID#
</
isParameterPresent
>
</
dynamic
>
</
update
>
<
delete
id
="DeleteEmployees"
parameterClass
="int"
>
Delete From Employees
<
dynamic
prepend
="WHERE"
>
<
isParameterPresent
>
employeeid=#EmployeeID#
</
isParameterPresent
>
</
dynamic
>
</
delete
>
</
statements
>
</
sqlMap
>
9. 接下来需要在 global中初始化 castle 容器:
1. 定义自己的容器类,这样可以在容器中初始化 ibatisnet的dao,即读取dao.config文件,将接口和实现类保存起来,
以后使用时直接取出即可,并在castle容器中加入DaoManager 对象实例 ,代码如下:(此类放在ATS.Common 中)
using
System;
using
System.Configuration;
using
Castle.Windsor;
using
IBatisNet.DataAccess;
using
IBatisNet.DataAccess.Configuration;

namespace
ATS.Common

...
{

/**//// <summary>
/// 自定义的容器类,初始化容器的时候,初始化dao,
/// 定义好的持久层的接口和实现类加载
/// </summary>
public class ATSContainer : WindsorContainer

...{
public ATSContainer(Castle.Windsor.Configuration.IConfigurationInterpreter inter) : base(inter)

...{
// Add DaoManager
DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
builder.Configure(@"dao.config");

this.Kernel.AddComponentInstance("DaoManager", typeof(DaoManager), DaoManager.GetInstance("SqlMapDao"));
}
}
}
2. global中初始化容器:
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.SessionState;


using
Castle.Windsor;
using
Castle.Windsor.Configuration.Interpreters;
using
Castle.Core.Resource;
using
ATS.Common;

namespace
ATS.Web

...
{
public class Global : System.Web.HttpApplication, IContainerAccessor

...{

private static WindsorContainer container;

protected void Application_Start(object sender, EventArgs e)

...{
try

...{
//初始化容器,采用从默认(从Web.config中读取默认块)文件中读取
container = new ATSContainer(new XmlInterpreter(new ConfigResource()));
}
catch (ConfigurationException ex)

...{
string msg = ex.Message;
}
}

protected void Application_End(object sender, EventArgs e)

...{
container.Dispose();
}


IContainerAccessor Members#region IContainerAccessor Members

public IWindsorContainer Container

...{

get ...{ return container; }
}

#endregion
}
}
3. 在ATS.Common 中定义 ContainerAccessorUtil.cs类 ,此类主要用来获取容器,
在使用时可以从容器中取出接口:
//
从容器中取出接口
IEmployeesService _IEmployeesService
=
(ContainerAccessorUtil.GetContainer())[
"
Employees
"
]
as
IEmployeesService;
//
并调用此接口的方法,系统会自动实例化实现IEmployeesService 的类,并调用其中的GetEmployeesList方法
IList
<
Employees
>
employees
=
_IEmployeesService.GetEmployeesList();

10. 定义实体类:(domain)
namespace
ATS.Domain

...
{
using System;

/**//// <summary>
/// Employees
/// </summary>
[Serializable]
public class Employees

...{
public Employees()

...{
}
private int _employeeid;
private string _lastname;
private string _firstname;
private string _title;
private string _titleofcourtesy;
private DateTime _birthdate;
private DateTime _hiredate;
private string _address;
private string _city;
private string _region;
private string _postalcode;
private string _country;
private string _homephone;
private string _extension;
private System.Byte[] _photo;
private string _notes;
private int _reportsto;
private string _photopath;

/**////<sumary>
/// EmployeeID
///</sumary>
public int EmployeeID

...{

get...{return _employeeid;}

set...{_employeeid = value;}
}

/**////<sumary>
/// LastName
///</sumary>
public string LastName

...{

get...{return _lastname;}

set...{_lastname = value;}
}

/**////<sumary>
/// FirstName
///</sumary>
public string FirstName

...{

get...{return _firstname;}

set...{_firstname = value;}
}

/**////<sumary>
/// Title
///</sumary>
public string Title

...{

get...{return _title;}

set...{_title = value;}
}

/**////<sumary>
/// TitleOfCourtesy
///</sumary>
public string TitleOfCourtesy

...{

get...{return _titleofcourtesy;}

set...{_titleofcourtesy = value;}
}

/**////<sumary>
/// BirthDate
///</sumary>
public DateTime BirthDate

...{

get...{return _birthdate;}

set...{_birthdate = value;}
}

/**////<sumary>
/// HireDate
///</sumary>
public DateTime HireDate

...{

get...{return _hiredate;}

set...{_hiredate = value;}
}

/**////<sumary>
/// Address
///</sumary>
public string Address

...{

get...{return _address;}

set...{_address = value;}
}

/**////<sumary>
/// City
///</sumary>
public string City

...{

get...{return _city;}

set...{_city = value;}
}

/**////<sumary>
/// Region
///</sumary>
public string Region

...{

get...{return _region;}

set...{_region = value;}
}

/**////<sumary>
/// PostalCode
///</sumary>
public string PostalCode

...{

get...{return _postalcode;}

set...{_postalcode = value;}
}

/**////<sumary>
/// Country
///</sumary>
public string Country

...{

get...{return _country;}

set...{_country = value;}
}

/**////<sumary>
/// HomePhone
///</sumary>
public string HomePhone

...{

get...{return _homephone;}

set...{_homephone = value;}
}

/**////<sumary>
/// Extension
///</sumary>
public string Extension

...{

get...{return _extension;}

set...{_extension = value;}
}

/**////<sumary>
/// Photo
///</sumary>
public System.Byte[] Photo

...{

get...{return _photo;}

set...{_photo = value;}
}

/**////<sumary>
/// Notes
///</sumary>
public string Notes

...{

get...{return _notes;}

set...{_notes = value;}
}

/**////<sumary>
/// ReportsTo
///</sumary>
public int ReportsTo

...{

get...{return _reportsto;}

set...{_reportsto = value;}
}

/**////<sumary>
/// PhotoPath
///</sumary>
public string PhotoPath

...{

get...{return _photopath;}

set...{_photopath = value;}
}
}
}
11. 定义ATS.Persistence.Interface.IEmployeesDao.cs 类
namespace
ATS.Persistence.MapperDao

...
{
using System;
using System.Collections;
using System.Collections.Generic;
using IBatisNet.Common;
using IBatisNet.DataMapper;
using IBatisNet.Common.Exceptions;

using ATS.Domain;
using ATS.Persistence.Interface;


/**//// <summary>
/// EmployeesSqlMapDao
/// </summary>
public class EmployeesSqlMapDao : BaseSqlMapDao, IEmployeesDao

...{
public EmployeesSqlMapDao ()

...{
//
// TODO: 此处添加EmployeesSqlMapDao的构造函数
//
}


/**//// <summary>
/// 得到列表
/// </summary>
public IList<Employees> GetEmployeesList()

...{
return ExecuteQueryForList<Employees>("SelectEmployees",null);
}


/**//// <summary>
/// 新建
/// </summary>
public void AddEmployees(Employees employees)

...{
ExecuteInsert("InsertEmployees",employees);
}

/**//// <summary>
/// 修改
/// </summary>
public void UpdateEmployees(Employees employees)

...{
ExecuteUpdate("UpdateEmployees",employees);
}

/**//// <summary>
/// 得到明细
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public Employees GetEmployeesDetail(System.Int32 EmployeeID)

...{
return ExecuteQueryForObject<Employees>("SelectEmployees",EmployeeID);
}


/**//// <summary>
/// 删除
/// </summary>
/// <param name="id"></param>
public void DeleteEmployees(System.Int32 EmployeeID)

...{
ExecuteDelete("DeleteEmployees",EmployeeID);
}

}
}
BaseSqlMapDao.cs 文件
using
System;
using
System.Collections;
using
System.Collections.Generic;
using
IBatisNet.Common.Pagination;
using
IBatisNet.DataMapper;
using
IBatisNet.DataMapper.Exceptions;
using
IBatisNet.DataMapper.Configuration;
using
IBatisNet.DataAccess;
using
IBatisNet.DataAccess.DaoSessionHandlers;
using
IBatisNet.DataAccess.Exceptions;
using
IBatisNet.DataAccess.Interfaces;
using
IBatisNet.Common.Utilities;
using
ATS.Common;

namespace
ATS.Persistence.MapperDao

...
{

/**//// <summary>
/// Summary description for BaseSqlMapDao.
/// </summary>
public class BaseSqlMapDao : IDao

...{
protected const int PAGE_SIZE = 4;


/**//// <summary>
/// 创建并返回一个sqlmap对象
/// (下面对持久层的操作,都需要用到)
/// Looks up the parent DaoManager, gets the local transaction
/// (which should be a SqlMapDaoTransaction) and returns the
/// SqlMap associated with this DAO.
/// </summary>
/// <returns>The SqlMap instance for this DAO.</returns>
protected SqlMapper GetLocalSqlMap()

...{
//通过DaoManager获取本地的SqlMapper对象
DaoManager daoManager = DaoManager.GetInstance(this) as DaoManager;
SqlMapDaoSession sqlMapDaoSession = (SqlMapDaoSession)daoManager.LocalDaoSession;

return sqlMapDaoSession.SqlMap as SqlMapper;
}


非泛型方法#region 非泛型方法

/**//// <summary>
/// Simple convenience method to wrap the SqlMap method of the same name.
/// Wraps the exception with a DataAccessException to isolate the SqlMap framework.
/// </summary>
/// <param name="statementName"></param>
/// <param name="parameterObject"></param>
/// <returns></returns>
protected IList ExecuteQueryForList(string statementName, object parameterObject)

...{
SqlMapper sqlMap = GetLocalSqlMap();
try

...{
return sqlMap.QueryForList(statementName, parameterObject);
}
catch (Exception e)

...{
throw new DataMapperException("Error executing query '" + statementName + "' for list. Cause: " + e.Message, e);
}
}


/**//// <summary>
/// Simple convenience method to wrap the SqlMap method of the same name.
/// Wraps the exception with a DataAccessException to isolate the SqlMap framework.
/// </summary>
/// <param name="statementName"></param>
/// <param name="parameterObject"></param>
/// <param name="skipResults"></param>
/// <param name="maxResults"></param>
/// <returns></returns>
protected IList ExecuteQueryForList(string statementName, object parameterObject, int skipResults, int maxResults)

...{
SqlMapper sqlMap = GetLocalSqlMap();
try

...{
return sqlMap.QueryForList(statementName, parameterObject, skipResults, maxResults);
}
catch (Exception e)

...{
throw new DataMapperException("Error executing query '" + statementName + "' for list. Cause: " + e.Message, e);
}
}


/**//// <summary>
/// Simple convenience method to wrap the SqlMap method of the same name.
/// Wraps the exception with a DataAccessException to isolate the SqlMap framework.
/// </summary>
/// <param name="statementName"></param>
/// <param name="parameterObject"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
protected IPaginatedList ExecuteQueryForPaginatedList(string statementName, object parameterObject, int pageSize)

...{
SqlMapper sqlMap = GetLocalSqlMap();
try

...{
return sqlMap.QueryForPaginatedList(statementName, parameterObject, pageSize);
}
catch (Exception e)

...{
throw new DataMapperException("Error executing query '" + statementName + "' for paginated list. Cause: " + e.Message, e);
}
}


/**//// <summary>
/// Simple convenience method to wrap the SqlMap method of the same name.
/// Wraps the exception with a DataAccessException to isolate the SqlMap framework.
/// </summary>
/// <param name="statementName"></param>
/// <param name="parameterObject"></param>
/// <returns></returns>
protected object ExecuteQueryForObject(string statementName, object parameterObject)

...{
SqlMapper sqlMap = GetLocalSqlMap();

try

...{
return sqlMap.QueryForObject(statementName, parameterObject);
}
catch (Exception e)

...{
throw new DataMapperException("Error executing query '" + statementName + "' for object. Cause: " + e.Message, e);
}
}


/**//// <summary>
/// Simple convenience method to wrap the SqlMap method of the same name.
/// Wraps the exception with a DataAccessException to isolate the SqlMap framework.
/// </summary>
/// <param name="statementName"></param>
/// <param name="parameterObject"></param>
/// <returns></returns>
protected int ExecuteUpdate(string statementName, object parameterObject)

...{
SqlMapper sqlMap = GetLocalSqlMap();

try

...{
return sqlMap.Update(statementName, parameterObject);
}
catch (Exception e)

...{
throw new DataMapperException("Error executing query '" + statementName + "' for update. Cause: " + e.Message, e);
}
}


/**//// <summary>
/// Simple convenience method to wrap the SqlMap method of the same name.
/// Wraps the exception with a DataAccessException to isolate the SqlMap framework.
/// </summary>
/// <param name="statementName"></param>
/// <param name="parameterObject"></param>
/// <returns></returns>
protected object ExecuteInsert(string statementName, object parameterObject)

...{
SqlMapper sqlMap = GetLocalSqlMap();

try

...{
return sqlMap.Insert(statementName, parameterObject);
}
catch (Exception e)

...{
throw new DataMapperException("Error executing query '" + statementName + "' for insert. Cause: " + e.Message, e);
}
}
#endregion


泛型方法#region 泛型方法


/**//// <summary>
/// 得到列表
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
/// <param name="statementName">操作名称,对应xml中的Statement的id</param>
/// <param name="parameterObject">参数</param>
/// <returns></returns>
protected IList<T> ExecuteQueryForList<T>(string statementName, object parameterObject)

...{
SqlMapper sqlMap = GetLocalSqlMap();

try

...{
return sqlMap.QueryForList<T>(statementName, parameterObject);
}
catch (Exception e)

...{
throw new DataMapperException("Error executing query '" + statementName + "' for list. Cause: " + e.Message, e);
}
}


/**//// <summary>
/// 得到指定数量的记录数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="statementName"></param>
/// <param name="parameterObject">参数</param>
/// <param name="skipResults">跳过的记录数</param>
/// <param name="maxResults">最大返回的记录数</param>
/// <returns></returns>
protected IList<T> ExecuteQueryForList<T>(string statementName, object parameterObject, int skipResults, int maxResults)

...{
SqlMapper sqlMap = GetLocalSqlMap();

try

...{
return sqlMap.QueryForList<T>(statementName, parameterObject, skipResults, maxResults);
}
catch (Exception e)

...{
throw new DataMapperException("Error executing query '" + statementName + "' for list. Cause: " + e.Message, e);
}
}


/**//// <summary>
/// 查询得到对象的一个实例
/// </summary>
/// <typeparam name="T">对象type</typeparam>
/// <param name="statementName">操作名</param>
/// <param name="parameterObject">参数</param>
/// <returns></returns>
protected T ExecuteQueryForObject<T>(string statementName, object parameterObject)

...{
SqlMapper sqlMap = GetLocalSqlMap();

try

...{
return sqlMap.QueryForObject<T>(statementName, parameterObject);
}
catch (Exception e)

...{
throw new DataMapperException("Error executing query '" + statementName + "' for object. Cause: " + e.Message, e);
}
}


/**//// <summary>
/// 执行删除
/// </summary>
/// <param name="statementName">操作名</param>
/// <param name="parameterObject">参数</param>
protected void ExecuteDelete(string statementName, object parameterObject)

...{
SqlMapper sqlMap = GetLocalSqlMap();

try

...{
sqlMap.Delete(statementName, parameterObject);
}
catch (Exception e)

...{
throw new DataMapperException("Error executing query '" + statementName + "' for delete. Cause: " + e.Message, e);
}
}

#endregion
}
}
13. 定义 ATS.Service.Interface.IEmployeesService.cs 类
using
System;
using
System.Collections.Generic;
using
System.Text;

using
ATS.Domain;

namespace
ATS.Service.Interface

...
{
public interface IEmployeesService

...{

/**//// <summary>
/// 得到列表
/// </summary>
IList<Employees> GetEmployeesList();

}
}
14. 定义 ATS.Service.Implement.EmployeesService.cs 类
using
System;
using
System.Collections.Generic;
using
System.Text;

using
ATS.Persistence.Interface;
using
ATS.Persistence.MapperDao;
using
ATS.Service.Interface;
using
ATS.Domain;
using
IBatisNet.DataAccess;

namespace
ATS.Service.Implement

...
{
public class EmployeesService : IEmployeesService

...{

Private Fields#region Private Fields
private IEmployeesDao _employeesDao = null;
private DaoManager _daoManager = null;
#endregion


Constructor#region Constructor
public EmployeesService(DaoManager daoManager)

...{
_daoManager = daoManager;
_employeesDao = _daoManager.GetDao( typeof(IEmployeesDao) ) as IEmployeesDao;
}
#endregion


/**//// <summary>
/// 得到列表
/// </summary>
public IList<Employees> GetEmployeesList()

...{
return _employeesDao.GetEmployeesList();
}

}
}
最后 在页面的aspx.cs文件中调用了, 将数据显示在 gridview 中;
以下是调用代码
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;

using
ATS.Domain;
using
ATS.Service.Implement;
using
ATS.Service.Interface;
using
ATS.Common;
using
System.Collections.Generic;

using
ATS.Common;

namespace
ATS.Web

...
{
public partial class TestForm : System.Web.UI.Page

...{
IEmployeesService _IEmployeesService = null;

protected void Page_Load(object sender, EventArgs e)

...{
//从容器中取出接口(从services.config中的ID获取)
_IEmployeesService = (ContainerAccessorUtil.GetContainer())["Employees"] as IEmployeesService;
if (!IsPostBack)

...{
BindData();
}
}

public void BindData()

...{
//使用接口的方法,容器会自动查找并调用实现类中的方法
IList<Employees> employees = _IEmployeesService.GetEmployeesList();

dgData.DataSource = employees;
dgData.DataBind();
}
}

}
刚刚开始学习,请高手指点 :)