iBatis把一个表的sqlmap配置的多个xml中。
背景:
在日新月异的今天,作为程序员的,为了提高工作效率,我们需要努力地去“偷懒”,于是我们想尽办法去使用各种各样的工作方式、工作方法,因此,我们会使用到代码生成器,由于软件的项目是时刻变化着的,因此,很多时候需要去修改表结构,而iBatis配置文件这种东西,基本上是使用代码生成器自动生成的,但由于项目有很多特殊性,因此,我们不推荐把特殊性的SQL直接配置到代码生成器所生成的文件中去,因此,我们需要把某个表的iBatis配置,配置到另外一个xml中去。
实现:
其实我们只需要在iBatis配置中添加多一个xml就可以。但值得注意的是,必须把代码生成器生成的基础xml配置在特殊的xml配置之前,否则,特殊的xml使用基础xml中的resultMap时会报错。因为iBatis在初始化的时候,是根据<sqlMaps>的顺序逐个初始化的。
下面举个例子。虽然不是特殊的xml,但我还是把一个表的sql分开到2个xml中去进行配置:
下面是iBatis的<sqlMaps>的配置代码:
<sqlMaps>
<sqlMap resource="Maps/Person_Auto.xml"/>
<sqlMap resource="Maps/Person.xml"/>
</sqlMaps>
<sqlMap resource="Maps/Person_Auto.xml"/>
<sqlMap resource="Maps/Person.xml"/>
</sqlMaps>
基础xml配置(Person_Auto.xml):
代码
<?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="piggyWinFormTemplet.Model.Person, piggyWinForm" />
</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="SelectByPrimaryKey" 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:int#,
#FirstName:varchar#,
#LastName:varchar#,
#BirthDate:smalldatetime#,
#WeightInKilograms:decimal#,
#HeightInMeters:decimal#
)
</insert>
<update id="Update" parameterClass="Person" resultClass="int">
update Person set
<isNotNull property="FirstName" >PER_FIRST_NAME=#FirstName:varchar#</isNotNull>
<isNotNull property="LastName" prepend=",">PER_LAST_NAME=#LastName:varchar#</isNotNull>
<isNotNull property="BirthDate" prepend=",">PER_BIRTH_DATE=#BirthDate:smalldatetime#</isNotNull>
<isNotNull property="WeightInKilograms" prepend=",">PER_WEIGHT_KG=#WeightInKilograms:decimal#</isNotNull>
<isNotNull property="HeightInMeters" prepend=",">PER_HEIGHT_M=#HeightInMeters:decimal#</isNotNull>
where PER_ID=#Id:int#
</update>
<delete id="Delete" parameterClass="int" resultClass="int">
delete from PERSON
where PER_ID = #value#
</delete>
</statements>
</sqlMap>
<sqlMap namespace="Person" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<alias>
<typeAlias alias="Person" type="piggyWinFormTemplet.Model.Person, piggyWinForm" />
</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="SelectByPrimaryKey" 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:int#,
#FirstName:varchar#,
#LastName:varchar#,
#BirthDate:smalldatetime#,
#WeightInKilograms:decimal#,
#HeightInMeters:decimal#
)
</insert>
<update id="Update" parameterClass="Person" resultClass="int">
update Person set
<isNotNull property="FirstName" >PER_FIRST_NAME=#FirstName:varchar#</isNotNull>
<isNotNull property="LastName" prepend=",">PER_LAST_NAME=#LastName:varchar#</isNotNull>
<isNotNull property="BirthDate" prepend=",">PER_BIRTH_DATE=#BirthDate:smalldatetime#</isNotNull>
<isNotNull property="WeightInKilograms" prepend=",">PER_WEIGHT_KG=#WeightInKilograms:decimal#</isNotNull>
<isNotNull property="HeightInMeters" prepend=",">PER_HEIGHT_M=#HeightInMeters:decimal#</isNotNull>
where PER_ID=#Id:int#
</update>
<delete id="Delete" parameterClass="int" resultClass="int">
delete from PERSON
where PER_ID = #value#
</delete>
</statements>
</sqlMap>
特殊xml配置(Person.xml):
代码
<?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" >
<statements>
<select id="SelectAll" resultMap="SelectResult">
select
PER_ID,
PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M
from PERSON
</select>
</statements>
</sqlMap>
<sqlMap namespace="Person" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<statements>
<select id="SelectAll" resultMap="SelectResult">
select
PER_ID,
PER_FIRST_NAME,
PER_LAST_NAME,
PER_BIRTH_DATE,
PER_WEIGHT_KG,
PER_HEIGHT_M
from PERSON
</select>
</statements>
</sqlMap>
总结:类似的这种情况不只是出现在这种地方,我们,类似的还有我们的model类。我们加个字段,我们就得用代码生成器对其重新生成。我们最好还得派生出一个子类来继承该自动生成的类,把我们项目中的特殊性代码写在该派生类里,这样,我们用代码生成器生成后可以直接进行替换,不用担心把特殊性代码给覆盖掉。
原创作品出自努力偷懒,转载请说明文章出处:http://www.cnblogs.com/kfarvid/
分类: iBatis.NET