The selectKey of ibatis is used to obtain the id after data is inserted.

Source: Internet
Author: User

The selectKey of ibatis is used to insert data and obtain the id. Some sequence data is used when ibatis is used to insert data into the database. In some cases, after the insertion is complete, you must return the sequence value before proceeding to the next step. Use the selectKey of ibatis to obtain the sequence value and return the value. However, there are different operations for different databases. For oracle: <insert id = "insertUser" parameterClass = "ibatis. user "> <selectKey resultClass =" long "keyProperty =" id "> select SEQ_USER_ID.nextval as id from dual </selectKey> insert into user (id, name, password) values (# id #, # name #, # password #) </insert> after the statement is executed, the id field in DO of the passed parameter User object will be assigned the sequence value. For mysql <insert id = "insertUser" parameterClass = "ibatis. user "> insert into user (name, password) values (# name #, # password #) <selectKey resultClass =" long "keyProperty =" id "> SELECT LAST_INSERT_ID () as id </selectKey> </insert> put selectKey after insert, and use LAST_INSERT_ID () to obtain the value of the newly inserted auto-increment id.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.