动态SQL
- if
- choose (when otherwise)
- where
- set
- trim
- foreach
if
我们直接使用if,的时候,有可能会有多余and or等符号的错误,因为不符合SQL语法。
解决方式以前的可以使用 添加额外的固定条件 1=1
where
使用mybatis提供的where标签来解决
- 可以去除多余的前缀的and or
- 如果一个条件没有,它会自动去除where本身
select *
from t_student
<where>
<if test="name != null">
<bind name="newName" value="'%'+name+'%'" />
and name like #{newName}
</if>
<if test="age != null">
and age = #{age}
</if>
</where>
set
修改值
trim
经常用来代替where set