这几天有些时间,看到网上好多朋友学习IbatisNet,我也就来学学,和NHibernate一样这种框架也是从java转化过来的,自然配置文件是少不了的,但是配置文件出错也是让人最为郁闷的事了!我也遇到了这样的问题下面以我学IbatisNet 的第一个例子来讲讲
l 准备工作:
框架可以从http://ibatis.apache.org/dotnetdownloads.cgi上下载。
我用的版本是DataMapper 1.5.1 ,DataAccess 1.8.1
建议下载源代码Source Revision 356824
运行环境: Visual Studio 2005 Team Suite, Sql Server 2000
l 开始编码
1. 创建数据表
CREATE TABLE [Persons] (
[PersonID] [int] IDENTITY (100, 1) NOT NULL ,
[PersonName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[PersonAddress] [varchar] (250) COLLATE Chinese_PRC_CI_AS NULL ,
[CreateDate] [datetime] NULL ,
CONSTRAINT [PK_Persions] PRIMARY KEY CLUSTERED
(
[PersonID]
) ON [PRIMARY]
) ON [PRIMARY]
2. 创建SqlMap.config文件
<?xml version="1.0" encoding="utf-8" ?>
<sqlMapConfig xmlns="http://ibatis.apache.org/dataMapper" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<settings>
<setting cacheModelsEnabled="true"/>
<setting useStatementNamespaces="false" />
</settings>
<providers resource="providers.config" />
<database>
<provider name="sqlServer2.0"></provider>
<dataSource name="IBatisNet" connectionString="server=
</database>
<sqlMaps>
<sqlMap resource="Person.xml" />
</sqlMaps>
</sqlMapConfig>
注意: xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
是必须添加的,我就是因为这个问题出现提示SqlMap.config文件配置有错误.
3. Person.xml
<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="IBatisNet.QuickStart" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<alias>
<typeAlias alias="Person" type="IBatisNet.QuickStart.Person,IBatisNet.QuickStart" />
</alias>
<statements>
<select id="SelectAll" resultMap="SelectAllResult">
select
PersonID,
PersonName,
PersonAddress,
CreateDate
from Persons
</select>
<insert id="InsertPerson" parameterClass="Person">
INSERT INTO Persons(
[PersonName],[PersonAddress],[CreateDate]
)VALUES(
#PersonName#,#PersonAddress#,#CreateDate#
)
</insert>
</statements>
<resultMaps >
<resultMap id="SelectAllResult" class="Person">
<result property="PersonID" column="PersonID" />
<result property="PersonName" column="PersonName" />
<result property="PersonAddress" column="PersonAddress" />
<result property="CreateDate" column="CreateDate" />
</resultMap>
</resultMaps>
</sqlMap>
4. 实体类如下
namespace IBatisNet.QuickStart
{
/// <summary>
/// Person Entity
/// </summary>
[Serializable]
public class Person
{
public Person()
{
}
private int _personid;
private string _personname;
private string _personaddress;
private DateTime _createdate;
/// <summary>
/// Person ID
/// </summary>
public int PersonID
{
set { _personid = value; }
get { return _personid; }
}
/// <summary>
/// Person Name
/// </summary>
public string PersonName
{
set { _personname = value; }
get { return _personname; }
}
/// <summary>
/// Person Address
/// </summary>
public string PersonAddress
{
set { _personaddress = value; }
get { return _personaddress; }
}
/// <summary>
/// Create Date
/// </summary>
public DateTime CreateDate
{
set { _createdate = value; }
get { return _createdate; }
}
}
}
5. providers.config
这个文件可以在下载的文件中 IBatisNet.DataMapper-bin-
注意:在设置你所加载的驱动的属性enabled="true"
以上的配置文件放的位置你的系统必须能够加载到!
有关更多的配置信息可以参看IBatisNet的帮助或是http://pw.cnblogs.com/category/58599.html
6.获取ISqlMapper对象
代码如下:
DomSqlMapBuilder builder = new DomSqlMapBuilder();
//方法一
ISqlMapper Map = builder.Configure("SqlMap.config");
//方法二
ISqlMapper Map = builder.ConfigureAndWatch("SqlMap.config");
7.基本操作
由于我的上面的Person.xml中我只定义了添加和获取全部的方法,别的方法可参照IBatisNet帮助自已添加
using IBatisNet.QuickStart;//person实体类
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
DomSqlMapBuilder builder = new DomSqlMapBuilder();
ISqlMapper Map = builder.Configure("SqlMap.config");
//ISqlMapper Map = builder.ConfigureAndWatch("SqlMap.config");
Person p = new Person();
p.PersonName = "Test";
p.PersonAddress = "china";
p.CreateDate = System.DateTime.Now;
Map.Insert("InsertPerson", p);
小结:
自已学习这些时出的问题最多的还是配置文件上的问题,我的解决方法是在引用dll时我引用的是自已编译Source-revision-426164后的dll.这样做的目的是我们在出错时可以跟踪到源代码中自已查看其中的错误,要不程序出现一个什么配置错误的异常是让人很郁闷的!
希望我的例子能帮助哪些和我一样刚学IBatisNet的朋友!~