本节我参考官方网站上的Quick Start Guide,网址:
http://opensource2.atlassian.com/confluence/oss/display/IBATIS/Quick+Start+Guide
我会跟着该例子创建一个实例代码.
补充以下,IBatisNet包括两个部分Data Mapper和DataAccess,这个实例主要针对 Data Mapper的.
1.在我们MSSQL中建立表如下:
1
create database IBatisDemo
2
go
3
use IBatisDemo
4
if exists(select * from sysobjects where type='u' and name='Person')
5
drop table dbo.Person
6
go
7
create table dbo.Person
8
(
9
PER_ID int not null,
10
PER_FIRST_NAME varchar(20) null,
11
PER_LAST_NAME varchar(20) null,
12
PER_BIRTH_DATE smalldatetime null,
13
PER_WEIGHT_KG decimal null,
14
PER_HEIGHT_M decimal null,
15
primary key(PER_ID)
16
)
2.使用VS2003创建WebProject,名称为WebIBatis(注意大小写)
3.在项目目录下建立lib目录,复制IBatisNet必需的dll,如下:
IBatisNet.Common.dll
IBatisNet.DataMapper.dll
IBatisNet.DataAccess.dll
log4net.dll
Castle.DynamicProxy.dll
并在项目中添加这些dll的引用
4.创建模型对象
在项目中创建目录 Model,并在该目录下创建Person类文件
1
using System;
2
namespace WebIBatis.Model
3

