一、前言:
本文档主要介绍了使用Ibatis的数据访问和Log4Net的日志管理在开发项目中一些常用的操作,关于dll等的引入等暂不作介绍了
参考资料: Doc-DataMapper- 帮助文档
网上的部分资料
二、实战:
我们以一个例子来介绍和说明如何在项目中使用Ibatis中的数据访问
A.数据访问
我们以SQL2000中Northwind数据库中的表Employees为例来介绍如何进行数据访问
1. 用CodeSmith或其他工具生成表Employees对应的类(这里我们取名为Employee)
using System.Data;
using System.Configuration;
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;
namespace UsingIBatisNet
{
#region Employee
///
/// 数据库[Northwind] 中表[dbo.Employees] 的实体类.
///
/// 创建人: Steven Pei
/// 创建日期:
/// 修改人:
/// 修改日期:
/// 修改内容:
/// 版 本:
public class Employee
{
private int _EmployeeID;
private string _Lastname = String.Empty;
private string _Firstname = String.Empty;
private string _Title = String.Empty;
private string _Titleofcourtesy = String.Empty;
private DateTime _Birthdate;
private DateTime _Hiredate;
private string _Address = String.Empty;
private string _City = String.Empty;
private string _Region = String.Empty;
private string _Postalcode = String.Empty;
private string _Country = String.Empty;
private string _Homephone = String.Empty;
private string _Extension = String.Empty;
private byte [] _Photo;
private string _Notes = String.Empty;
private int _Reportsto;
private string _Photopath = String.Empty;
// Instantiate empty Employees for inserting
public Employee() { }
// Retrieve Employees with Id for updating
public Employee( int EmployeeID)
{
this ._EmployeeID = EmployeeID;
}
#region Public Properties
public int EmployeeID
{
get { return _EmployeeID; }
set { _EmployeeID = value; }
}
public string LastName
{
get { return _Lastname; }
set { _Lastname = value; }
}
public string FirstName
{
get { return _Firstname; }
set { _Firstname = value; }
}
public string Title
{
get { return _Title; }
set { _Title = value; }
}
public string TitleOfCourtesy
{
get { return _Titleofcourtesy; }
set { _Titleofcourtesy = value; }
}
public DateTime BirthDate
{
get { return _Birthdate; }
set { _Birthdate = value; }
}
public DateTime HireDate
{
get { return _Hiredate; }
set { _Hiredate = value; }
}
public string Address
{
get { return _Address; }
set { _Address = value; }
}
public string City
{
get { return _City; }
set { _City = value; }
}
public string Region
{
get { return _Region; }
set { _Region = value; }
}
public string PostalCode
{
get { return _Postalcode; }
set { _Postalcode = value; }
}
public string Country
{
get { return _Country; }
set { _Country = value; }
}
public string HomePhone
{
get { return _Homephone; }
set { _Homephone = value; }
}
public string Extension
{
get { return _Extension; }
set { _Extension = value; }
}
public byte [] Photo
{
get { return _Photo; }
set { _Photo = value; }
}
public string Notes
{
get { return _Notes; }
set { _Notes = value; }
}
public int ReportsTo
{
get { return _Reportsto; }
set { _Reportsto = value; }
}
public string PhotoPath
{
get { return _Photopath; }
set { _Photopath = value; }
}
#endregion
}
#endregion
}
2.建立数据库和Employee类之间的映射关系(这里的映射文件名用Employee.xml)
< sqlMap namespace = " ks_zthb " xmlns = " http://ibatis.apache.org/mapping " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " >
< alias >
< typeAlias alias = " Employee " type = " UsingIBatisNet.Employee, UsingIBatisNet " />
alias >
< resultMaps >
< resultMap class = " Employee " id = " Employees_Result " >
—数据库中表与类中元素的一一对应关系 -->
< result column = " EmployeeID " property = " EmployeeID " />
< result column = " LastName " property = " LastName " />
< result column = " FirstName " property = " FirstName " />
< result column = " Title " property = " Title " />
< result column = " TitleOfCourtesy " property = " TitleOfCourtesy " />
< result column = " BirthDate " property = " BirthDate " />
< result column = " HireDate " property = " HireDate " />
< result column = " Address " property = " Address " />
< result column = " City " property = " City " />
< result column = " Region " property = " Region " />
< result column = " PostalCode " property = " PostalCode " />
< result column = " Country " property = " Country " />
< result column = " HomePhone " property = " HomePhone " />
< result column = " Extension " property = " Extension " />
< result column = " Photo " property = " Photo " />
< result column = " Notes " property = " Notes " />
< result column = " ReportsTo " property = " ReportsTo " />
< result column = " PhotoPath " property = " PhotoPath " />
resultMap >
resultMaps >
id :该存储过程的唯一标识符
parameterClass : 传入的参数的类型(可以是string,类等)
listClass :查询后结果集的类型
resultMap:查询后结果集的输出形式(此处为resultMap中id为Employees_Result的输出形式) -->
SELECT [EmployeeID],[LastName],[FirstName],[Title],[TitleOfCourtesy],[BirthDate],[HireDate],[Address],[City],[Region],[PostalCode],[Country],[HomePhone],[Extension],[Photo],[Notes],[ReportsTo],[PhotoPath]
FROM [dbo].[Employees]
order by IssueDate
id :该存储过程的唯一标识符
parameterClass : 传入的参数的类型(可以是string,类等)-->
INSERT INTO [dbo].[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#)
id :该存储过程的唯一标识符
parameterClass : 传入的参数的类型(可以是string,类等)此处传递的为一个实例-->
DELETE FROM [dbo].[Employees] WHERE [EmployeeID] = #EmployeeID#
id :该存储过程的唯一标识符
parameterClass : 传入的参数的类型(可以是string,类等)此处传递的为一个实例-->
UPDATE [dbo].[Employees] SET [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# WHERE [EmployeeID] = #EmployeeID#
ViewTicket
说明:在这个映射文件中我们可以看到很多没有碰到过的属性名,如alias、resultMap、result等等,下面来介绍一下这些常用元素的用法
Alias (必须的)
typeAlias
alias :我们所标识的该类的别名,必须唯一
type : 指明该类存放的具体位置,格式为: (命名空间.类名,程序集的名称,Version=[*], Culture=[*],PublicKeyToken=[*]) 绿色为可选项
resultMap
id :在resultMap中的唯一标识
class :对应于typeAlias中哪一个alias中的类
extends:从某一resultMap中的父类中继承下来的类的所有属性
result
property:对应的类中的属性
column:对应于操作后返回的结果显示的相应数据库字段名
columnIndex:对应列的索引
statementsid: 在SqlMap中的唯一标识
parameterMap:定义了将字段名和对象的属性对应起来值的规则, 通常匹配?号(如下:)
insert into PRODUCT (PRD_ID, PRD_DESCRIPTION) values (?,?);
这里的(?,?)实际上是类Product中的属性 (id, description)
cacheModel:将查询的结果放入缓存中(举例如下:)
select * from PRODUCT where PRD_CAT_ID = #value#
extends:即在已有的Sql语句上添加条件等,extends后跟已有的Sql操作的id
3.根据需要获取或操作相应的结果集
Ibatis在返回的结果集上主要有以下几种:
Ilist、Idictionary、Object等,没有我们DataSet、DataTable,这些需要我们扩展
4. 根据需要生成动态的Sql
在动态生成的Sql中主要有以下几种:(我们在此配置上增加)
SELECT * FROM [dbo].[Employees]
1)条件为:当传入的EmployeeID字段值不为空的时候根据EmployeeID来查询
EmployeeID = #EmployeeID#
2)条件为:出生日期大于1981
Birthdate = #Birthdate#
3)条件为:City不为空
City like # City # + '%'
这里只是介绍了三种常用的生成动态Sql是如何实现的,其他可参照帮助文档
B.Ibatis的日志管理
首先,在应用程序中引入log4net.dll和IBatisNet.Common.Logging.Log4Net.dll这两个dll。接着 在应用程序的配置文件中加入配置节信息:(这里以Web应用程序为例)
如果我们要获取
1、在自定义配置节中加入
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> 2、在appSetting配置节中加入 3、记录加载的一些程序集信息则在加入如下配置信息 type="System.Diagnostics.TextWriterTraceListener" initializeData="e:\log4net.txt" /> //信息保存的路径 4、如果想输出已经配置好的 SQL 命令,则使得 同理,还可添加其它输出信息,具体参数设置参见帮助文档 三、小结 Ibatis的功能非常强大,这里只是简单介绍了Ibatis的一些基本操作,事务的处理等也没有介绍,大家在使用的时候可以参照 Doc-DataMapper-1.5.1 帮助文档 (相关下载: Doc-DataMapper-1.5.1 帮助文档 本篇Blog的Word文档)
IBatisNet.DataMapper.Configuration.Statements.DefaultPreparedCommand
可用,在