IBatis.net使用说明
2011-06-19 15:36
274 查看
IBatis.net是移植于java版本,所以在.net平台上的操作和java平台上的操作基本没有什么太大的差别。严格来说,ibatis.net并不算的上是一种orm,因为它的sql语句均写到了配置文件中,没有自己专门的sql映射语句。但是相比Nhibernate来说,不仅简易,而且方便易学。学习成本还是很低的。
回到正规话题上来,实例IBatis.net需要两个类库:IBatisNet.DataMapper.dll和IBatisNet.Common.dll,新建项目,我的项目结构如下:
View Code
<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="Person" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<alias>
<typeAlias alias="Person" type="Model.Person,Model"/>
</alias>
<resultMaps>
<resultMap id="SelectAllResult" 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="SelectAllPerson" resultMap="SelectAllResult">
select
PER_ID,
PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M
from PERSON
</select>
<select id="SelectAllPerson2" resultMap="SelectAllResult">
select
PER_ID AS Id,
PER_FIRST_NAME AS FirstName,
PER_LAST_NAME AS LastName,
PER_BIRTH_DATE AS BirthDate,
PER_WEIGHT_KG AS WeightInKilograms,
PER_HEIGHT_M AS HeightInMeters
from PERSON
<dynamic prepend="WHERE">
<isParameterPresent>
PER_FIRST_NAME LIKE #FirstName#+'%'
</isParameterPresent>
</dynamic>
</select>
<select id="SelectByPersonId" resultClass="Person" parameterClass="int">
select
PER_ID,
PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M
from PERSON
where PER_ID = #value#
</select>
<insert id="InsertPerson" parameterclass="Person">
<selectKey property="Id" type="post" resultClass="int">
${selectKey}
</selectKey>
insert into Person
( PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M)
values
(#FirstName#,#LastName#,#BirthDate#, #WeightInKilograms#, #HeightInMeters#)
</insert>
<update id="UpdatePerson"
parameterclass="Person">
<![CDATA[ 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="DeletePerson" parameterclass="Person">
delete from Person
where
PER_ID = #Id#
</delete>
</statements>
</sqlMap>
下面的文章将会整合log4net开发,并且更加详细。谢谢
回到正规话题上来,实例IBatis.net需要两个类库:IBatisNet.DataMapper.dll和IBatisNet.Common.dll,新建项目,我的项目结构如下:
View Code
<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="Person" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<alias>
<typeAlias alias="Person" type="Model.Person,Model"/>
</alias>
<resultMaps>
<resultMap id="SelectAllResult" 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="SelectAllPerson" resultMap="SelectAllResult">
select
PER_ID,
PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M
from PERSON
</select>
<select id="SelectAllPerson2" resultMap="SelectAllResult">
select
PER_ID AS Id,
PER_FIRST_NAME AS FirstName,
PER_LAST_NAME AS LastName,
PER_BIRTH_DATE AS BirthDate,
PER_WEIGHT_KG AS WeightInKilograms,
PER_HEIGHT_M AS HeightInMeters
from PERSON
<dynamic prepend="WHERE">
<isParameterPresent>
PER_FIRST_NAME LIKE #FirstName#+'%'
</isParameterPresent>
</dynamic>
</select>
<select id="SelectByPersonId" resultClass="Person" parameterClass="int">
select
PER_ID,
PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M
from PERSON
where PER_ID = #value#
</select>
<insert id="InsertPerson" parameterclass="Person">
<selectKey property="Id" type="post" resultClass="int">
${selectKey}
</selectKey>
insert into Person
( PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M)
values
(#FirstName#,#LastName#,#BirthDate#, #WeightInKilograms#, #HeightInMeters#)
</insert>
<update id="UpdatePerson"
parameterclass="Person">
<![CDATA[ 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="DeletePerson" parameterclass="Person">
delete from Person
where
PER_ID = #Id#
</delete>
</statements>
</sqlMap>
下面的文章将会整合log4net开发,并且更加详细。谢谢
相关文章推荐
- C#.Net项目生成器(ibatis)使用说明
- FCKeditor.NET 2.5使用说明
- FCKeditor 在ASP.Net 中的使用说明
- FCKeditor 在ASP.Net 中的使用说明
- Ibatis2.0使用说明(二)——配置篇(2)[原]
- Net的Collection类的一些使用说明
- Ibatis2.0使用说明——配置篇(1)
- ASP.Net 中Frames 的一些使用说明...
- iBatis.NET在使用浮点类型时报错
- FCKeditor使用说明http://blog.csdn.net/jhaij/archive/2007/05/01/1594124.aspx
- 基于DotNet构件技术的企业级敏捷软件开发平台 - AgileEAS.NET - 4.0 最新发布版本 -下载使用说明
- FCKeditor 在ASP.Net 中的使用说明
- ASP.NET笔记之Calender的使用说明
- Jquery中getJSON在asp.net中的使用说明
- ASP.NET MVC @helper使用说明
- Ibatis2.0使用说明(一)——入门实例篇[原]
- iBatis.NET中使用时间的注意事项
- Ibatis2.0使用说明(二)——配置篇(3)[原]
- iBATIS.NET Tips & Tricks(1) : 使用Nullable类型
- Ibatis2.0使用说明——配置篇(2)