Ibatisnet: Let the insert operation return the primary key value of the new record

Source: Internet
Author: User

Project Reference Ibatis Package:

IBatisNet.Common.dll--File version 1.6.2.0
IBatisNet.DataAccess.dll
IBatisNet.DataMapper.dll

Project directory Structure:

It has been a long time since the project used Ibatis to do the data access layer. The development team member reflected that the result of the insert operation returned by Ibatis is null, which is very uncomfortable. In fact, we all want to be able to return the primary key value of the new record. Last week, we have the anti-compilation Ibatis package, see its implementation principle, and later, try other methods, have not been able to implement this function.

These two days, decided to find some time to take care of this headache.

Combined with the online search, it concludes that the scheme is to return the primary key value of the record generated by the Insert command through the Selectkey child element of the INSERT statement.

The following is an Insert node in the XML mapping file:

<InsertID= "Insertentity"Parameterclass= "T_ent_project">INSERT INTO T_ent_project (Entid,projectno,projectname,budgetamount,lockedamount,usedamount,availableamount, Active,createdby,createdtime,modifiedby,modifidtime) VALUES (#EntId #, #ProjectNo #, #ProjectName #, #BudgetAmount #, #  lockedamount#, #UsedAmount #, #AvailableAmount #, #Active #, #CreatedBy #, #CreatedTime #, #ModifiedBy #, #ModifidTime #) <!--returns the primary key value of the record generated by the Insert command through the Selectkey child element of the INSERT statement -  <SelectkeyResultClass= "int"type= "POST" Property= "ProjectID">SELECT @ @IDENTITY</Selectkey></Insert>

It should be noted that the above select @ @IDENTITY is relative to MSSQL, if the database is MySQL or Oracle, it will be different.

In addition, when the Selectkey is misconfigured, running the program will appear with an exception similar to the following:

    • Unknown Selectkey Type: '
    • There is no Set member named "in class ' T_ent_project '

The following is a test class of crud for Ibatis:

usingIbatisdemo.domains;usingIbatisnet.datamapper;usingIBatisNet.DataMapper.Configuration;usingMicrosoft.VisualStudio.TestTools.UnitTesting;usingSystem;namespaceibatisdemo{[TestClass] Public classibatiscrudtest {[TestMethod] Public voidTestcrud () {//initializing the Sqlmapper objectDomsqlmapbuilder Builder =NewDomsqlmapbuilder ();//bin\\            stringPath = System.AppDomain.CurrentDomain.BaseDirectory +/*"bin\\" +*/   "\\SqlMap.config"; Isqlmapper Mapper=Builder.            Configure (path); //Defining entity ObjectsT_ent_project model =NewT_ent_project () {ProjectName="Test MyBatis", Entid=0, Createdtime=DateTime.Now}; //Insert            varret = mapper. Insert ("mybatis_t_ent_project.insertentity", model); intNewId =Convert.ToInt32 (ret); Console.WriteLine ("The primary key value of the record after the insert:"+newId); //Select            varNewmodel = Mapper. Queryforobject<t_ent_project> ("Mybatis_t_ent_project.getbykey", newId);            Assert.AreEqual (NewId, Newmodel.projectid); //both Update and Delete returns the number of rows affected. Model. ProjectID =newId; Model. ProjectName+="Updated"; intUpdrowcount = Mapper. Update ("mybatis_t_ent_project.updateentity", model); Console.WriteLine ("update affects the number of rows:"+updrowcount); intDelret = Mapper. Delete ("mybatis_t_ent_project.deleteentity", model.            ProjectID); Console.WriteLine ("Delete affects the number of rows:"+Delret); }    }}

Test results returned:

Ibatisnet: Let the insert operation return the primary key value of the new record

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.