Mybatis returns the configuration of the primary key ID during the insert operation (recommended)

Many times, when inserting data into the database, you need to keep the ID of the inserted data for subsequent update operations or store the ID in other tables as a foreign key.

However, by default, the insert operation returns an int value, which does not represent the primary key ID, but the number of rows affected by the current SQL statement...

Next, let's take a look at how mybatis binds the returned ID to the object when performing insert operations using MySQL and Oracle.

MySQL usage:

In the above configuration, "keyproperty" indicates that the returned ID is to be saved in the property of the object, "usegeneratedkeys" indicates that the primary key ID is in self growth mode.

The above configuration in MySQL is OK. It is relatively simple and will not be repeated.

Oracle usage:

In Oracle usage, it should be noted that since Oracle does not have the saying of self growth, only the form of imitating self growth is sequence, so the "usegeneratedkeys" attribute can no longer be used.

Instead, use < selectkey > to obtain and assign the ID to the attribute of the object. The ID is inserted normally during the insert operation.

summary

The above is what Xiaobian introduced to you. Mybatis returns the configuration of the primary key ID during the insert operation. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>