iBatis의 insert가 주 키 ibatis insert로 되돌아오는 방법

    ibatis         ,          。   

Ibatis         ,        :

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" 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>

[  ]

    <insert id="insertAuthor" parameterClass="Author">
        insert into AUTHOR( AUT_NAME ) values ( #name# )
        <selectKey resultClass="int" keyProperty="id">
            select LAST_INSERT_ID() as id
        </selectKey>
    </insert>

Integer resultId = ( (Integer)getSqlMap().insert("insertAuthor", newer) ).intValue();

좋은 웹페이지 즐겨찾기