首页 > 数据库技术 > 详细

III-MyBatis 动态sql语句

时间:2015-07-22 14:53:09      阅读:317      评论:0      收藏:0      [点我收藏+]

mybatis 的动态sql语句是基于OGNL表达式的。 分以下几类:

1. if 语句 (简单的条件判断)
<if test="itemOid != null">
    item_oid = #{itemOid}
</if> //item_oid 为表的列名,itemOid为传入参数的属性名
2. choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中的choose 很类似.(所有的when和otherwise条件中,只有一个会输出)
<choose>
  <when test="title != null">
        and title = #{title}
  </when>
  <when test="content != null">
        and content = #{content}
  </when>
  <otherwise>
        and owner = "owner1"
  </otherwise>
</choose>
3. trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)
<trim prefix="where" prefixOverrides="and |or" suffix="">
</trim>
4. where (主要是用来简化sql语句中where条件判断的,能智能的处理 and or ,不必担心多余导致语法错误)
<where>
    <if test="title != null">
        and title = #{title}
	</if>
    <if test="content != null">
        and content = #{content}
    </if>
</where>
5. set (主要用于更新时)
<set>
    <if test="title != null">
        title = #{title},
    </if>
    <if test="content != null">
        content = #{content},
    </if>
    <if test="owner != null">
        owner = #{owner}
    </if>
</set>
6. foreach (在实现 mybatis in 语句查询时特别有用)
where id in
<foreach collection="list" index="index" item="temp" open="(" separator="," close=")">
    #{temp}
</foreach>

III-MyBatis 动态sql语句

原文:http://my.oschina.net/u/1384818/blog/482195

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!