Integer and int stepped pit

I found an interesting bug in doing SSM project, here to record it.

Database table is as follows:

Entity classes: grade field is initially set to an int

At the time of the third data to be modified with mybatis, want to change the assignment, unassigned does not change, test run

 

After modifying grade field found in the database becomes 0, this is not what I want, (it should be in accordance with the normal requirements of the original number 5)

 

I checked the log and found that the transmission of the disease:

 When int type, not assigned by default to 0, to execute the update statement mybatis, is treated as the number 0 instead of null,

Think about the difference between Integer and int, the default value of the integer null, that is, Integer can distinguish the difference between an assignment and not a value of 0, int can not express without assignment,

I again before class grade entity type to the Integer successfully achieved what you want.

Summary: Try to use the modified entity class Integer, Integer and can distinguish the difference between a value of 0 unassigned, most of our judgments are to do "" null "" to judge, especially in this to be a note.

Guess you like

Origin www.cnblogs.com/ly-520/p/11241080.html