您的位置:首页 > 其它

ibatis mybatis区别1

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

   1、标签使用上的不同

         1)ibatis:

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

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

             示例:    

[html]
view plaincopyprint?





<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">  
         <isNullpropertyisNullproperty="username">customer IS NOT NULL</isNull>  
         <isNotNullpropertyisNotNullproperty="username">  
            orders.customer=#username#    
         </isNotNull>  
     </dynamic>  
 </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

[html]
view plaincopyprint?

<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>  

<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)

[html]
view plaincopyprint?

<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>    

<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)

[html]
view plaincopyprint?

<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>    

<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

[html]
view plaincopyprint?

<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>   

<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代码

[html]
view plaincopyprint?

<select id="dynamicForeachTest" resultType="Blog">     
      select * from t_blog where id in     
      <foreachcollectionforeachcollectionforeachcollectionforeachcollection="list" index="index" item="item" open="(" separator="," close=")">    
           #{item}     
      </foreach>     
  </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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: