When Mybatis dynamic parameter value is 0, the condition is invalid

Reproduce the problem 

Retrospective reason

Solutions

Solution 1: Do not pass the value

Solution 2: Condition judgment to remove empty channel judgment

Problem expansion


 

Reproduce the problem 

Let's look at our example first

I want to find out the data whose status is 0

Our data is

Dynamic sql is

unit test

Retrospective reason

This is because mybatis for numeric parameters, if it is 0, it will be treated as ”, which is an empty string, so the dynamic sql judgment condition <if test="status != null and status !=''"> does not hold, so sql does not splice the status filter conditions

Solutions

Now that the problem is known, it is easy to solve it. For the problem with a value of 0, treat it as an empty string", we can have 2 solutions

  • No value
  • Conditional judgment removes empty judgment

Solution 1: Do not pass the value

Solution 2: Condition judgment to remove empty channel judgment

Problem expansion

When our problem was shown above, the value passed was 0, the default is int type, in fact, other numeric types have this problem, as long as the value is 0, there is this problem, such as long integer 0L, single precision floating point number 0.0f, double-precision floating-point number 0.0d are the same, and their packaging classes are the same

The following only demonstrates double type 0 value

 

 

 

 

 

Guess you like

Origin blog.csdn.net/lgl782519197/article/details/109275996