什么是动态SQL?
根据不同条件拼接SQL语句,实现对数据库更准确的操作。
常用的动态SQL元素:
If元素:判断语句,单条件分支判断
choose元素(when,otherwise):多条件分支判断
trim元素(where,set):辅助元素,用于处理一些SQL拼接的问题
1.if元素:
语法: <if test=""> SQL语句 </if>
eg:
<if test="classid != 0">
and classid =#{classid}
</if>
2. choose元素(when,otherwise)
语法: <choose> <when test=“条件”>满足条件的语句</ when>
<otherwise> 满足其他条件的语句 <otherwise> </choose>
可以有多个when标签,只有一个otherwise标签
<choose>
<when test="sname != null">
and sname =#{sname}
</when>
<when test="ssex != null"> and ssex=#{ssex}</when>
<when test="classid != 0">and classid=#{classid}</when>
<otherwise> and sid = 12</otherwise>
</choose>
3. trim元素(where,set)
(1)where标签
语法: <where> <if test =”条件”> 满足条件的语句 </if> </where>
注意:
1.当无条件时 ,where关键字不会出现
eg:
<where>
<if test="ssex != null"> and ssex = #{ssex}</if>
<if test="classid != 0"> and classid = #{classid}</if>
</where>
(2).set元素:修改辅助
语法: <set> <if test =”条件”> 满足条件的语句 </if> </set>
eg:
写一个根据sid,在student表里添加信息的例子
<update id="updatestudent" parameterType="student">
update student
<set>
<if test="sname != null"> sname=#{sname},</if>
<if test="ssex != null"> ssex=#{ssex},</if>
<if test="classid != null"> classid=#{classid},</if>
<if test="birthday != null"> birthday=#{birthday},</if>
</set>
<where> sid = #{sid}</where>
</update>
注意:
1.添加一个set关键词
2.将要修改的字段=值,最后一个逗号去掉
(3).trim元素万能标签(常用于新增)
四个属性: prefix 开头添加一个
prefixOverrides 开头去掉一个
suffix 结尾添加一个
suffixOverrides 结尾去掉一个
eg:
<update id="updatestudent" parameterType="student">
update student
<trim prefix="set" prefixOverrides="," >
<if test="sname != null"> sname=#{sname},</if>
<if test="ssex != null"> ssex=#{ssex},</if>
<if test="classid != null"> classid=#{classid},</if>
<if test="birthday != null"> birthday=#{birthday},</if>
<where>
sid=#{sid}
</where>
</trim>
</update>
4.foreach 元素
语法:<foreach item = “”index=“” collection=“” open=“” separator=“” close=“”> </foreach>
item: 只循环中的当前元素index: 当前循环元素的我位置下标
collection:方法传递的参数,一个数组或集合 数组:array 集合:list
open:以什么符号开始,将元素包装起来
separator:以什么符号结束,将元素包装起来
close: 各个元素的间隔符号
eg:
<select id="findshu" resultType="student">
select * from student where sid
<foreach collection="array" item="x" open="in(" close=")" separator=",">
#{x}
</foreach>
</select>
<select id="findji" resultType="student">
select * from student where sid
<foreach collection="list" item="x" open="in(" close=")" separator=",">
#{x}
</foreach>
</select>
5.bind元素:模糊查询
语法: <bind name=“” value=“ _parameter”> </bind>
name: 自定义变量名
value: 自定义变量的变量值
_parameter: 传递进来的参数
模糊查询:五种方法
<select id="muhu" resultType="student" parameterType="String">
<!-- 方法一 -->
<bind name="sname" value="_parameter+'%'"/>
select * from student where sname like #{sname}
<!-- 方法二 -->
<!-- select * form student where sname like #{v} -->
<!-- 方法三 -->
<!-- select * from student where sname like concat(#{},'%')-->
<!-- 方法四 ${} 字符串替换 不能方式SQL注入 #{} 可以防止SQL注入 -->
<!-- select * from student where sname like'$(V)%' -->
<!-- 方法五 -->
<!-- select * from student where sname like "%"#{v}"%" -->
</select>
这里涉及一到面试题:
#{ } 和 ${ } 的区别:
${ } 字符串替换 不能方式SQL注入
#{ } 可以防止SQL注入,在mybatis中sql语句中被预处理使用占位