前段时间写了一篇关于配置文件的,由于最近太忙了,一直没接着往下写,现在抽出时间来继续写IBatisNet1.5学习及使用经历.
通过上一篇文章,大概了解了Ibatisnet中SqlMap的配置,但仅仅有SqlMap我们还不能进行IBatisnet的操作,我们还需要根据数据库表来编写映射文件.
Ibatisnet的xml映射文件主要包括这几个部分,resultMaps,parameterMaps,下面分别说一下IBatisnet的映射文件中的Elements及其Attributes.
一、Statements
简单的说,这一部分里放的是对数据库操作的SQL语句及存储过程等,它包括以下子元素:
Statement Element |
Attributes |
Child Elements |
Methods |
<statement> |
id
parameterClass
resultClass
listClass
parameterMap
resultMap
cacheModel |
All dynamic elements |
Insert
Update
Delete
All query methods |
<insert> |
id
parameterClass
parameterMap |
All dynamic elements
<selectKey>
<generate> |
Insert
Update
Delete |
<update> |
id
parameterClass
parameterMap
extends |
All dynamic elements
<generate> |
Insert
Update
Delete |
<delete> |
id
parameterClass
parameterMap
extends |
All dynamic elements
<generate> |
Insert
Update
Delete |
<select> |
id
parameterClass
resultClass
listClass
parameterMap
resultMap
cacheModel
extends |
All dynamic elements
<generate> |
All query methods |
<procedure> |
id
parameterMap
resultClass
resultMap
cacheModel |
All dynamic elements |
Insert
Update
Delete
All query methods |
然后说一下,这些Statement Element的Attributes
1、id
statement的标识,每个statement必须的attribute,还需注意的是,在所有的sqlmap中,它都是唯一的。
2、parameterMap
parameterMap的值会按照顺序匹配statement中的sql语句中的?(占位符)。如下:
<
parameterMaps
>
<
parameterMap
id
="simpleinsert"
class
="Employees"
>
<
parameter
property
="id"
/>
<
parameter
property
="empcode"
/>
</
parameterMap
>
</
parameterMaps
>
<
statement
id
="insertEmployee"
parameterMap
="simpleinsert"
>
insert into Employees (Id, EmpCode) values (?,?);
</
statement
>
上面代码中的parameterMap中我们定义了两个参数id,empcode,而在下面的statement中的insert语句的第一个?就会被id的值所取代,第二个?的值为empcode的值。当然IBatisnet中并不是这样一种参数的定义方式,另外还有两种inline,named方式。
3、parameterClass
还是看上面的例子,如果我们没有对statement指定parameterMap,我们还可以通过指定parameterClass并使用inline parameter来完成同样的目的。如下:
<
statement
id
="insertEmployee"
parameterClass
="OPS.Domain.Employees,OPS.Domain"
>
insert into Employees (Id, EmpCode) values (#Id#,#EmpCode#);
</
statement
>
parameterClass也可以直接写成Employees。
<
statement
id
="insertEmployee"
parameterClass
="Employees"
>
insert into Employees (Id, EmpCode) values (#Id#,#EmpCode#);
</
statement
>
4、resultMap
通过设置resultMap,我们可以控制如何从查询的结果集中提取数据。比如我们只需要结果集中的某几列就可以来设置resultMap中的result来完成。
<
resultMaps
>
<
resultMap
id
="SelectResult"
class
="Employees"
>
<
result
property
="Id"
column
="id"
/>
<
result
property
="EmpCode"
column
="empcode"
/>
<
result
property
="EmpPassword"
column
="emppassword"
/>
<
result
property
="EmpName"
column
="empname"
/>
<
result
property
="EmpSex"
column
="empsex"
/>
<
result
property
="EmpAge"
column
="empage"
/>
<
result
property
="EmpMail"
column
="empmail"
/>
<
result
property
="EmpPhoneNo"
column
="empphoneno"
/>
<
result
property
="Description"
column
="description"
/>
<
result
property
="IsAdmin"
column
="isadmin"
/>
<
result
property
="IsDelete"
column
="isdelete"
/>
</
resultMap
>
</
resultMaps
>
<
statement
id
="SelectEmployees"
resultMap
="Employees"
>
select * from Employees
</
statement
>
上面这个statement的查询结果就返回Employees的所有列,如果你只想返回其中几列,只需要更改resultMap就可以了。
5、resultClass
对于控制查询结果的返回值,我们也可以通过设置resultClass来代替resultMap。指定的resultClass将自动对应到查询结果的列。
<
select
id
="SelectEmployees"
parameterClass
="int"
resultClass
="Employees"
>
Select
id as Id,
empcode as EmpCode,
emppassword as EmpPassword,
empname as EmpName,
empsex as EmpSex,
empage as EmpAge,
empmail as EmpMail,
empphoneno as EmpPhoneNo,
description as Description,
isadmin as IsAdmin,
isdelete as IsDelete
From Employees
</
select
>
Employees.cs应包括Id,EmpCode,EmpPassword,EmpName,EmpSex,EmpAge,EmpMail,EmpPhoneNo,Description,IsAdmin,IsDelete这些属性。
Employee.cs
using System;
namespace OPS.Domain
{
/**//// <summary>
/// Employees
/// </summary>
[Serializable]
public class Employees
{
public Employees()
{
}
private int id;
/**////<sumary>
/// Id
///</sumary>
public int Id
{
get{return id;}
set{id = value;}
}
private string empcode;
/**////<sumary>
/// EmpCode
///</sumary>
public string EmpCode
{
get{return empcode;}
set{empcode = value;}
}
private string emppassword;
/**////<sumary>
/// EmpPassword
///</sumary>
public string EmpPassword
{
get{return emppassword;}
set{emppassword = value;}
}
private string empname;
/**////<sumary>
/// EmpName
///</sumary>
public string EmpName
{
get{return empname;}
set{empname = value;}
}
private string empsex;
/**////<sumary>
/// EmpSex
///</sumary>
public string EmpSex
{
get{return empsex;}
set{empsex = value;}
}
private int? empage;
/**////<sumary>
/// EmpAge
///</sumary>
public int? EmpAge
{
get{return empage;}
set{empage = value;}
}
private string empmail;
/**////<sumary>
/// EmpMail
///</sumary>
public string EmpMail
{
get{return empmail;}
set{empmail = value;}
}
private string empphoneno;
/**////<sumary>
/// EmpPhoneNo
///</sumary>
public string EmpPhoneNo
{
get{return empphoneno;}
set{empphoneno = value;}
}
private string description;
/**////<sumary>
/// Description
///</sumary>
public string Description
{
get{return description;}
set{description = value;}
}
private string isadmin;
/**////<sumary>
/// IsAdmin
///</sumary>
public string IsAdmin
{
get{return isadmin;}
set{isadmin = value;}
}
private string isdelete;
/**////<sumary>
/// IsDelete
///</sumary>
public string IsDelete
{
get{return isdelete;}
set{isdelete = value;}
}
}
}
6、listClass
为了提供返回IList of Objects的能力,statement中可以使用强类型的自定义集合类来做为listClass Attribute的值。这个类必须实现System.Collections.CollectionBase。当然statement还需指定resultClass,使得DataMapper知道如何在集合中处理这种类型。使用方法如下:
EmployeeCollection
using System;
using System.Collections;
using System.Text;
namespace OPS.Domain
{
public class EmployeeCollection : CollectionBase
{
public EmployeeCollection() { }
public Employees this[int index]
{
get { return (Employees)List[index]; }
set { List[index] = value; }
}
public int Add(Employees value)
{
return List.Add(value);
}
public void AddRange(Employees[] value)
{
for (int i = 0; i < value.Length; i++)
{
Add(value[i]);
}
}
public void AddRange(EmployeeCollection value)
{
for (int i = 0; i < value.Count; i++)
{
Add(value[i]);
}
}
public bool Contains(Employees value)
{
return List.Contains(value);
}
public void CopyTo(Employees[] array, int index)
{
List.CopyTo(array, index);
}
public int IndexOf(Employees value)
{
return List.IndexOf(value);
}
public void Insert(int index, Employees value)
{
List.Insert(index, value);
}
public void Remove(Employees value)
{
List.Remove(value);
}
}
}
<
select id
=
"
SelectEmployees
"
parameterClass
=
"
int
"
resultClass
=
"
Employess
"
listClass
=
"
EmployeeCollection
"
>
Select
id
as
Id,
empcode
as
EmpCode,
emppassword
as
EmpPassword,
empname
as
EmpName,
empsex
as
EmpSex,
empage
as
EmpAge,
empmail
as
EmpMail,
empphoneno
as
EmpPhoneNo,
description
as
Description,
isadmin
as
IsAdmin,
isdelete
as
IsDelete
From Employees
</
select
>
7、cacheModel
如果你希望将查询结果集装入缓存,你可以通过设置statement的cacheModel来实现。首先需要配置缓存的模式,如下:
<
cacheModel
id
="employee-cache"
implementation
="LRU"
>
<
flushInterval
hours
="24"
/>
<
flushOnExecute
statement
="insertEmployee"
/>
<
flushOnExecute
statement
="updateEmployee"
/>
<
flushOnExecute
statement
="deleteEmployee"
/>
<
property
name
="size"
value
="1000"
/>
</
cacheModel
>
关于缓存模式,后面会专门介绍。
在statement中使用缓存:
<
statement
id
="selectAllEmployees"
cacheModel
="employee-cache"
>
select * from Employees
</
statement
>
8、extends
继承的属性也非常的有用,有很多SQL的操作的主题都一样,只是,Where子句不同,或Order By的内容不同等等,我们都可以将主体独立出来,作为BASE,然后写不同的条件或排序规则来继承它。如下:
<
select
id
="SelectEmployees"
parameterClass
="int"
resultMap
="SelectResult"
>
Select
id,
empcode,
emppassword,
empname,
empsex,
empage,
empmail,
empphoneno,
description,
isadmin,
isdelete
From Employees
</
select
>
<!--
员工列表
-->
<
select
id
="EmployeeList"
parameterClass
="int"
resultMap
="SelectResult"
extends
="SelectEmployees"
>
Where isdelete='n'
</
select
>
<!--
员工明细
-->
<
select
id
="EmployeeDetail"
parameterClass
="int"
resultMap
="SelectResult"
extends
="SelectEmployees"
>
Where id=#Id#
</
select
>
<!--
登录验证
-->
<
select
id
="Login"
parameterClass
="int"
resultMap
="SelectResult"
extends
="SelectEmployees"
>
Where empcode=#EmpCode# and emppassword=#EmpPassword# and isdelete='n'
</
select
>
<!--
判断员工编号是否重复
-->
<
select
id
="CheckEmpCode"
parameterClass
="String"
resultMap
="SelectResult"
extends
="SelectEmployees"
>
Where empcode=#EmpCode# and isdelete='n'
</
select
>
OK,先写到这里。