IBatisNet 之 自动生成主关键字

博客介绍了IbatisNet的iBATIS Data Mapper的具体应用及注意点,给出了person.xml的示例代码,包括别名定义、结果映射、插入语句等,还提到了<selectKey>节返回自动生成关键字,最后给出了部分插入操作的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  很多系统支持自动生成主关键字。一些数据库厂商预先生成 (oracle) ,一些数据库厂商之后生成 (mssal mysql). 。如果你在 <insert> 元素中使用 <selectKey> 节,你就能获得一个预先生成的 key. 。下面的例子演示了这种方法: <?xml:namespace prefix = o />

<!—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; 
              }

         }

转载于:https://www.cnblogs.com/shanyou/archive/2005/10/13/254255.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值