Ibatis adding information to the current Add object ID

Source: Internet
Author: User
Tags cdata

in Ibatis, the return value of insert () is the primary key of an object, in fact the primary key of this object is this: if the inserted keyproperty is set in the bean's XML file, insert () method returns the value of the primary key. For example, so we want to insert a sequence value into a field in the database when inserting (of course, the type of this field is number), we can do the following configuration in the XML file (the following is the premise that the DB Server is Oracle):<insert id="Insertuser" parameterclass="po." User "> <selectkey resultclass="int" keyproperty=" userId" > SELECT User_account_s.nextval as UserID from dual </selectKey> INSERT INTO User_account (userid, username, password, groupname)VALUES (#userId #, #userName #, #password #, #groupName #) </insert> at this point, the value of the UserID field is inserted into a table in the database even if the value is sequence. Note, however, that the red bold font that appears in the configuration must correspond to the attribute field in the Bean, which is the same as the attribute field name in the bean, or an exception is thrown. in the case of a SQL Server database, configure the following:<insert id="Insertuser" parameterclass="po." User "> INSERT INTO User_account (userid, username, password, groupname)VALUES (#userId #, #userName #, #password #, #groupName #) <selectkey resultclass="int" keyproperty="UserId" >< /c16> SELECT @ @IDENTITY as UserID </selectKey> </insert> in the case of a MySQL database, configure the following:

<insert id="Ms-sys-seq-insert">

<! [Cdata[INSERT into SYS_SEQ (name) VALUES (#name #)]]> <selectkey resultclass="Long" keyproperty="id"> <! [Cdata[select last_insert_id () as ID]]> </selectKey> </insert> The above is copied from other places, these days have been ibatis to insert the data of some problems plagued for a long time, now is finally to ibatis this aspect has mastered. Their ability is still too poor, learning progress is too slow. I'm using the IBATIS,ORACLE10 database. the mapping XML file is configured as:

<insert id="Addstudent" parameterclass="Student">
<selectkey ResultClass="int" keyproperty="id">
Select Seq_student. next val as value from dual
<!--here you need to explain the generation of different database primary keys, There are different ways to separate databases:-->
<!--mysql:select last_insert_id () as VALUE--
<!--mssql:select @ @IDENTITY as value-->
<!--oracle:select stockidsequence. Nextval as VALUE from DUAL-->
<!--It's also important to note that different database manufacturers generate primary keys differently, Some are pre-generated (pre-generate) primary keys, such as Oracle and PostgreSQL.
</SELECTKEY>
insert into Tbl_student (Id,name, Birth,score) VALUES (#id #, #name #, #birth #, #score #)
</INSERT>

Selectkey element Part in fact can be placed before the INSERT statement can also be placed behind, put in before the words with Nextval, put in after the words with Currval. If put in before with Currval will error prompt to first nextval operation and then Currval. since I created a new sequences when I was creating the data table, when I put Selectkey in front of the INSERT statement, I found that the primary key ID would increment two times each time the insert data operation was performed, and the increment by must be more than 1. So it's a bit of a wonder, isn't it useless to put it before, and why did everyone put it before? I used PowerDesigner to build the table and sequence when I remember to automatically create a new trigger, the sequence value will be automatically added 1 before each insert operation ... I can only put it behind and use currval, so that the primary key is automatically increased by 1 each time, and the primary key value of the inserted data record can be returned normally. My mapping file is finally configured as follows:

<insert id = "addstudent" parameterclass = "Student" >
insert into Tbl_ Student (Id,name,birth,score) VALUES (#id #, #name #, #birth #, #score #)
< Selectkey resultclass= "int" keyproperty= "id" >
select Seq_student.currval as value from dual
</ Selectkey>

</insert>

Transfer from http://bujingyun23.blog.163.com/blog/static/18131024320137240417135/

Ibatis adding information to the current Add object ID

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.