IBatisNet 之 自動產生主關鍵字

來源:互聯網
上載者:User
 很多系統支援自動產生主關鍵字。一些資料庫廠商預先產生(oracle),一些資料庫廠商之後產生(mssal mysql).。如果你在<insert>元素中使用<selectKey>節,你就能獲得一個預先產生的key.。下面的例子示範了這種方法:

<!—Oracle SEQUENCE Example -->
<insert id="insertProduct-ORACLE"        parameterClass="product">
      <selectKey resultClass="int"       Property="id" > SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL

        </selectKey> insert into PRODUCT (PRD_ID,PRD_DESCRIPTION) values

        (#id#,#description#) </insert>  

<!— Microsoft SQL Server IDENTITY Column Example -->

<insert id="insertProduct-MS-SQL"

        parameterClass="product"> insert into PRODUCT (PRD_DESCRIPTION)

        values (#description#) <selectKey resultClass="int"

        Property="id" > SELECT @@IDENTITY AS ID </selectKey>

        </insert>

上面是IbatisNet的iBATIS Data Mapper Developer Guide上的說明:下面來介紹一下具體的應用和注意的地方:
person.xml
?xml version="1.0" encoding="utf-8" ?>

<sqlMap
 namespace="Person"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="SqlMap.xsd">

 <!-- XML "behind" document for the People service class. -->

 <alias>
  <typeAlias alias="Person" type="IbatisTest.Domain.Person, IbatisTest.Domain" />
 </alias>
 
 <resultMaps>
  <resultMap id="SelectResult" class="Person">
   <result property="Id" column="PER_ID" />
   <result property="FirstName" column="PER_FIRST_NAME" />
   <result property="LastName" column="PER_LAST_NAME" />
   <result property="BirthDate" column="PER_BIRTH_DATE" />
   <result property="WeightInKilograms" column="PER_WEIGHT_KG" />
   <result property="HeightInMeters" column="PER_HEIGHT_M" />
  </resultMap>
 </resultMaps>
 
  <insert id="Insert" parameterClass="Person">
   insert into PERSON
    (PER_FIRST_NAME, PER_LAST_NAME,
    PER_BIRTH_DATE, PER_WEIGHT_KG, PER_HEIGHT_M)
   values
    ( #FirstName#, #LastName#,
    #BirthDate#, #WeightInKilograms#, #HeightInMeters#)
   <selectKey property="Id" type="post" resultClass="int"> 
   select CAST(@@IDENTITY as int) as value
   </selectKey> 
  </insert>    
   </statements>
 
</sqlMap> 
<selectKey>節返回一個從sql server產生的自動產生關鍵字。

    
             //新增一個員工


             private void Button1_Click(object sender, System.EventArgs e)

 
          {

 
              IbatisTest.Domain.Person person = new IbatisTest.Domain.Person();

 
              person.Id =-1;//這裡的賦值很重要,否則會出錯
              person.FirstName = "--New Person--";


                     person.BirthDate = Convert.ToDateTime("2000-4-2");

  
              PersonHelper personhelper = new PersonHelper();

 
                     personhelper.Insert(person);

 
         }

 

          //商務邏輯類方法
          public int Insert (Person person) 
          {
              try
              {

                   int result = -1;

                    result =(int)Mapper().Insert("Insert",person);

                   return result;

              }
             catch(Exception ex) 
              {

                   throw ex; 
              }

         }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.