Stepping on the date type conversion of Mybatis

Scenes

First insert data into the database, which contains a date field insertDate, and then return the date field. Then, after the business logic is processed, and then based on the date, according to the database records, there will be a certain probability that the update will fail at this time. After investigation, it is found that the jdbcType in the xml automatically generated by mybatis-generator is timestamp, but the database field type is actually datetime. Then check the SQL log for printing the inserted data and find that the date has milliseconds.
SQL log snippet on insert
After mybatis inserts and executes, when it is converted to the datetime type of the database, the subsequent milliseconds are rounded, causing the insertion time to be 1s later than the actual time.

solution

1. When inserting, set the number of milliseconds to zero, which is more recommended, without modifying the database design
insert image description here
2. Adjust the database field to timestamp

Guess you like

Origin blog.csdn.net/baidu_38956956/article/details/115380511