if 标签
if标签中有一个test属性,test属性值是一个符合OGNL要求的判断表达式,表达式的结果可以使true或者false, 除此之外所有的非0值都为true

(1)、数字类型

1.1 例如:  如果参数为数字类型的时候没有特俗需求的情况只需要判断是否为null即可。
<if test="id != null"></if>
 1.2 例如:如果有特俗需求,例如判断是否大于某个数的时候才行。只需要加上对应的条件判断即可。
<if test='id != null and id > 28'></if>
 1.3 例如:mybatis对于这种大于小于等等还有另一种形式。<if test='id != null and id gt 28'></if>
<if test='id != null and id > 28'></if> 这两种一样<if test='id != null and id gte 28'></if>
<if test='id != null and id >= 28'></if> 这两种一样<if test='id != null and id lt 28'></if> 正常
<if test='id != null and id < 28'></if> 报错<if test='id != null and id lte 28'></if> 正常
<if test='id != null and id <= 28'></if> 报错

对应关系:

gt          对应             >gte         对应              >=lt           对应             <(会报错  相关联的 "test" 属性值不能包含 '<' 字符)lte          对应             <=(会报错  相关联的 "test" 属性值不能包含 '<=' 字符)

(2) 字符串类型

2.1 例如: 如果不需要过滤空串的情况 仅仅判断null即可<if test="username != null"></if>
2.2 例如:如果需要过滤空串,添加空串判断即可  不支持 &&和 || ,所以这里用 and  or 来做逻辑与或的判断 <if test="username != null and '' != username"></if> 或者 <if test="username != null and ''  neq username"></if>
2.3 例如:如果判断字符串是否已某个特俗字符开头,结尾等。直接调用String的对应方法即可<if test="username != null and username.indexOf('ji') == 0"> </if> <!-- 是否以什么开头 --><if test="username != null and username.indexOf('ji') >= 0"> </if> <!-- 是否包含某字符 --><if test="username != null and username.lastIndexOf('ji') > 0"></if>  <!-- 是否以什么结尾 -->
2.4 例如: 是否是某个特定字符串,某些业务有此需要。<if test="username != null and 'hello' == username"></if> 或者<if test="username != null and 'hello' eq username"></if>注意:<if test="username != null and 'hello' == username"></if>这种形式的写法在参数类型是字符串的时候是没有问题的,但是参数类型为非字符串类型的时候就需要写成 <if test="username != null and 'hello'.toString() == username.toString()"></if>仅仅写成<if test="username != null and 'hello'.toString() == username"></if>也会有很大可能会挂。也许你会说非字符串的为什么要写成这样。这就要看特殊需要了。

对应关系:

eq                  对应                ==neq               对应                 !=
 3 判断list是否为空if条件判断可以直接调用对象自身的方法进行逻辑判断,所以list判空。可以调用.size()>0或者.isEmpty()例如:<if test="userList != null and userList.isNotEmpty()"></if> , <if test="userList != null and userList.size()>0"></if>
4 map参数同同理  取值的话 map.key(map中的key名字)即可

where标签
标签会进行自动判断, 如果任何条件都不成立,那么就在sql语句里就不会出现where关键字
如果有任何条件成立,会自动去掉多出来的 and 或者 or。(就不需要我们追加1=1之类的入侵性代码了)

