IBatisNet 之 自動產生主關鍵字和Insert返回主鍵ID

來源:互聯網
上載者:User
文章目錄
  • Feedback
IBatisNet 之 自動產生主關鍵字和Insert返回主鍵ID
很多系統支援自動產生主關鍵字。一些資料庫廠商預先產生(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; 
              }

         }

自由、創新、研究、探索……
Url: http://shanyou.cnblogs.com
website: http://www.openbeta.cn

Feedback#1樓 
  回複  引用  查看    

2005-10-24 16:37 by 浪子      如果多主鍵不是如何處理?

對於多主鍵的配置問題:

此時主鍵由兩個欄位組成per_id 和 group_id,parameterClass應該如何寫?需要自己在cs裡面定義該表的主鍵類,然後映射回去嗎?還是有更好的辦法?期待能看到你的解決辦法.謝謝

<select id="Select" parameterClass="int" resultMap="SelectResult">

select

PER_ID,

PER_FIRST_NAME,

PER_LAST_NAME,

PER_BIRTH_DATE,

PER_WEIGHT_KG,

PER_HEIGHT_M

from PERSON

<dynamic prepend="WHERE">

<isParameterPresent>

//如何配置?

PER_ID = #value# and group_ID=??

</isParameterPresent>

</dynamic>

</select>

http://www.cnblogs.com/shanyou/Messages/2005/10/24/260907.html

#2樓 
  回複  引用  查看    

2005-10-24 16:37 by 浪子      如果多主鍵不是如何處理?

對於多主鍵的配置問題:

此時主鍵由兩個欄位組成per_id 和 group_id,parameterClass應該如何寫?需要自己在cs裡面定義該表的主鍵類,然後映射回去嗎?還是有更好的辦法?期待能看到你的解決辦法.謝謝

<select id="Select" parameterClass="int" resultMap="SelectResult">

select

PER_ID,

PER_FIRST_NAME,

PER_LAST_NAME,

PER_BIRTH_DATE,

PER_WEIGHT_KG,

PER_HEIGHT_M

from PERSON

<dynamic prepend="WHERE">

<isParameterPresent>

//如何配置?

PER_ID = #value# and group_ID=??

</isParameterPresent>

</dynamic>

</select>

http://www.cnblogs.com/shanyou/Messages/2005/10/24/260907.html

#3樓 [樓主]
  回複  引用  查看    

2005-10-24 19:02 by 自由、創新、研究、探索……      如果是多主鍵,這兩個多主鍵一般是另外的幾張表的外鍵。這種情況你不需要去自動產生主鍵阿,處理在一個事務中就可以保證資料的完整性了吧。

我一般是將多主鍵可以轉化成單主鍵

可以這樣配置

對於多主鍵的配置問題:

此時主鍵由兩個欄位組成per_id 和 group_id,parameterClass應該如何寫?<select id="Select" parameterClass="Hashtable" resultMap="SelectResult">

select

PER_ID,

PER_FIRST_NAME,

PER_LAST_NAME,

PER_BIRTH_DATE,

PER_WEIGHT_KG,

PER_HEIGHT_M

from PERSON

<dynamic prepend="WHERE">

<isNotNull prepend="AND" property="PerID">PER_ID = #PerID#</isNotNull>

<isNotNull prepend="AND" property="GroupId">group_ID= #GroupId#</isNotNull>

</dynamic>

</select>

#4樓 
  回複  引用  查看    

2005-10-25 15:24 by 浪子      處理成Hashtable?

該Hashtable包含PerID和GroupID對值?

Hastable ht = new Hastable();

ht.Add('PerID','001');

ht.Add('GroupID','001');

public Person Select (Hastable ht)

{

return (Person) Mapper().QueryForObject ("Select", ht);

}

這樣子處理嗎?

<dynamic prepend="WHERE">

<isNotNull prepend="AND" property="PerID">PER_ID = #PerID#</isNotNull>

<isNotNull prepend="AND" property="GroupId">group_ID= #GroupId#</isNotNull>

</dynamic>

謝謝你的回複,並且共用你的想法.

#5樓 [樓主]
  回複  引用  查看    

2005-10-25 17:56 by 自由、創新、研究、探索……      對,處理成Hashtable,該Hashtable包含PerID和GroupID對值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.