MyBatis的动态SQL查询-让查询更灵活多变!

来源:互联网 发布:elasticsearch sql 编辑:程序博客网 时间:2024/06/10 06:48

序言 

      MyBatis,大家都知道,半自动的ORM框架,原来叫ibatis,后来好像是10年apache软件基金组织把它托管给了goole code,就重新命名了MyBatis,功能相对以前更强大了。它相对全自动的持久层框架hibernate,更加灵活,更轻量级,这点我还是深有体会的。

       MyBatis的一个强大特性之一就是动态SQL能力了,能省去我们很多串联判断拼接SQL的痛苦,根据项目而定,在一定的场合下使用,能大大减少程序的代码量和复杂程度,不过还是不是过度太过复杂的使用,以免不利于后期的维护和扩展。

下边就简单介绍一下吧。

介绍

foreach 批量处理

<foreach> 元素是非常强大的,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内。它也允许你指定开放和关闭的字符串,在迭代之间放置分隔符。这个元素是很智能的,它不会偶然地附加多余的分隔符。注意:你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时候,MyBatis 会自动将它包装在一个Map 中,用名称在作为键。List 实例将会以“list”作为键,而数组实例将会以“array”作为键。最常用在批量删除和批量插入功能上,如下:

[html] view plain copy
  1. <!-- 批量删除数据 -->  
  2. <delete id="batchDelete">  
  3.     delete from test where id in  
  4.     <foreach collection="list" index="index" item="item" open="(" separator="," close=")">  
  5.         #{item}  
  6.     </foreach>  
  7. </delete>  


 

[html] view plain copy
  1.        <!--批量插入数据-->  
  2. <insert id="batchInsert">  
  3.     insert into test (id, name, year,birthday) values  
  4.     <foreach collection="list" item="item" index="index" separator=",">  
  5.         (#{id}, #{name},#{year},#{birthday,jdbcType=DATE})  
  6.     </foreach>  
  7. </insert>  


if经常判空使用

[html] view plain copy
  1. <!--更新数据,根据传入条件选择性更新数据,如id为null,则更新全部数据-->  
  2. <update id="update" parameterType="com.inspur.demo.po.ExampleBean" >  
  3.     update test set name=#{name},year=to_number(#{year}),birthday=to_date(#{birthday},'yyyy-mm-dd hh24:mi:ss')  
  4.     <if test="id!=null">  
  5.         where id = #{id}  
  6.     </if>  
  7. </update>  

 

where set的使用

       <where> 元素知道如果由被包含的标记返回任意内容,就仅仅插入“WHERE” 。而且,如果以“AND”或“OR”开头的内容,那么就会跳过 WHERE 不插入。

[html] view plain copy
  1. <where>  
  2.     <if test="id!= null">  
  3.         m.id=#{id}  
  4.     </if>  
  5.     <if test="name!= null">  
  6.         and m.name like '%${name}%'  
  7.     </if>  
  8.     <if test="year!= null">  
  9.         and m.year=#{year,jdbcType=INTEGER}  
  10.     </if>  
  11.     <if test="birthday!= null">  
  12.         <![CDATA[and to_char(m.birthday,'yyyy-MM-dd') < #{birthday}]]>  
  13.     </if>  
  14. </where>  


 

       <set> 元素可以被用于动态包含更新的列,而不包含不需更新的。

[html] view plain copy
  1.       <!--更新数据,根据传入条件选择性更新数据,如id为null,则更新全部数据-->  
  2. lt;update id="update" parameterType="com.inspur.demo.po.ExampleBean" >  
  3.    update test  
  4.     <set>  
  5.     <if test="name!= null">name=#{name},</if>  
  6.     <if test="year!= null">year=#{year,jdbcType=INTEGER},</if>  
  7.     <if test="birthday!= null">birthday=#{birthday,jdbcType=DATE}</if>  
  8. </set>  
  9. <if test="id=!null" >  
  10.     where id=#{id}  
  11. </if>  
  12. lt;/update>  


choose when的使用(相对来说用的少)

[html] view plain copy
  1. <!-- 一些情况可选择choose,when,otherwise的使用 -->  
  2. <select id="getByBean" parameterType="com.inspur.demo.po.ExampleBean" resultType="map">  
  3.     select m.* from test m where 1=1  
  4.     <choose>  
  5.         <when test="判断1">  
  6.             and 条件1  
  7.         </when>  
  8.         <when test="判断2">  
  9.             and 条件2  
  10.         </when>  
  11.         <otherwise>  
  12.             and 条件3  
  13.         </otherwise>  
  14.     </choose>  
  15. </select>  


 

转载请注明—作者:Java我人生(陈磊兴)   原文出处:http://blog.csdn.net/chenleixing/article/details/43818227

原创粉丝点击