mybatis solves the problem of recognizing the value 0 as an empty string

  • 2021-09-16 07:08:03
  • OfStack

I encountered a problem today

When I use the BigDecimal object of java, the value is 0.00000; The object is not empty, it comes from new, and I need to insert the value into the database, where the receiving field type for the value is decimal.

I use Mybatis. The statement in mybatis is roughly as follows:


       UPDATE user_consumption 
  <trim prefix="SET" suffixOverrides=",">
            <if test="totalConsumption!=null" and totalConsumption!=''>
            total_consumption = #{totalConsumption},
            </if>
  </trim>  
        WHERE 
         id = #{id} and state = 'A'

Where totalConsumption is the object of type BigDecimal in the java object, the variable maps to the field in the database table as total_consumption. However, the statement in your mybatis xml file is abbreviated above, and the value you need to set is 0.00000. When running at this time, you will find an error, which is as follows:

An error indicates that the sql statement looks like this, and no value for set is required


UPDATE user_consumption   WHERE id = #{id} and state = 'A'

It can be seen from the error statement that Mybatis thinks that totalConsumption is null or an empty string, but totalConsumption is obviously from New and will not be empty, so the only possibility of 1 is that mybatis recognizes totalConsumption with a value of 0.00000 as an empty string, which is true. I will look at the source code another day and post it here. Remember that mybatis will recognize the value of 0 as an empty string in the future.

Solution:

To modify the above problems, the judgment statement for judging empty strings can be removed. But this is not a general solution, because the BigDecimal in my problem needs New, and even if it is not New, it will not become an empty string before entering mybatis, so I can directly remove the empty string.


        UPDATE user_consumption 
  <trim prefix="SET" suffixOverrides=",">
            <if test="totalConsumption!=null">
            total_consumption = #{totalConsumption},
            </if>
  </trim>  
        WHERE 
         id = #{id} and state = 'A'

But for those data types that don't require new, such as Integer in your Java, you'd better change the type to string, and this won't happen.

mybatis Obtain data as null (annotation mode)

When using muybatis to obtain the date data of mysql, one data 1 is directly empty, while other data in the same entity can be obtained normally.

This situation is probably the reason why the data in mysql failed to map with the entity class data in java. When using annotation development, it is required that the attribute in the entity class data must be the same as the attribute name in mysql (unless you configure the mapping transformation), and the case can be ignored.

The problem this time is that one of the attributes is written with one letter less, which leads to the mismatch with mysql


Related articles: