2013-02-08 62 views
0

我正在使用IBATIS 2.3.4版本。 數據庫是MS SQL Server。iBATIS - 自動生成密鑰異常

我想在表T_PROFILE中插入記錄,它具有Identity主列自動生成的值。

我IBATIS配置

<insert id="insertProfile" parameterClass="profileDO" useGeneratedKeys="true" keyProperty="profileId"> 
     INSERT INTO T_PROFILE (E_ID,PROFILE_NAME,DEFAULT_PROFILE) 
     VALUES(#eId#, #profileName#, #isDefaultProfile#) 

     <selectKey resultClass="java.lang.Long" keyProperty="profileId" > 
      SELECT @@IDENTITY AS profileId 
     </selectKey> 
    </insert> 

這是給誤差

Caused by: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: org.xml.sax.SAXParseException: Attribute "useGeneratedKeys" must be declared for element type "settings". 
    at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:62) 
    at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:62) 
    at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:55) 
    at org.springframework.orm.ibatis.SqlMapClientFactoryBean.buildSqlMapClient(SqlMapClientFactoryBean.java:339) 
    ... 160 more 
Caused by: org.xml.sax.SAXParseException: Attribute "useGeneratedKeys" must be declared for element type "settings". 

回答

1

如果您的身份列在數據庫中設置,下面的查詢本身會工作。 useGeneratedKeys不需要。

<insert id="insertProfile" parameterClass="profileDO" > 
    INSERT INTO T_PROFILE (E_ID,PROFILE_NAME,DEFAULT_PROFILE) 
    VALUES(#eId#, #profileName#, #isDefaultProfile#) 

    <selectKey resultClass="long" keyProperty="profileId" > 
     SELECT @@IDENTITY AS profileId 
    </selectKey> 
</insert>