元素 | 作用 | 备注 |
---|---|---|
if | 判断语句 | 单条件分支判断 |
choose、when、otherwise | Java中的case..when.. | 多条件分支判断 |
trim、where、set | 辅助元素 | 用于处理 SQL 拼装问题 |
foreach | 循环语句 | 在in语句等列举条件常用,常用于实现批量操作 |
if
我们可以使用 <if test="...">
进行条件判断,但是按照下面这样的写法就有一个问题,如果两个入参都为空,那么 where 后面就没有语句,不符合 SQL 的规范。
|
where
当遇到多个查询条件,使用where 1=1 可以很方便的解决我们的问题,但是这样很可能会造成非常大的性能损失。
因为添加了 “where 1=1 ”的过滤条件之后,数据库系统就无法使用索引等查询优化策略,数据库系统将会被迫对每行数据进行扫描(即全表扫描) 以比较此行是否满足过滤条件,当表中的数据量较大时查询速度会非常慢;此外,还会存在SQL 注入的风险。
|
where 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入“WHERE”子句。而且,若语句的开头为“AND”或“OR”,where 元素也会将它们去除。<where>
其实就是 trim
的一种实现。
set
语句集中有值,那就加个 set,同时去掉 set 语句中最后的逗号。
|
trim
|