MyBatis的9种动态标签详解

MyBatis的9种动态标签详解
⽬录
前⾔
动态标签⽤法
1.if
2.choose、when、otherwise
3.where
4.set
6.foreach
7.bind
前⾔
MyBatis提供了9种动态SQL标签:trim、where、set、foreach、if、choose、when、otherwise、bind;
其执⾏原理为,使⽤OGNL从SQL参数对象中计算表达式的值,根据表达式的值动态拼接SQL,以此来完成动态SQL的功能。动态标签⽤法
1.if
If : 当参数满⾜条件才会执⾏某个条件
<select id="findName" resultType="String">
SELECT stu.name FROM tab_stu stu WHERE age = 20
<if test="name != null">
AND name like #{name}
</if>
</select>
2.choose、when、otherwise
choose、when、otherwise : choose标签是按顺序判断其内部when标签中的test条件是否成⽴,如果有⼀个成⽴,则choose 结束;如果所有的when条件都不满⾜时,则执⾏otherwise中的SQL。类似于java的switch语句
<select id="findName" resultType="String">
SELECT stu.name FROM tab_stu stu WHERE age = #{age}
<choose>
<when test="name != null">
AND name like #{name}
</when>
<when test="class != null">
AND class like #{class}
</when>
bind9
<otherwise>
AND class = 1
</otherwise>
</choose>
</select>
3.where
<select id="findName" resultType="String">
SELECT stu.name FROM tab_stu stu WHERE
<if test="age != null">
age = #{age}
</if>
<if test="name!= null">
AND name= #{name}
</if>
<if test="class!= null">
AND class = #{class}
</if>
</select>
当第⼀个if不满或第⼀第⼆第三个if都不满⾜,会出现以下情况
SELECT stu.name FROM tab_stu stu WHERE AND name = "⼩⽶" AND class ="1班”;
SELECT stu.name FROM tab_stu stu WHERE;
这会导致查询失败。使⽤where标签可以解决这个问题
<select id="findName" resultType="String">
SELECT stu.name FROM tab_stu stu
<where>
矿业114<if test="age != null">
age = #{age}
</if>
<if test="name!= null">
AND name= #{name}
</if>
<if test="class!= null">
AND class = #{class}
</if>
</where>
</select>
where标签会在只有⼀个以上的if条件满⾜的情况下才去插⼊WHERE关键字,⽽且,若最后的内容是”AND”或”OR”开头
的,where也会根据语法绝对是否需要保留。
4.set
set标签⽤于解决动态更新语句存在的符号问题
<update id="updateStu">
Update tab_stu
<set>
<if test="name != null"> name=#{name},</if>
<if test="age != null"> age=#{age},</if>
<if test="class != null"> class=#{class},</if>
<if test="subject != null"> subject=#{subject}</if>
</set>
</update>
set标签会动态前置SET关键字,同时也会消除⽆关的逗号,因为⽤了条件语句后,可能就会在⽣成的赋值语句的后⾯留下逗号。
trim:trim标签可实现where/set标签的功能
Trim标签有4个属性,分别为prefix、suffix、prefixOverrides、suffixOverrides
prefix:表⽰在trim标签包裹的SQL前添加指定内容
suffix:表⽰在trim标签包裹的SQL末尾添加指定内容
prefixOverrides:表⽰去掉(覆盖)trim标签包裹的SQL指定⾸部内容,去掉多个内容写法为and |or(中间空格不能省略)(⼀般⽤于if判断时去掉多余的AND |OR)
suffixOverrides:表⽰去掉(覆盖)trim标签包裹的SQL指定尾部内容(⼀般⽤于update语句if判断时去掉多余的逗号)
<select id="findName" resultType="String">
SELECT stu.name FROM tab_stu stu
<trim prefix="where" prefixOverrides="and |or">
<if test="age != null">
age = #{age}
</if>
<if test="name!= null">
AND name= #{name}
</if>
<if test="class!= null">
OR class = #{class}
</if>
</trim>
</select>
<update id=”updateStu”>
Update tab_stu
<trim prefix="set" subfix="where id=#{id}" suffixOverrides=",">
<if test="name != null"> name=#{name},</if>
<if test="age != null"> age=#{age},</if>
ccbot<if test="class != null"> class=#{class},</if>
<if test="subject != null"> subject=#{subject}</if>
</trim>
</update>
6.foreach
foreach:对集合进⾏遍历
<select id="findName" resultType="String">
SELECT stu.name FROM tab_stu stu where id in
<foreach item=”item” index=”index” collection=”listName” open=”(” separator=”,” close=”)”>
#{item}
</foreach>
</select>
下⾯是foreach标签的各个属性:
collection:迭代集合的名称,可以使⽤@Param注解指定,该参数为必选(java⼊参,相对于#{listName})
item:表⽰本次迭代获取的元素,若collection为List、Set或数组,则表⽰其中元素;若collection为Map,则代表key-value的value,该参数为必选
index:在List、Set和数组中,index表⽰当前迭代的位置,在Map中,index指元素的key,该参数是可选项
open:表⽰该语句以什么开始,最常使⽤的是左括弧”(”,MyBatis会将该字符拼接到foreach标签包裹的SQL语句之前,并且只拼接⼀次,该参数是可选项
close:表⽰该语句以什么结束,最常使⽤的是右括弧”)”,MyBatis会将该字符拼接到foreach标签包裹的SQL语句末尾,该参数是可选项
separator:MyBatis会在每次迭代后给SQL语句添加上separator属性指定的字符,该参数是可选项
7.bind
bind:bind标签可以从OGNL(对象图导航语⾔)表达式中创建⼀个变量并将其绑定到上下⽂
Mybatis中使⽤Mysql的模糊查询字符串拼接(like)中也涉及到bind的使⽤
<select id="findName" resultType="String">
SELECT stu.name FROM tab_stu stu
湖南工业大学学报<where>
<if test="name!= null">
<bind name="stuName" value="'%'+stuName+'%'">
name like #{stuName}
pitstop
</if>哦!冬夜的灯光
</where>
</select>
到此这篇关于MyBatis的9种动态标签详解的⽂章就介绍到这了,更多相关MyBatis动态标签内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

本文发布于:2024-09-21 21:58:52,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/692935.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:标签   语句   动态
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议