Characters in IF statement

1,521 views
Skip to first unread message

Romain Van der Keilen

unread,
Aug 26, 2010, 3:51:54 AM8/26/10
to mybatis-user
Hi there,
I've a little question for you. Is it possible to insert characters
comparaison in an IF statement ? I think an example will speak from
itself :

<if test="irregularity != null">
<if test="irregularity == 'I'">
AND TRG_CLOSED IS NOT NULL AND TRG_CLOSED != 1 AND TRG_BLOCKING != 1
</if>
<if test="irregularity == 'N'">
AND ( TRG_CLOSED IS NULL OR TRG_CLOSED = 1 )
</if>
<if test="irregularity == 'B'">
AND TRG_CLOSED IS NOT NULL AND TRG_CLOSED != 1 AND TRG_BLOCKING = 1
</if>
</if>

When runnin this, I get an NumberFormatException saying that my letter
(I,N or B) cannot be parsed into a Double value ... Is there something
I forgot or do wrong ?
Thanks!
Romain.

Poitras Christian

unread,
Aug 26, 2010, 8:21:29 AM8/26/10
to mybati...@googlegroups.com
I use that frequently.
<if test="restrictions.support.toString() == 'SOLUTION'">

Is it possible that irregularity is a double? If so, ONGL may try to cast 'I' into a double.

Christian

Nathan Maves

unread,
Aug 26, 2010, 10:41:30 AM8/26/10
to mybati...@googlegroups.com
not sure if it matter but you might try to use the eq operator over the ==

Romain Van der Keilen

unread,
Aug 27, 2010, 2:32:03 AM8/27/10
to mybatis-user
I've tried both == and eq notation and both two gives me the
java.lang.NumberFormatException ...
Irregularity is a String ...

Romain Van der Keilen

unread,
Aug 27, 2010, 2:59:28 AM8/27/10
to mybatis-user
ok, I found something that's working :
<if test="irregularity != null">
<if test="irregularity.toString().equalsIgnoreCase('I')">
AND TRG_CLOSED IS NOT NULL AND TRG_CLOSED != 1 AND TRG_BLOCKING != 1
</if>
<if test="irregularity.toString().equalsIgnoreCase('N')">
AND ( TRG_CLOSED IS NULL OR TRG_CLOSED = 1 )
</if>
<if test="irregularity.toString().equalsIgnoreCase('B')">
AND TRG_CLOSED IS NOT NULL AND TRG_CLOSED != 1 AND TRG_BLOCKING = 1
</if>
</if>

I didn't knew that I could insert java code in the test case, so I
naturally wrote something that cannot be parsed as a Double, and ...
that rocks!

Thanks for your help ;)

Romain.

Patrick Bond

unread,
May 2, 2017, 4:13:47 PM5/2/17
to mybatis-user
I just ran into this issue myself, I know this was posted 10 years ago but it still apparently is an issue so I hope this helps anyone else that comes across this thread. 

You can't use == because its not comparing strings correctly, so your immediate thought should be to use the following (note this will may still NOT work)

<if test="irregularity.equals('B')">

</if>

The above will only work if the variable "irregularity" was defined as a char. If it is defined as a string, this will still not work. This is caused by the Single quotes around the 'B', which in the xml will read as a character and the conditional will then be comparing a string to a character and will not yield a correct result (although it doesn't throw an error).

Instead, you need to flip the quotes around, single quotes on the outside and double quotes on the inside. This will compare a string and string.

<if test = 'irregularity.equals("B")'>

</if>

Now as long as irregularity is a string you should yield a correct result.
Reply all
Reply to author
Forward
0 new messages