开发者

Generating DAO with iBATIS problem

开发者 https://www.devze.com 2023-01-11 05:36 出处:网络
I have created a table in MSSQL having following structure - **Table Name - TestTable** id (int) (Primary key)

I have created a table in MSSQL having following structure -

**Table Name - TestTable**
id (int) (Primary key)
name (varchar) 
owner (varchar)

I have given Identity specification for column 'id' with auto increment with 1 for each new row. When I created iBatis artifacts for TestTable, the insert function is getting generated, in the DAO, with following signature -

 void insert(TestTable record); 

I want the insert function to return the 开发者_开发技巧newly generated 'id' for that row, instead of void.

How to achieve this?


In your ibatis SqlMap file, add the following:

<insert id="myInsertStatement">
    INSERT INTO TestTable (name, owner)
    VALUES (#name#, #owner#)
    <selectKey keyProperty="id" resultClass="int">
         SELECT SCOPE_IDENTITY()
    </selectKey>
</insert>

Or you could replace Select Scope_identity with another SQL query designed to return the last identity column inserted. The Scope_identity is specific to MS SQL (as requested in the question).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号