日期:2014-05-16  浏览次数:20573 次

iBatis中的insert如何返回个类数据库的主键
<SPAN style="COLOR: #ff0000">当我们在ibatis中使用一对一的时候,想取得自增加字段的值。      
  
Ibatis也提供这种取值方式,具体的配置如下 :</SPAN>   
  
DROP TABLE IF EXISTS `t_blog`;   
CREATE TABLE `t_blog`(   
  `id` bigint NOT NULL auto_increment,    -- blog编号   
  `name` varchar(255) NOT NULL ,  -- blog名称   
  `description` varchar(255) default '' ,  -- blog介绍   
  `domain` varchar(255) NOT NULL ,  -- blog域名   
  PRIMARY KEY  (`id`)   
)   
TYPE=InnoDB DEFAULT CHARSET=utf8 ;   
  
ibatis中对应的配置如下:   
<!-- 添加博客 -->   
<insert id="insertBlog">   
        insert into t_blog    
            (name, description, domain)   
        values    
            (#name#, #description#, #domain#)   
        <selectKey resultClass="int" keyProperty="id" >   
            SELECT @@IDENTITY AS ID   
        </selectKey>    
</insert>   
DAO中的调用方法:   
  
public int insertBlog(Blog blog) throws DAOException {   
        try{   
            return ((Integer)getSqlMapClientTemplate().insert("insertBlog", blog)).intValue();   
        }   
        catch(Exception ex){   
            logger.error(ex);   
            throw new DAOException("创建博客发生错误...");   
        }       
}   
test insertBlog方法返回的int为自增加的id。   
  
[转帖2]   
  
  
Xml代码    
<!-- Oracle SEQUENCE -->      
<insert id="insertProduct-ORACLE" parameterClass="com.domain.Product">      
<selectKey resultClass="int" keyProperty="id" type="pre">      
<![CDATA[SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL]]>      
</selectKey>      
<![CDATA[insert into PRODUCT (PRD_ID,PRD_DESCRIPTION) values #id#,#description#)]]>      
</insert>      
     
<!-- Microsoft SQL Server IDENTITY Column -->      
<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">      
<![CDATA[insert into PRODUCT (PRD_DESCRIPTION) values (#description#) ]]>      
<selectKey resultClass="int" keyProperty="id" type="post">      
<![CDATA[SELECT @@IDENTITY AS ID ]]> <!-- 该方法不安全 应当用SCOPE_IDENTITY() 但这个函数属于域函数,需要在一个语句块中执行。 -->     
</selectKey>      
</insert>     
     
<!-- Microsoft SQL Server IDENTITY Column 改进-->      
<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">      
<selectKey resultClass="int" keyProperty="id">      
<![CDATA[insert into PRODUCT (PRD_DESCRIPTION) values (#description#)    
    SELECT SCOPE_IDENTITY() AS ID ]]>     
</selectKey>      
</insert>     
     
<!-- Mysql Last Insert Id -->     
<insert id="insertProduct-Mysql" parameterClass="com.domain.Product">     
<![CDATA[insert into PRODUCT(PRD_DESCRIPTION) values (#description#)]]>      
<selectKey resultClass="int" keyProperty="id">     
<![CDATA[SELECT LAST_INSERT_ID() AS ID ]]> <!-- 该方法LAST_INSERT_ID()与数据库连接绑定,不会发生上述MS SQL Server的函数问题。 -->     
</selectKey>     
</insert>     
<!-- Oracle SEQUENCE -->    
<insert id="insertProduct-ORACLE" parameterClass="com.domain.Product">    
<selectKey resultClass="int" keyProperty="id" type="pre">    
<![CDATA[SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL]]>    
</selectKey>    
<![CDATA[insert into PRODUCT (PRD_ID,PRD_DESCRIPTION) values #id#,#description#)]]>    
</insert>    
  
<!-- Microsoft SQL Server IDENTITY Column -->    
<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">    
<![CDATA[insert into PRODUCT (PRD_DESCRIPTION) values (#description#) ]]>    
<selectKey resultClass="int" keyProperty="id" type="post">    
<![CDATA[SELECT @@IDENTITY AS ID ]]> <!-- 该方法不安全 应当用SCOPE_IDENTITY() 但这个函数属于域函数,需要在一个语句块中执行。 -->   
</selectKey>    
</insert>   
  
<!-- Microsoft SQL Server IDENTITY Column 改进-->    
<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">    
<selectKey resultClass="int" key