Mybatis dynamic sql if condition judgment

Mybatis dynamic sql if condition judgment

1. If the parameter is a numeric type

<if test='id != null and id > 28'></if>

gt corresponds to >
gte corresponds to >=
lt corresponds to < (the associated "test" attribute value cannot contain the '<' character)
lte corresponds to <= (the associated "test" attribute value cannot contain the '<' character)
2. If it is a string type
, if you need to filter empty strings, you can add empty string judgments to not support &&, so use and or || here to make logical and or judgments

<if test="username != null and '' != username"></if> 
或者 
<if test="username != null and '' neq username"></if>

If it is judged whether the string has a special character at the beginning, end, etc. Just call the corresponding method of String directly

<if test="username != null and username.indexOf('ji') == 0"> </if> <!-- 是否以什么开头 -->
<if test="username != null and username.indexOf('ji') >= 0"> </if> <!-- 是否包含某字符 -->
<if test="username != null and username.lastIndexOf('ji') > 0"></if>  <!-- 是否以什么结尾 -->    

Is it a specific string

<if test="username != null and 'hello' == username"></if> 或者<if test="username != null and 'hello' eq username"></if>

Note: There is no problem with this form of writing when the parameter type is a string,
but when the parameter type is a non-string type, it needs to be written as:

<if test="username != null and 'hello'.toString() == username.toString()"></if>

eq corresponds to ==
neq corresponds to !=
3 to judge whether the list is empty
. If the conditional judgment can directly call the method of the object itself for logical judgment, so the list is judged to be empty. You can call .size()>0 or .isEmpty()

 <if test="userList != null and userList.isEmpty()"></if> ,
 <if test="userList != null and userList.size()>0"></if>

4 If the map parameter is the same as the value, map.key (the name of the key in the map) can be used. You can
also write a method by yourself, use it in if, and define a class by yourself, and a method in it is used for conditional judgment.

<if test="dynamicSql1Model.getMySelfMethod()">
dynamicSql1Model为自定义的类,getMySelfMethod()是自定义类里面的自定义方法

Original link: https://blog.csdn.net/xu1916659422/article/details/78104976
Thanks!

Supongo que te gusta

Origin blog.csdn.net/weixin_44006731/article/details/104038226
Recomendado
Clasificación