In the process of developing with MyBatis, when you encounter inserting a record into a database table, the data is inserted incorrectly, and if you determine that it is the cause of the primary key, it is similar to the following error message:
And your intention is the primary key automatically grow, then, you can refer to the following methods to try to solve:
First step: Set the primary key to self-grow in the configuration (database I'm using MySQL)
If you are using the MyBatis Auto-generated INSERT statement, in addition to setting the usegeneratedkeys= "true" given above, you also need to remove the red part as shown, which is a piece of code that queries the maximum value of the primary key.
If not, you should check your database table for the primary key setting and whether it is an auto-grow setting.
You can add auto_increment after the primary key in the SQL statement.
If you are using the tool "Navicat for MySQL", such as I want to set the primary key of Tb-device_basic table to autogrow, click "Design Table", then "Auto Grow" tick, then try again.
Table primary key self-growth mybatis Insert data error