现在的位置: 首页 > 综合 > 正文

从IBatis2.X 移植到IBatis3.0 sqlMapConfig and sqlMap XML 配置文件升级说明

2012年10月21日 ⁄ 综合 ⁄ 共 4169字 ⁄ 字号 评论关闭

以下将介绍 从Ibatis2.X 升级到 Ibatis3.0 配置文件的升级,这仅仅是一个初步的修改意见,我已经决定并开始移植工作,

我没有很多的时间去测试它,所以目前我不能保证他完全正确,但是我会通过你们的建议让他逐步的完善准确.

新的 sqlMapConfig.xml DTD:

<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">

新的 sqlMap (*.map.xml) DTD:

<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD iBatis Mapper 3.0 //EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
Configuration XML 变化:
1、根节点标签 由 <sqlMapConfig>  更改为  <configuration>
2、<settings x="y" foo="bar"/>  更改为如下写法

<settings>

    <setting name="x" value="y"/>

    <setting name="foo" value="bar"/>

</settings>

3、<typeAlias> 标签必须从 <sqlMap> 节点移动到 <configuration><typeAliases></typeAliases></configuration> 内

如下示例:

<configuration>
	<settings>
	...
	</settings>
	<typeAliases>
		<typeAlias ... />
	</typeAliases>
</configuration>
 
Mapping XML 变化

1、根节点由 <sqlMap>  更改为 <mapper>

2、属性 parameterClass 必须更改为 parameterType

3、属性 resultClass 必须更改为 resultType

4、属性 class 必须更改为 type

5、"groupBy" 属性已经删除.

在Ibatis2.X 时候,groupBy 使用方式如下

<resultMap id="productRM" type="product" groupBy="id">
     <result property="id" column="product_id"/>
     <result property="name" column="product_name"/>
     <result property="category" column="product_category"/>
     <result property="subProducts" resultMap="Products.subProductsRM"/>
</resultMap>

在3.0中使用方式如下:

<resultMap id="productRM" type="product" >
	<id property="id" column="product "/>
	<result property="name " column="product_name "/>
	<result property="category " column="product_category "/>
	<collection property="subProducts" resultMap="Products.subProductsRM"/>
</resultMap>

其他的变更对比如下:

Ibatis2.X:

<resultMap id="invoiceRM" type="invoice" extends="Invoice.abstractInvoiceRM">
	<result property="client" resultMap="Client.clientRM"/>
	<result property="accounts" column="invoiceNumber=INVOICE_NUMBER" select="Invoice.getAccountsSql"/>
	<result property="products" column="productGroup=PRODUCT_GROUP_ID" select="Invoice.getProductsSql"/>
</resultMap>

Ibatis3.0:

<resultMap id="agreementDetailRM" type="agreement" extends="Agreement.agreementRM">
	<association property="client" resultMap="Agreement.clientRM"/>
	<collection property="accounts" column="agreementNumber=AGREEMENT_NUMBER" select="Agreement.getAccountsSql"/>
	<collection property="products" column="serviceGroupId=SERVICE_GROUP_ID" select="Agreement.getProductsSql"/>
</resultMap>

上面的示例中 id被定义在父 result map 中.

Dynamic SQL 的变化:

项目中最经常使用的动态语句是 "isNotNull". 此处将给出一个替代的方案:

比如下面的写法:

<isNotNull.*?property=\"(.*?)\"

可以这样写:

<if test="$1 != null"
注意:如果使用了<if ..> 关闭标签也必须由</isNotNull> 更改为</if>
 
原文地址:http://opensource.atlassian.com/confluence/oss/display/IBATIS/Porting+sqlMapConfig+and+sqlMap+XML+from+2.x+to+3.0
用自己蹩脚的英文加工具翻译的本篇文章 以备自己查阅方便,如有翻译不当之处望指正.

抱歉!评论已关闭.