什么是动态SQL?
根据不同条件拼接 SQL 语句,实现对数据库更准确的操作;
映射器配置文件或者注解。
常用的动态SQL标签有哪些?
If
语法
< if test =”条件”> 满足条件的语句 </ if>
Choose
语法
<choose>
<when test=“条件”>满足条件的语句</ when>
<otherwise> 满足其他条件的语句 <otherwise>
</choose>
Where
1. 没有任何条件的时候 where标签整体不出现 也不会添加where关键词
2. 将遇到的第一个 and 去掉,没有其他表达式的时候
3. 当有条件的时候 会添加一个 Where 关键词
Set
1. 添加一个set 关键词
2. 将条件中的最后一个Set去掉
trim 万能标签
能代替 where标签 set 标签
prefix 开头添加一个
prefixOverrides 开头去掉一个
suffix 结尾添加一个
suffixOverrides 结尾去掉一个
foreach 标签 循环
collection 集合用list 数组用array
item 每个元素的存放起一个变量名
open 开始添加一个
close 结束添加一个
separator 每个元素的分隔符
模糊查询的五种方式
<!-- select * from student where sname like "#{v}%" -->
<!-- 方式一 张% -->
<!-- select * from student where sname like #{v} -->
<!-- 方式二 -->
<!-- select * from student where sname like concat(#{v},'%') -->
<!-- 方式三
${} 字符串替换 不能方式sql注入
#{} 可以防止sql注入
-->
<!-- select * from student where sname like '${v}%' -->
<!-- 方式四 -->
<!-- select * from student where sname like "%"#{v}"%" -->
<!-- 方式五 bind 标签 -->
<bind name="aa" value="_parameter+'%'"/>
select * from student where sname like #{aa}