仅仅有SqlMap,我们还不能进行iBatisNet的操作,还需要根据数据库表来编写映射文件。参考文章:http://www.cnblogs.com/pw/archive/2006/11/22/566979.html
例如:Person.xml
iBatisNet的XML映射文件主要包括这几个部分:resultMaps,parameterMaps,下面详解:
一、Statements
这一部分放的是对数据库操作的SQL语句及存储过程,它包括:
<statement> 属性有id,parameterClass,resultClass,listClass,parameterMap,resultMap,cacheModel
<insert>属性有id,parameterClass,parameterMap
<update>属性有id,parameterClass,parameterMap,extends
<delete>属性有id,parameterClass,parameterMap,extends
<update>属性有id,parameterClass,resultClass,listClass,parameterMap,resultMap,cacheModel,extends
<procedure>属性有id,parameterClass,resultClass,resultMap,cacheModel
各属性的含义:
1. id statement的标识,每个statement必须的attribute,还需注意的是,在所有的sqlmap中,它都是唯一的。
2. parameterMap parameterMap的值会按照顺序匹配statement中的sql语句中的?(占位符)。
parameterMaps
<
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来完成同样的目的。如下:
parameterClass
<
statement
id
="insertEmployee"
parameterClass
="OPS.Domain.Employees,OPS.Domain"
>
insert into Employees (Id, EmpCode) values (#Id#,#EmpCode#);
</
statement
>
也可以写成:
<
statement
id
="insertEmployee"
parameterClass
="Employees"
>
insert into Employees (Id, EmpCode) values (#Id#,#EmpCode#);
</
statement
>
4. resultMap
通过设置resultMap,我们可以控制如何从查询的结果集中提取数据。比如我们只需要结果集中的某几列就可以来设置resultMap中的result来完成。
resultMaps
<
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
>
stements:
<
statement
id
="SelectEmployees"
resultMap
="Employees"
>
select * from Employees
</
statement
>
上面这个statement的查询结果就返回Person的所有列,如果你只想返回其中几列,只需要更改resultMap就可以了。
5. resultClass
对于控制查询结果的返回值,我们也可以通过设置resultClass来代替resultMap。指定的resultClass将自动对应到查询结果的列。
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这些属性。
Employees.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知道如何在集合中处理这种类型。
7. cacheModel
如果你希望将查询结果集装入缓存,你可以通过设置statement的cacheModel来实现。cacheModel
8. extends
继承的属性也非常的有用,有很多SQL的操作的主题都一样,只是,Where子句不同,或Order By的内容不同等等,我们都可以将主体独立出来,作为BASE,然后写不同的条件或排序规则来继承它。