本节主要讲了动态SQL的几个标签:where set trim

where: 检出where语句的最前面是否含有AND和一个空格 或者 or和一个空格 ,如果有的话删除

set: 检出set的最后是否有逗号 ,如果有,则清空

trim:可用来替换where和set

foreach:遍历集合(array,list,key)

CDATA:不支持标签,用于有<等需要&lt;时

where 和 if 条件查询

<select id="whereMore01" resultType="com.shxt.model.User">
  SELECT
  *
  FROM user
  <where>
    <if test="user_name!=null and user_name.length() >0">
      AND user_name like CONCAT(#{user_name},'%')
    </if>
    <!-- &amp;&amp; 不推荐记忆 &lt;< -->
    <if test="account!=null and account.length() > 0">
      AND account = #{account}
    </if>
    </where>
</select>

set 和 if 更新对象信息

<update id="update01" parameterType="com.shxt.model.User" >
  UPDATE user
  <set>
    <if test="account!=null">
      account =#{account},
    </if>
    <if test="password!=null">
      password = #{password},
    </if>
    <if test="user_name!=null">
      user_name =#{user_name},
    </if>

  </set>
  WHERE
  id = #{id}
</update>

trim替换

  替换条件查询where

<select id="whereMore02" resultType="com.shxt.model.User">
  SELECT
  *
  FROM user
  <trim prefix="WHERE" prefixOverrides="AND |OR ">
    <if test="user_name!=null and user_name.length() >0">
      AND user_name like CONCAT(#{user_name},'%')
    </if>
    <!-- &amp;&amp; 不推荐记忆 &lt;< -->
    <if test="account!=null and account.length() > 0">
      AND account = #{account}
    </if>
  </trim>
</select>

  替换更新操作set

<update id="update02" parameterType="com.shxt.model.User" >
  UPDATE user
  <trim prefix="set" suffixOverrides=",">
    <if test="account!=null">
      account =#{account},
    </if>
    <if test="password!=null">
      password = #{password},
    </if>
    <if test="user_name!=null">
      user_name =#{user_name},
    </if>
  </trim>
  WHERE
  id = #{id}
</update>

foreach遍历in(2,3,4,5)

<select id="collectionTest" resultType="User">

  select * from user where id in

  <foreach collection="list|array|key" index = "index" item="us" open="(" close=")" separator=",">

    #{us}

  </foreach>

</select>

CDATA:查询小于id的集合

  <![CDATA[

    select * from user where id < #{id}

  ]]>

动态SQL之标签的更多相关文章

  1. Mybatis学习笔记之---动态sql中标签的使用

    动态Sql语句中标签的使用 (一)常用标签 1.<if> if标签通常用于WHERE语句中,通过判断参数值来决定是否使用某个查询条件, 他也经常用于UPDATE语句中判断是否更新某一个字段 ...

  2. 动态SQL常用标签

    动态 SQL 目的:为了摆脱在不同条件拼接 SQL 语句的痛苦 在不同条件在生成不同的SQL语句 本质上仍然是SQL语句,不过是多了逻辑代码去拼接SQL,只要保证SQL的正确性按照格式去排列组合 可以 ...

  3. 动态SQL各个标签作用以及注意事项详解

    创建com.mybatis包,包含:UserMapper.xml和mybatis-config.xml UserMapper.xml代码: <?xml version="1.0&quo ...

  4. MyBatis动态SQL foreach标签实现批量插入

    需求:查出给定id的记录: <select id="getEmpsByConditionForeach" resultType="com.test.beans.Em ...

  5. Mybatis 最强大的动态sql <where>标签

    <select id="findActiveBlogLike" resultType="Blog"> SELECT * FROM BLOG WHER ...

  6. [刘阳Java]_MyBatis_动态SQL标签用法_第7讲

    1.MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. 2.MyBatis中用于实现动态SQL的元素主要有 if choose(when,otherwi ...

  7. Mybatis:缓存,动态SQL,注解SQL以及动态标签使用

    1 转义字符 字符 转义 描述 < < 小于 <= <= 小于等于 > > 大于 >= >= 大于等于 <> <> 不等于 &a ...

  8. Mybatis系列全解(八):Mybatis的9大动态SQL标签你知道几个?提前致女神!

    封面:洛小汐 作者:潘潘 2021年,仰望天空,脚踏实地. 这算是春节后首篇 Mybatis 文了~ 跨了个年感觉写了有半个世纪 ... 借着女神节 ヾ(◍°∇°◍)ノ゙ 提前祝男神女神们越靓越富越嗨 ...

  9. 【ibatis】IBatis的动态SQL的写法

    Ⅰ .动态SQL的写法 开始 <dynamic 条件成立时前面要加的字符串 prepend ="字符串"> prepend="字符串" 判断条件的对 ...

随机推荐

  1. word 无法显示图片问题解决

    1 打开Word文档,点击“Office按钮”→“Word选项”. 2 在打开的“Word选项”对话框中,点击左侧的“高级”选项卡,在右侧找到“显示文档内容”栏目,取消勾选“显示图片框”.

  2. ZSDR101-跑成品MRP

    *&---------------------------------------------------------------------**& Report ZSDR101*&a ...

  3. php 小知识积累

    1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row['id']的速度是$row[id]的7倍. 3.echo比print快,并且使用echo的多重 ...

  4. 349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  5. Kernel panic - not syncing: Attempted to kill init

    解决方法:系统启动的时候,按下‘e’键进入grub编辑界面,编辑grub菜单,选择“kernel /vmlinuz-2.6.23.1-42.fc8 ro root=/dev/vogroup00/log ...

  6. 救援行动(save) (BFS)

    时间限制: 1 Sec  内存限制: 64 MB提交: 42  解决: 9[提交][状态][讨论版] 题目描述 Angel被人抓住关在一个迷宫了!迷宫的长.宽均不超过200,迷宫中有不可以越过的墙以及 ...

  7. POJ-1741 Tree (树上点分治)

    题目大意:一棵带边权无根树,边权代表距离,求距离小于等于k的点对儿数. 题目分析:这两个点之间的路径只有两种可能,要么经过根节点,要么在一棵子树内.定义depth(i)表示点 i 到根节点的距离,be ...

  8. Oracle学习系列2

    SQL语法练习: 1,选择部门30中的所有员工 select * from emp where deptno=30; 2,列出办事员的姓名,编号和部门编号 select ename, empno, d ...

  9. CSS 通用原子类

    /*文字排版*/.f10 { font-size: 10px; }.f11 { font-size: 11px; }.f12 { font-size: 12px; }.f13 { font-size: ...

  10. svn常见错误总结

    1. svn: Server sent unexpected return value (500 Internal Server Error) in response to OPTIONS reque ...