if
基本语法
xml
<if test="条件">
SQL片段
</if>- 条件成立 → 拼接 SQL
- 条件不成立 → 不拼接
- 使用
and和or连接多个条件
xml
<select id="queryUser" resultType="User">
SELECT * FROM user
WHERE 1=1
<if test="name != null">
AND name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
</select>test
test中可以直接使用传入Mapper的参数
java
List<User> query(UserDTO dto);xml
<if test="name != null">- 判空
xml
<if test="name != null">- 判空 + 非空字符串
xml
<if test="name != null and name != ''">- 数字判断
xml
<if test="age != null and age > 0">- 布尔
xml
<if test="deleted == false">- 集合判断
xml
<if test="list != null and list.size() > 0">