{
4
/**////
5
/// Person 的摘要说明。
6
///
7
public class Person
8

{
9
private int _Id;
10
public int Id
11

{
12
get
{ return _Id; }
13
set
{ _Id = value; }
14
}
15
private string _FirstName;
16
public string FirstName
17

{
18
get
{ return _FirstName; }
19
set
{ _FirstName = value; }
20
}
21
private string _LastName;
22
public string LastName
23

{
24
get
{ return _LastName; }
25
set
{ _LastName = value; }
26
}
27
private DateTime _BirthDate;
28
public DateTime BirthDate
29

{
30
get
{ return _BirthDate; }
31
set
{ _BirthDate = value; }
32
}
33
private decimal _WeightInKilograms;
34
public decimal WeightInKilograms
35

{
36
get
{ return _WeightInKilograms; }
37
set
{ _WeightInKilograms = value; }
38
}
39
private decimal _HeightInMeters;
40
public decimal HeightInMeters
41

{
42
get
{ return _HeightInMeters; }
43
set
{ _HeightInMeters = value; }
44
}
45
}
46
}
这个类就是对Person的一个描述,只包含一些属性,这就是这个系统的数据的载体
4.定义实体定义的XML
在项目目录下建Maps目录下,在该目录下建立Person.xml
1
xml version="1.0" encoding="utf-8" ?>
2
<sqlMap
3
namespace="Person"
4
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
xsi:noNamespaceSchemaLocation="SqlMap.xsd">
6
XML "behind" document for the People service class. -->
7
<alias>
8
<typeAlias alias="Person" type="WebIBatis.Model.Person, WebIBatis" />
9
alias>
10
<resultMaps>
11
<resultMap id="SelectResult" class="Person">
12
<result property="Id" column="PER_ID" />
13
<result property="FirstName" column="PER_FIRST_NAME" />
14
<result property="LastName" column="PER_LAST_NAME" />
15
<result property="BirthDate" column="PER_BIRTH_DATE" />
16
<result property="WeightInKilograms" column="PER_WEIGHT_KG" />
17
<result property="HeightInMeters" column="PER_HEIGHT_M" />
18
resultMap>
19
resultMaps>
20
<statements>
21
<select id="Select" parameterClass="int" resultMap="SelectResult">
22
select
23
PER_ID,
24
PER_FIRST_NAME,
25
PER_LAST_NAME,
26
PER_BIRTH_DATE,
27
PER_WEIGHT_KG,
28
PER_HEIGHT_M
29
from PERSON
30
<dynamic prepend="WHERE">
31
<isParameterPresent>
32
PER_ID = #value#
33
isParameterPresent>
34
dynamic>
35
select>
36
<insert id="Insert" parameterClass="Person" resultClass="int">
37
insert into PERSON
38
(PER_ID, PER_FIRST_NAME, PER_LAST_NAME,
39
PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M)
40
values
41
(#Id#, #FirstName#, #LastName#,
42
#BirthDate#, #WeightInKilograms#, #HeightInMeters#)
43
insert>
44
<update id="Update" parameterClass="Person" resultClass="int">
45
update PERSON set
46
PER_FIRST_NAME = #FirstName#,
47
PER_LAST_NAME = #LastName#,
48
PER_BIRTH_DATE = #BirthDate#,
49
PER_WEIGHT_KG = #WeightInKilograms#,
50
PER_HEIGHT_M = #HeightInMeters#
51
where PER_ID = #Id#
52
update>
53
<delete id="Delete" parameterClass="int" resultClass="int">
54
delete from PERSON
55
where PER_ID = #value#
56
delete>
57
statements>
58
sqlMap>
59
<typeAlias alias="Person" type="WebIBatis.Model.Person, WebIBatis" />表示为WebIBatis.Model.Person取了个别名,这样在下面的class=别名就可以了
resultMap 是数据库字段和Person的类的对应关系,也是SQL语句操作的结果的载体,其作用就是,SQL语句操作返回的数据的值根据这个resultMap的定义,将相应的字段的值赋给Person类对应的属性.
5.定义数据连接
在项目根目录下定义sqlmap.config
xml version="1.0" encoding="UTF-8" ?>
<sqlMapConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="SqlMapConfig.xsd">
<settings>
<setting useStatementNamespaces="false"/>
<setting cacheModelsEnabled="true"/>
settings>
<database>
<provider name="sqlServer1.1"/>
<dataSource name="iBatisTutorial" connectionString="server=.;User ID=sa;Password=;database=IBatisDemo;Connection Reset=FALSE"/>
database>
<sqlMaps>
<sqlMap resource="Maps/Person.xml"/>
sqlMaps>
sqlMapConfig>
并拷贝providers.config文件到根目录,该文件定义各种数据库的驱动,包括SqlServer, Oracle, MySQL, PostgreSQL, DB2 and OLEDB, ODBC
6.定义Mapper
在根目录下创建Mapper类,该类是得到单一的SqlMapper对象
1
using IBatisNet.Common.Utilities;
2
using IBatisNet.DataMapper;
3
namespace WebIBatis
4

{
5
/**////
6
/// Mapper 的摘要说明。
7
///
8
public class Mapper
9

{
10
private static volatile SqlMapper _mapper = null;
11
protected static void Configure (object obj)
12

{
13
_mapper = (SqlMapper) obj;
14
}
15
protected static void InitMapper()
16

{
17
ConfigureHandler handler = new ConfigureHandler (Configure);
18
_mapper = SqlMapper.ConfigureAndWatch (handler);
19
}
20
public static SqlMapper Instance()
21

{
22
if (_mapper == null)
23

{
24
lock (typeof (SqlMapper))
25

{
26
if (_mapper == null) // double-check
27
InitMapper();
28
}
29
}
30
return _mapper;
31
}
32
public static SqlMapper Get()
33

{
34
return Instance();
35
}
36
}
37
}
38
7.取数据
在Webform1.aspx窗体添加一DataGrid,在后置代码的Page_Load中添加代码如下:
1
IList list = Mapper.Instance().QueryForList("Select",null);
2
DataGrid1.DataSource = list;
3
DataGrid1.DataBind();
其中Select是在Person中定义的statements
8.其他操作的写法
1
//添加
2
Person newperson = new Person();
3
//给person赋值
4
newperson.FirstName = "姚";
5
//
.
6
7
Mapper.Instance().Insert("Insert",newperson);
8
//查看明细和修改
9
//根据ID得到明细
10
int id = 1;
11
//得到Person对象
12
Person person = Mapper.Instance().QueryForObject("Select",id) as Person;
13
//修改person的值
14
person.LastName = "国荣";
15
Mapper.Instance().Update("Update",person);
实例附件下载:
/Files/maplye/WebIBatis.rar