Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在xml中的判断条件不生效的问题 #3401

Open
Woshizz opened this issue Jan 23, 2025 · 2 comments
Open

在xml中的判断条件不生效的问题 #3401

Woshizz opened this issue Jan 23, 2025 · 2 comments

Comments

@Woshizz
Copy link

Woshizz commented Jan 23, 2025

<if test="query.tenantId != null and query.tenantId != '' and query.tenantId != '0'">
                AND tenant_id = #{query.tenantId}
            </if>

当tenantId ="0"时判断条件为true 会加上这个查询条件。当吧判断条件写成

<if test="query.tenantId != null and query.tenantId != '' and query.tenantId != 0">
                AND tenant_id = #{query.tenantId}
            </if>

时判断条件为false 不会加上这个判断条件

@harawata
Copy link
Member

Hello @Woshizz ,

If the type of query.tenantId is String, you have to use double quotes. i.e.

<if test='name != null and name != "" and name != "0"'>

In OGNL, a character enclosed in single quotes (e.g. '0') is recognized as a char, so query.tenantId != '0' will always be true.


If this is not what you mean, please provide a test case or small demo project with assertions.

@lidu-collect
Copy link

简单来说'0'是Character类型,“0”是String,0是Integer。mybatis源码中会对比较的值转换成double再比较,Stirng类型的转换是Double.parseDouble(s),Character类型的是(double)(Character)value Character被转为double会被转为ascii码

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants