ibatis mybatis区别1

其实ibatis mybatis在项目中都使用了,但并没有留意他们之间的区别。现在有时间对ibatis及mybatis从使用层面及源码层面做细致的总结。

   1、标签使用上的不同

         1)ibatis:

              <dynamic>动态查询里面可能含有的标签:

              <isNull><isEmpty ><isNotEqual><isGreaterThan><isGreaterEqual><isLessThan><isLessEqual > 

             示例:    

  1. <select id="getOrders1"parameterClass="com.air.Account" resultClass="com.air.Product">  
  2.      SELECT     
  3.          orders.id as id,     
  4.          orders.product asproduct,    
  5.          orders.customer ascustomer     
  6.      FROM orders    
  7.      <dynamic prepend=" WHERE">  
  8.          <isNullpropertyisNullproperty="username">customer IS NOT NULL</isNull>  
  9.          <isNotNullpropertyisNotNullproperty="username">  
  10.             orders.customer=#username#    
  11.          </isNotNull>  
  12.      </dynamic>  
  13.  </select>  
   <select id="getOrders1"parameterClass="com.air.Account" resultClass="com.air.Product">
        SELECT   
            orders.id as id,   
            orders.product asproduct,  
            orders.customer ascustomer   
        FROM orders  
        <dynamic prepend=" WHERE">
            <isNullproperty="username">customer IS NOT NULL</isNull>
            <isNotNullproperty="username">
               orders.customer=#username#  
            </isNotNull>
        </dynamic>
    </select>
       2)mybatis:

             if

  1. <select id="findActiveBlogLike"     
  2.      parameterType="Blog" resultType="Blog">    
  3.   SELECT * FROM BLOG WHERE state = ‘ACTIVE’     
  4.   <if test="title != null">    
  5.     AND title like #{title}    
  6.   </if>    
  7.   <if test="author != null and author.name != null">    
  8.     AND author_name like #{author.name}    
  9.   </if>    
  10. </select>  
<select id="findActiveBlogLike"   
     parameterType="Blog" resultType="Blog">  
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’   
  <if test="title != null">  
    AND title like #{title}  
  </if>  
  <if test="author != null and author.name != null">  
    AND author_name like #{author.name}  
  </if>  
</select>

choose (when, otherwise)

  1. <select id="findActiveBlogLike"     
  2.      parameterType="Blog" resultType="Blog">    
  3.   SELECT * FROM BLOG WHERE state = ‘ACTIVE’    
  4.   <choose>    
  5.     <when test="title != null">    
  6.       AND title like #{title}    
  7.     </when>    
  8.     <when test="author != null and author.name != null">    
  9.       AND author_name like #{author.name}    
  10.     </when>    
  11.     <otherwise>    
  12.       AND featured = 1    
  13.     </otherwise>    
  14.   </choose>    
  15. </select>    
<select id="findActiveBlogLike"   
     parameterType="Blog" resultType="Blog">  
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’  
  <choose>  
    <when test="title != null">  
      AND title like #{title}  
    </when>  
    <when test="author != null and author.name != null">  
      AND author_name like #{author.name}  
    </when>  
    <otherwise>  
      AND featured = 1  
    </otherwise>  
  </choose>  
</select>  

trim (where, set)

  1. <select id="findActiveBlogLike"     
  2.      parameterType="Blog" resultType="Blog">    
  3.   SELECT * FROM BLOG     
  4.   <where>     
  5.     <if test="state != null">    
  6.          state = #{state}    
  7.     </if>     
  8.     <if test="title != null">    
  9.         AND title like #{title}    
  10.     </if>    
  11.     <if test="author != null and author.name != null">    
  12.         AND author_name like #{author.name}    
  13.     </if>    
  14.   </where>    
  15. </select>    
<select id="findActiveBlogLike"   
     parameterType="Blog" resultType="Blog">  
  SELECT * FROM BLOG   
  <where>   
    <if test="state != null">  
         state = #{state}  
    </if>   
    <if test="title != null">  
        AND title like #{title}  
    </if>  
    <if test="author != null and author.name != null">  
        AND author_name like #{author.name}  
    </if>  
  </where>  
</select>  

foreach

  1. <select id="selectPostIn" resultType="domain.blog.Post">    
  2.   SELECT *    
  3.   FROM POST P    
  4.   WHERE ID in    
  5.   <foreach item="item" index="index" collection="list"    
  6.       open="(" separator="," close=")">    
  7.         #{item}    
  8.   </foreach>    
  9. </select>   
<select id="selectPostIn" resultType="domain.blog.Post">  
  SELECT *  
  FROM POST P  
  WHERE ID in  
  <foreach item="item" index="index" collection="list"  
      open="(" separator="," close=")">  
        #{item}  
  </foreach>  
</select> 

foreach 元素是非常强大的 , 它允许你指定一个集合 , 声明集合项和索引变量 , 它们可 以用在元素体内。它也允许你指定开放和关闭的字符串 , 在迭代之间放置分隔符。这个元素 是很智能的 , 它不会偶然地附加多余的分隔符。

item表示集合中每一个元素进行迭代时的别名, index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
1.如果传入的是一个参数且参数类型是一个List的时候,collection属性值为list 
2.如果传入的是一个参数且参数类型是一个array数组的时候,collection的属性值为array 
3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在breast里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key
单参数List的类型:

java代码

public List<Blog> dynamicForeachTest(List<Integer> ids); 

xml代码

  1. <select id="dynamicForeachTest" resultType="Blog">     
  2.       select * from t_blog where id in     
  3.       <foreachcollectionforeachcollectionforeachcollectionforeachcollection="list" index="index" item="item" open="(" separator="," close=")">    
  4.            #{item}     
  5.       </foreach>     
  6.   </select>     
<select id="dynamicForeachTest" resultType="Blog">   
      select * from t_blog where id in   
      <foreachcollectionforeachcollection="list" index="index" item="item" open="(" separator="," close=")">  
           #{item}   
      </foreach>   
  </select>   




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值