When the Integer parameter is 0, mybatis does not take effect

When mybatis receives an Integer parameter, if the parameter value is 0, it will be parsed as an empty string ' '.

usual writing

<if test="status != null and status != ''">
		and ppi.status =#{status}
</if>

Preliminary - Solution:

  1. Only judge that the Integer parameter is not null, do not judge that the parameter is not an empty string;

<if test="status != null">
    and ppi.status =#{status}
</if>

2. It can be judged that the parameter is not equal to 0

<if test="status != null and status != '' or status == 0">
  and ppi.status =#{status}
</if>

The above two methods will have a problem, because the int type defaults to 0; when the front-end query condition is empty, when accessing the background interface, the queried data is still the data filtered by 0,

The problem is in my status design:

My status is defined like this: 0-not started, 1-in progress, 2-delayed, 3-completed

Solution:

1. Change the definition of status to 1-not started, 2-in progress, 3-delayed, 4-completed (if the change is too large, it will be useless)

After the change, the sql writing method is the same as usual

2. Change the default value of the int type status to 999.

<if test="status != null and status != 999">
    and ppi.status =#{status}
</if>

Guess you like

Origin blog.csdn.net/weixin_44220970/article/details/129381444