Ibatis自动生成主键

Batis的sqlMap配置文件的selectKey元素有个type属性,可以指定pre或者post表示前生成(pre) 还是后生成(post)。
1.Oracle
(1)往oralce里建一个sequence:
  
                     create sequence seq_id_petition
                      minvalue 1
                      start with 1
                      increment by 1
         
   
  

  (2)ibatis配置文件:

  
   <insert id="insertPetitionLetter" parameterMap="PetitionLetterParam">
  	  <selectKey resultClass="int" keyProperty="id" type="pre">      
           <![CDATA[SELECT SEQ_ID_PETITION.NEXTVAL AS ID FROM DUAL]]>      
      </selectKey> 
    <![CDATA[  
  	insert into t_petitionletter(id, identifier, reporter, letterInformation, 
  	                             supervision, processFlow) values
  	                             (?,?,?,?,?,?)
   ]]>  
  </insert>
   


2.MySql
(1)ibatis配置文件:
  
   <insert id="insertPetitionLetter" parameterMap="PetitionLetterParam">
  	  <selectKey resultClass="int" keyProperty="id" type="pre">      
           <![CDATA[SELECT LAST_INSERT_ID() AS ID ]]>   
           <!-- 该方法LAST_INSERT_ID()与数据库连接绑定 -->        
      </selectKey> 
    <![CDATA[  
  	insert into t_petitionletter(id, identifier, reporter, letterInformation, 
  	                             supervision, processFlow) values
  	                             (?,?,?,?,?,?)
   ]]>  
  </insert>
   

    

你可能感兴趣的:(oracle,sql,xml,mysql,ibatis)