III-MyBatis 动态sql语句

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>

转载于:https://my.oschina.net/u/1384818/blog/482195

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值