亲宝软件园·资讯

展开

mybatis动态SQL if的test写法

阿狸远翔 人气:0

mybatis动态SQL if的test写法

使用动态SQL最常见情景

是根据条件包含 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

mybatis if test动态sql语句

<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>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论