IBatisNet系列二-QuickStart篇
本节我参考官方网站上的Quick Start Guide,网址:
http://opensource2.atlassian.com/confluence/oss/display/IBATIS/Quick+Start+Guide
我会跟着该例子创建一个实例代码.
补充以下,IBatisNet包括两个部分Data Mapper和DataAccess,这个实例主要针对 Data Mapper的.
1.在我们MSSQL中建立表如下:
2go
3use IBatisDemo
4if exists(select * from sysobjects where type='u' and name='Person')
5drop table dbo.Person
6go
7create table dbo.Person
8(
9PER_ID int not null,
10PER_FIRST_NAME varchar(20) null,
11PER_LAST_NAME varchar(20) null,
12PER_BIRTH_DATE smalldatetime null,
13PER_WEIGHT_KG decimal null,
14PER_HEIGHT_M decimal null,
15primary 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类文件
2namespace WebIBatis.Model
3{
4///
5/// Person 的摘要说明。
6///
7 public class Person
8{
9private int _Id;
10public int Id
11{
12get { return _Id; }
13set { _Id = value; }
14}
15private string _FirstName;
16public string FirstName
17{
18get { return _FirstName; }
19set { _FirstName = value; }
20}
21private string _LastName;
22public string LastName
23{
24get { return _LastName; }
25set { _LastName = value; }
26}
27private DateTime _BirthDate;
28public DateTime BirthDate
29{
30get { return _BirthDate; }
31set { _BirthDate = value; }
32}
33private decimal _WeightInKilograms;
34public decimal WeightInKilograms
35{
36get { return _WeightInKilograms; }
37set { _WeightInKilograms = value; }
38}
39private decimal _HeightInMeters;
40public decimal HeightInMeters
41{
42get { return _HeightInMeters; }
43set { _HeightInMeters = value; }
44}
45}
46}
4.定义实体定义的XML
在项目目录下建Maps目录下,在该目录下建立Person.xml
2<sqlMap
3namespace="Person"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xsi:noNamespaceSchemaLocation="SqlMap.xsd">
6 XML "behind" document for the People service class. -->
7<alias>
8<typeAlias alias="Person" type="WebIBatis.Model.Person, WebIBatis" />
9alias>
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" />
18resultMap>
19resultMaps>
20<statements>
21<select id="Select" parameterClass="int" resultMap="SelectResult">
22select
23PER_ID,
24PER_FIRST_NAME,
25PER_LAST_NAME,
26PER_BIRTH_DATE,
27PER_WEIGHT_KG,
28PER_HEIGHT_M
29from PERSON
30<dynamic prepend="WHERE">
31<isParameterPresent>
32PER_ID = #value#
33isParameterPresent>
34dynamic>
35select>
36<insert id="Insert" parameterClass="Person" resultClass="int">
37insert into PERSON
38(PER_ID, PER_FIRST_NAME, PER_LAST_NAME,
39PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M)
40values
41(#Id#, #FirstName#, #LastName#,
42#BirthDate#, #WeightInKilograms#, #HeightInMeters#)
43insert>
44<update id="Update" parameterClass="Person" resultClass="int">
45update PERSON set
46PER_FIRST_NAME = #FirstName#,
47PER_LAST_NAME = #LastName#,
48PER_BIRTH_DATE = #BirthDate#,
49PER_WEIGHT_KG = #WeightInKilograms#,
50PER_HEIGHT_M = #HeightInMeters#
51where PER_ID = #Id#
52update>
53<delete id="Delete" parameterClass="int" resultClass="int">
54delete from PERSON
55where PER_ID = #value#
56delete>
57statements>
58sqlMap>
59
<typeAlias alias="Person" type="WebIBatis.Model.Person, WebIBatis" />表示为WebIBatis.Model.Person取了个别名,这样在下面的class=别名就可以了
resultMap 是数据库字段和Person的类的对应关系,也是SQL语句操作的结果的载体,其作用就是,SQL语句操作返回的数据的值根据这个resultMap的定义,将相应的字段的值赋给Person类对应的属性.
5.定义数据连接
在项目根目录下定义sqlmap.config
<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对象
2using IBatisNet.DataMapper;
3namespace WebIBatis
4{
5///
6/// Mapper 的摘要说明。
7///
8 public class Mapper
9{
10private static volatile SqlMapper _mapper = null;
11protected static void Configure (object obj)
12{
13_mapper = (SqlMapper) obj;
14}
15protected static void InitMapper()
16{
17ConfigureHandler handler = new ConfigureHandler (Configure);
18_mapper = SqlMapper.ConfigureAndWatch (handler);
19}
20public static SqlMapper Instance()
21{
22if (_mapper == null)
23{
24lock (typeof (SqlMapper))
25{
26if (_mapper == null) // double-check
27 InitMapper();
28}
29}
30return _mapper;
31}
32public static SqlMapper Get()
33{
34return Instance();
35}
36}
37}
38
7.取数据
在Webform1.aspx窗体添加一DataGrid,在后置代码的Page_Load中添加代码如下:
2DataGrid1.DataSource = list;
3DataGrid1.DataBind();
其中Select是在Person中定义的statements
8.其他操作的写法
2 Person newperson = new Person();
3//给person赋值
4 newperson.FirstName = "姚";
5//.
6
7Mapper.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 = "国荣";
15Mapper.Instance().Update("Update",person);