用法:
<select id="listProduct" resultType="Product">select * from product_<where><if test="name!=null">and name like concat('%',#{name},'%')</if>        <if test="price!=null">and price > #{price}</if></where>
</select>

set标签
与where标签类似的,在update语句里也会碰到多个字段相关的问题。 在这种情况下,就可以使用set标签。 其效果与where标签类似,有数据的时候才进行设置。 set 元素可以用于动态包含需要更新的列,忽略其它不更新的列,set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号。

用法:<update id="updateProduct" parameterType="Product" >update product_<set><if test="name != null">name=#{name},</if><if test="price != null">price=#{price}</if> </set>where id=#{id}
</update>

trim标签
trim 有四个参数,分别是:
prefix:前缀(以什么开头), 
prefixoverride:去掉第一个(比如 “and”或者是“or”)
suffix:后缀(以什么结尾)
suffixoverride:去掉最后标记的字符(比如“,”)

用法:
<select id="listProduct" resultType="Product">select *from product_<trim prefix="WHERE" prefixOverrides="AND |OR "><if test="name!=null">and name like concat('%',#{name},'%')</if>        <if test="price!=null and price!=0">and price > #{price}</if></trim>
</select>trim 用来定制想要的功能,比如where标签就可以用trim 来替换<trim prefix="WHERE" prefixOverrides="AND |OR ">...
</trim>
<update id="updateProduct" parameterType="Product" >update product_<trim prefix="SET" suffixOverrides=","><if test="name != null">name=#{name},</if><if test="price != null">price=#{price},</if>   </trim>where id=#{id}
</update>set标签就可以用trim来替换 ,运行set标签中的代码,其效果是一样的。
<trim prefix="SET" suffixOverrides=",">...
</trim>

choose when otherwise 标签
有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。if是与(and)的关系,而choose是或(or)的关系

<select id="getUserList" resultType="com.it.bean.User" parameterType="com.it.bean.User">  SELECT <include refid="resultParam"></include> FROM User u   <where>  <choose>  <when test="username !=null and username != ''">  u.username LIKE CONCAT(CONCAT('%', #{username}),'%')  </when >  <when test="sex != null">  AND u.sex = #{sex}  </when >  <when test="birthday != null ">  AND u.birthday = #{birthday}  </when >  <otherwise> AND u.age = #{age}</otherwise>  </choose>  </where>
</select>

foreach标签
foreach标签通常用于in 这样的语法里。
collection :collection属性的值有三个分别是list、array、map三种,分别对应的参数类型为:List、数组、map集合,我在上面传的参数为数组,所以值为array

item : 表示在迭代过程中每一个元素的别名

index :表示在迭代过程中每次迭代到的位置(下标)

open :前缀

close :后缀

separator :分隔符,表示迭代时每个元素之间以什么分隔

<select id="listProduct" resultType="Product">SELECT * FROM product_WHERE ID in<foreach item="item" index="index" collection="list"open="(" separator="," close=")">#{item}</foreach>
</select>

bind标签
bind标签中,value对应传入实体类的某个字段,name属性既给对应字段取的变量名。在value属性中可以使用字符串拼接等特殊处理。

用法:<select id="listProduct" resultType="Product"><bind name="likename" value="'%' + name + '%'" />select * from   product_  where name like #{likename}</select>

sql片段标签
通过该标签可定义能复用的sql语句片段,在执行sql语句标签中直接引用即可。
这样既可以提高编码效率,还能有效简化代码,提高可读性。 sql标签 用来封装SQL语句, include 标签来调用。

用法:
<!--定义sql片段-->
<sql id="orderAndItem">    o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count
</sql>
<select id="findOrderAndItemsByOid" parameterType="java.lang.String" resultMap="BaseResultMap">select
<!--引用sql片段--><include refid="orderAndItem" />from ordertable ojoin orderitem i on o.orderitem_id = i.orderitem_idwhere o.order_id = #{orderId}
</select>

mybatis 动态标签详解相关推荐

  1. mybatis注解动态sql_超全MyBatis动态SQL详解

    MyBatis 令人喜欢的一大特性就是动态 SQL.在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的.MyBatis 动态 SQL 的出现, 解决了这个麻烦. MyBa ...

  2. 超全MyBatis动态代理详解!(绝对干货)

    我的新课<C2C 电商系统微服务架构120天实战训练营>在公众号儒猿技术窝上线了,感兴趣的同学,可以长按扫描下方二维码了解课程详情: 课程大纲请参见文末 前言 假如有人问你这么几个问题,看 ...

  3. MyBatis动态SQL详解

    一:MyBatis动态 sql 是? 1.动态 SQL 是 MyBatis 的强大特性之一.在 JDBC 或其它类似的框架中,开发人员通常需要手动拼接 SQL 语句.根据不同的条件拼接 SQL 语句是 ...

  4. mysql动态代理_超全MyBatis动态代理详解(绝对干货)

    前言 假如有人问你这么几个问题,看能不能答上来 Mybatis Mapper 接口没有实现类,怎么实现的动态代理 JDK 动态代理为什么不能对类进行代理(充话费送的问题) 抽象类可不可以进行 JDK ...

  5. mybatis 九大动态标签详解

    一.if:你们能判断,我也能判断! <select id="count" resultType="java.lang.Integer">select ...

  6. Mybatis常用标签详解

    文章目录 命名空间 顶级元素 select insert, update 和 delete sql resultMap cache cache-ref 动态sql if标签 choose.when.o ...

  7. Mybatis的特性详解——动态SQL

    Mybatis的特性详解--动态SQL 前言 一.动态sql的元素 1.MyBatis if标签:条件判断 2.MyBatis choose.when和otherwise标签 3.MyBatis wh ...

  8. MyBatis SQL语句详解

    MyBatis SQL语句详解 1 CRUD标签 1.1 select 1.2 insert 1.3 update 1.4 delete 1.5 #{ }和${ }的区别 2 动态SQL 2.1 if ...

  9. W3C中meta标签详解

    2019独角兽企业重金招聘Python工程师标准>>> meta是html语言head区的一个辅助性标签.几乎所有的网页里,我们可以看到类似下面这段的html代码: <meta ...

最新文章

  1. python staticmethod and classmethod方法
  2. python用os修改文件名_python查找特定文件并修改文件名
  3. boost::hana::unfold_left用法的测试程序
  4. HDOJ2072解题报告【字典树】
  5. python html转换为普通文本_将HTML表转换为可读的纯文本的Python解决方案
  6. cf Round 613
  7. java 内部类 单例_确保对象的唯一性——单例模式 (四):一种更好的单例实现方法(静态内部类)...
  8. 开启MYSQL慢查询日志
  9. JAVA基础--toString, equals方法
  10. 飞秋命令行发送消息和文件
  11. Eclipse项目环境配置
  12. 【PyQt5】串口数据实时绘图
  13. 模糊C均值聚类 C++代码
  14. CentOS导入CA证书
  15. 浏览器功能大比拼:谁更出彩?
  16. DOM算法系列002-寻找指定DOM节点的上一个或下一个节点
  17. 模拟科目二倒车入库训练
  18. Hub能新建但不能打开项目 Failed to connect to pipe_20220313
  19. 读《臧圩人的Java面试题解惑系列》
  20. XSS漏洞简单概述--UGa

热门文章

  1. 关于一个基于http的人人刷人气工具
  2. 泰森多边形(Voronoi图)的matlab绘制
  3. 使用 Resharper 快速做适配器
  4. 微服务的理解 —— 江湖往事
  5. c语言中change的用法,change的用法
  6. TMD之后,再无BAT?
  7. NR 物理层编码 卷积码8-slide
  8. 网际互联及TCP/IP 协议OSI七层模型: 物理层、数据链路层、网络层、传输层、会话层、表示层、应用层
  9. 网络中常见攻击及其防御方式
  10. 远程桌面连服务器踩过的所有坑(一、win10升级专业版)