动态SQL的定义:
根据不同条件拼接 SQL 语句,实现对数据库更准确的操作。
常用的动态SQL标签:
if 标签 :判断语句,单语句分支判断。
choose 标签(包含when,otherwise标签):多条件分支判断,等同于Java中的switch。
trim 标签(可代替where和set标签):辅助标签,用于处理一些sql拼接问题。
foreach 标签 :循环标签,在in语句等列举条件常用。
bind 标签:自定义上下文变量,传递参数。
(以下在xml文件中的展示,在注解方式中写 <script></script> 标签把动态sql语句包起来,其余的使用动态sql标签跟xml中一样)
if标签
语法:
<if text="条件">满足条件的语句</if>
<!-- if判断 输入条件-->
<select id="selectOnAll" parameterType="Student" resultType="Student">
<!-- sql语句语法格式-->
select *from student where 1=1
<if test="ssex != null">
and ssex=#{ssex}
</if>
<if test="classid != 0">
and classid=#{classid}
</if>
choose 标签
语法:
<choose>
<when test=“条件”>满足条件的语句</ when>
<otherwise> 满足其他条件的语句 <otherwise>
</choose>
<select id="selectAllchoose" parameterType="Student" resultType="Student">
select *from student where 1=1
<choose>
<!-- when相当于case-->
<when test="sname != null"> and sname =#{sname}</when>
<when test="ssex != null"> and ssex =#{ssex}</when>
<when test="classid != 0"> and classid =#{classid}</when>
<!-- 相当于default -->
<otherwise>and sid=20</otherwise>
</choose>
></select>
trim 标签
where 标签:
语法:
<where>
<if test =”条件”> 满足条件的语句 </if>
</where>
set 标签:
set 标签元素主要是用在更新操作的时候, 它的主要功能和 where 标签元素其实是差不 多的,主要是在包含的语句前输出一个 set, 然后如果包含的语句是以逗号结束的话将会 把该逗号忽略,如果 set 包含的内容为空的 话则会出错。有了 set 元素就可以动态的更 新那些修改了的字段。
语法:
<set>
<if test =”条件”> 满足条件的语句 </if>
</set>
trim的语法:
<trim prefix = “”suffixOverrides = “” prefixOverrides=“”suffix=“”></trim>
prefix 以什么开头
prefixOverrides 开头去掉什么
suffix 以什么结尾
sufixOverrides 结尾去掉什么
<!-- trim 万能标签 -->
<select id="selectAllTrim" parameterType="Student" resultType="Student">
select *from student
<!-- prefix开头 prefixOverrides开头去掉 suffix结尾 sufixOverrides结尾去掉 -->
<trim prefix="where" prefixOverrides="and">
<if test="ssex != null">
and ssex=#{ssex}
</if>
<if test="classid !=0">
and classid=#{classid}
</if>
</trim>
</select>
<insert id="addTrim" parameterType="Student">
insert into student
<trim prefix="(" suffixOverrides="," suffix=")">
<if test="sname !=null">
sname,
</if>
<if test="ssex !=null">
ssex,
</if>
<if test="classid !=null">
classid,
</if>
<if test="birthday !=null">
birthday,
</if>
<if test="sid !=null">
sid,
</if>
</trim>
values
<trim prefix="(" suffixOverrides="," suffix=")">
<if test="sname !=null">
#{sname},
</if>
<if test="ssex !=null">
#{ssex},
</if>
<if test="classid !=null">
#{classid},
</if>
<if test="birthday !=null">
#{birthday},
</if>
<if test="sid !=null">
#{sid},
</if>
</trim>
</insert>
foreach 标签
语法:
<foreach item = “”index=“” collection=“” open=“” separator=“” close=“”></foreach>
item 循环中的当前元素;
index 当前循环元素的位置下标;
collection 方法传递的参数,一个数组或者集合;
open 以什么符号开始将这些集合元素包装起来;
close 以什么符号结束将这些集合元素包装起来;
separator 各个元素的间隔符号。
<!-- foreach中的传入数据为一个数字组 -->
<select id="selectForeachArray" resultType="Student">
select *from student where sid in
<foreach collection="array" item="x" open="(" close=")" separator=",">
#{x}
</foreach>
</select>
<!-- foreach中传入一个集合 -->
<select id="selectForeachList" resultType="Student">
select *from student where sid in
<foreach collection="List" item="x" open="(" close=")" separator=",">
#{x}
</foreach>
</select>
bind 标签
语法:
<bind name=“”value=“_parameter”></bind>
name 自定义变量的变量名
value 自定义变量的变量值
_parameter 传递进来的参数
<select id="" parameterType="String" resultType="Student">
<bind name="aa" value="_parameter+'%'"/>
select * from student where sname like #{aa}/>
</select>