时间:2022-07-16 09:40:42 | 栏目:JAVA代码 | 点击:次
是根据条件包含 where 子句的一部分。
比如:
<select id="findActiveBlogWithTitleLike"
resultType="Blog">
SELECT * FROM BLOG
WHERE state = ‘ACTIVE'
<if test="title != null">
AND title like #{title}
</if>
</select>
其中 test 的表达式是基于OGNL 的表达式,语法规则也是OGNL的语法规则。
OGNL官方表达式手册:https://commons.apache.org/proper/commons-ognl/language-guide.html

上图是官方指导的一部分,主要说明了,在test中无法使用<= 等符号可以使用 lte 代替。
| 运算符 | 代替字符 |
|---|---|
| < | lt |
| <= | lte |
| > | gt |
| >= | gte |
<select id="getStudentId" parameterType="java.lang.String" resultType="java.lang.String">
SELECT MAX(Student_ID) FROM Student
<where>
<if test="classid !=null and classid !=''">
AND CLASS_ID = {student.classID}
</if>
<if test="age ==null or age ==''">
AND AGE = {student.age}
</if>
</where>
</select>