本节我参考官方网站上的Quick Start Guide,网址:
http://opensource2.atlassian.com/confluence/oss/display/IBATIS/Quick+Start+Guide
我会跟着该例子创建一个实例代码.
补充以下,IBatisNet包括两个部分Data Mapper和DataAccess,这个实例主要针对 Data Mapper的.
1.在我们MSSQL中建立表如下:
create database IBatisDemo
go
use IBatisDemo
if exists(select * from sysobjects where type='u' and name='Person')
drop table dbo.Person
go
create table dbo.Person
(
PER_ID int not null,
PER_FIRST_NAME varchar(20) null,
PER_LAST_NAME varchar(20) null,
PER_BIRTH_DATE smalldatetime null,
PER_WEIGHT_KG decimal null,
PER_HEIGHT_M decimal null,
primary key(PER_ID)
)
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类文件
using System;
namespace WebIBatis.Model
{
///
/// Person 的摘要说明。
///
public class Person
{
private int _Id;
public int Id
{
get { return _Id; }
set { _Id = value; }
}
private string _FirstName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
private string _LastName;
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
private DateTime _BirthDate;
public DateTime BirthDate
{
get { return _BirthDate; }
set { _BirthDate = value; }
}
private decimal _WeightInKilograms;
public decimal WeightInKilograms
{
get { return _WeightInKilograms; }
set { _WeightInKilograms = value; }
}
private decimal _HeightInMeters;
public decimal HeightInMeters
{
get { return _HeightInMeters; }
set { _HeightInMeters = value; }
}
}
}
这个类就是对Person的一个描述,只包含一些属性,这就是这个系统的数据的载体
4.定义实体定义的XML
在项目目录下建Maps目录下,在该目录下建立Person.xml
xml version="1.0" encoding="utf-8" ?>
<sqlMap
namespace="Person"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="SqlMap.xsd">
XML "behind" document for the People service class. -->
<alias>
<typeAlias alias="Person" type="WebIBatis.Model.Person, WebIBatis" />
alias>
<resultMaps>
<resultMap id="SelectResult" class="Person">
<result property="Id" column="PER_ID" />
<result property="FirstName" column="PER_FIRST_NAME" />
<result property="LastName" column="PER_LAST_NAME" />
<result property="BirthDate" column="PER_BIRTH_DATE" />
<result property="WeightInKilograms" column="PER_WEIGHT_KG" />
<result property="HeightInMeters" column="PER_HEIGHT_M" />
resultMap>
resultMaps>
<statements>
<select id="Select" parameterClass="int" resultMap="SelectResult">
select
PER_ID,
PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M
from PERSON
<dynamic prepend="WHERE">
<isParameterPresent>
PER_ID = #value#
isParameterPresent>
dynamic>
select>
<insert id="Insert" parameterClass="Person" resultClass="int">
insert into PERSON
(PER_ID, PER_FIRST_NAME, PER_LAST_NAME,
PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M)
values
(#Id#, #FirstName#, #LastName#,
#BirthDate#, #WeightInKilograms#, #HeightInMeters#)
insert>
<update id="Update" parameterClass="Person" resultClass="int">
update PERSON set
PER_FIRST_NAME = #FirstName#,
PER_LAST_NAME = #LastName#,
PER_BIRTH_DATE = #BirthDate#,
PER_WEIGHT_KG = #WeightInKilograms#,
PER_HEIGHT_M = #HeightInMeters#
where PER_ID = #Id#
update>
<delete id="Delete" parameterClass="int" resultClass="int">
delete from PERSON
where PER_ID = #value#
delete>
statements>
sqlMap>
<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对象
using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper;
namespace WebIBatis
{
///
/// Mapper 的摘要说明。
///
public class Mapper
{
private static volatile SqlMapper _mapper = null;
protected static void Configure (object obj)
{
_mapper = (SqlMapper) obj;
}
protected static void InitMapper()
{
ConfigureHandler handler = new ConfigureHandler (Configure);
_mapper = SqlMapper.ConfigureAndWatch (handler);
}
public static SqlMapper Instance()
{
if (_mapper == null)
{
lock (typeof (SqlMapper))
{
if (_mapper == null) // double-check
InitMapper();
}
}
return _mapper;
}
public static SqlMapper Get()
{
return Instance();
}
}
}
7.取数据
在Webform1.aspx窗体添加一DataGrid,在后置代码的Page_Load中添加代码如下:
IList list = Mapper.Instance().QueryForList("Select",null);
DataGrid1.DataSource = list;
DataGrid1.DataBind();
其中Select是在Person中定义的statements
8.其他操作的写法
//添加
Person newperson = new Person();
//给person赋值
newperson.FirstName = "姚";
//....
Mapper.Instance().Insert("Insert",newperson);
//查看明细和修改
//根据ID得到明细
int id = 1;
//得到Person对象
Person person = Mapper.Instance().QueryForObject("Select",id) as Person;
//修改person的值
person.LastName = "国荣";
Mapper.Instance().Update("Update",person);
实例附件下载:/Files/maplye/WebIBatis.rar