Have a problem today
When I use the BigDecimal object in java, the value is 0.00000; the object is not empty, it is new, I need to insert the value into the database, and the database receives field type for the value is decimal.
Mybatis I use. The statements in mybatis are 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 an object of type BigDecimal in a java object. The field mapped to the database table is total_consumption. However, the statements in your mybatis xml file are as abbreviated above. You need to set the value to be 0.00000. When running, you will find an error, and the error is as follows:
An error indicates that the SQL statement is like this, and there is no value of set required
UPDATE user_consumption WHERE id = #{id} and state = 'A'
From the statement that reports an error, it can be seen that Mybatis believes that the totalConsumption quantity is null or an empty string, but totalConsumption is clearly New and will not be empty. Then the only possibility is that mybatis recognizes totalConsumption with a value of 0.00000 as an empty string. This is true. I will look at the source code another day and post it here. Remember that mybatis will recognize the value 0 as an empty string.
Solution:
The modification to the above problem is to remove the judgment statement that determines the empty string. But this is not a general solution, because the BigDecimal in my question needs to be produced by New. Even if it is not produced by 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 do not require new, such as Integer in Java, it is best to change the type to string, so this will not happen.
mybatis gets the data as null (annotation method)
When using muybatis to obtain the date data of mysql, one of the data is always empty, while the other data in the same entity can be retrieved normally.
This situation is likely to be the reason why the data in mysql and java entity class data fails to map. When developing with annotations, the attributes in the entity class data must be consistent with the attribute names in mysql (unless you perform the mapping conversion configuration), and the upper and lower case can be ignored.
The problem this time is that one of the attributes has less letters, which makes it impossible to match mysql.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.