ibatis2.X升级mybatis3.X之动态SQL摸着石头过河

        参照第一篇《曲径通幽》完成第一步修改后就参照Google Code的官方资料(From iBATIS2.X to MyBatis)进行修改,例如将sqlmap中的<typeAlias>添加到<configuration></configuration>中。
在修改的过程中遇到的难题就是动态SQL,文档中只给了一部分修改方案,所以要自己查资料,看前人是怎么修改的,找不到的更要自己摸索,摸着石头过河。下面列出在项目中遇到的几个修改方案: 

1.<select id=”” resultMap=”” parameterClass=””>

select * from user where user_id = #id#

<isNotEmpty prepend="and" property="name">name=#name# </isNotEmpty>

</select>

改成:<select id=”” resultMap=”” parameterType=””>

select * from user where user_id = #{id}

//2011-9-22 修改

<if test="name != null">and name=#{name} </if>

替换成:<if test="name != null and name !=''">and name=#{name} </if>

</select>

2.where语句

 <select>

   Select * from user

<isParamaterPresent pretend=”where” removeFirstPrepend=”true”>

  <if prepend=”and” property=”name”>name=#name#</if>

</isParameterPresent>

 </select>

改成:<select>

        Select * from user

<where >

         <if test=”name != null”>name=#{name}</if>

</where>

       </select>

3. update语句中的:<dynamic prepend="set"></ dynamic>改成<set></set>

4.iterate改成foreach

<iterate property="id" prepend="where USER_ID not in " open="(" close=")" conjunction=",">#id[]#</iterate>

改成:

where user_id not in

<foreach collection="list" index="index" item="id" open="(" separator="," close=")"> 

  #{id}</foreach>

5.isGreaterThan>,<等运算符代替

<isGreaterThan prepend="and" property="regMoney" compareValue="0.0">

    REG_MONEY=#regMoney#

</isGreaterThan>

改成:<if test="regMoneym != null and regMoneym >0.0">and REG_MONEY=#{regMoney}</if>

6.<statement></statement>改成<select></select>

项目中多次用到<statement>标签,但是标签下的语句都是select语句,所以直接用<select>标签替换掉。

 

你可能感兴趣的:(ibatis2.X升级mybatis3.X之动态SQL摸着石头过河)