IBatisNet系列二-QuickStart篇

本节我参考官方网站上的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

posted @ 2006-03-25 14:59 福娃 阅读(2886) 评论(9)  编辑 收藏 所属分类: IBatis.Net

  回复  引用    
#1楼  2006-06-17 17:52 | ego [未注册用户]
谢谢你的例子,但我运行的时候,老报告以下错误:
{System.TypeLoadException: Could not load type : WebIBatis.Model.Person, WebIBatis

后来,我把Maps/Person.xml中的alias节点改为下面的内容就好了
<typeAlias alias="Person" type="WebIBatis.Model.Person" />

这是为什么?
  回复  引用    
#2楼  2007-06-29 17:54 | 凌云 [未注册用户]
你好 我下载你的例子可以运行

但是在IBatisNet.DataMapper 1.6.1.0(从http://ibatis.apache.org/下载的最新版本) 下一直出现这个错误“Error while configuring the Provider named "sqlServer1.1". Cause : The provider is not in 'providers.config' or is not enabled. ”,我在providers.config明明已经设置好了 找了很久也找不到解决办法

对了,在IBatisNet.DataMapper 1.6.1.0中没有SqlMapper.ConfigureAndWatch这个方法 要用DomSqlMapBuilder类

希望给予指点一下
  回复  引用    
#3楼  2007-06-29 18:02 | 凌云 [未注册用户]
刚才看了你写的
“IBatis.Net系列 - Invalid SqlMap.config document. cause :Cannot load schema for the namespace '' ”这一篇文章 然后相应的修改了代码 发现可以正常运行了 嘻嘻
  回复  引用    
#4楼  2007-07-02 23:42 | 朕 [未注册用户]
int id = 0;
int.TryParse(GridView1.Rows[e.NewEditIndex].Cells[4].Text, out id);
GBrandBLL g_brand = this.Container["GBrandBLL"] as GBrandBLL;

GBrand b = g_brand.Get(id);
//执行上面这句时,提示
There is no Get property named 'id' in class 'Int32'
但我执行,g_brand.Select();方法时可以

不知道为什么?
  回复  引用    
#5楼  2007-08-11 13:07 | livion [未注册用户]
在1.6.1.0中用的是DaoManager这个类,还没有研究用法,。

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
博客园首页

新闻频道

社区

小组

博问

网摘

闪存

  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2006-04-26 18:06 编辑过
成果网帮您增加网站收入


相关链接: