How to return ids on Inserts with mybatis in mysql with annotations?

The insert update and delete statements return the number of affected rows, as is common with database APIs.

The , and statements return the number of affected rows, as is common with database APIs. If a new ID is generated for the inserted row, it is reflected in the object you passed as a parameter. So for example, if you call mapper.

Insert(someObject) inside your annotated insert method, after inserting, you can call someObject. GetId (or similar) to retrieve it. Using the options of , you can tweak how (by providing an SQL statement) and when (before or after the actual insertion) the id is generated or retrieved, and where in the object it is URL1 may be instructive to use the MyBatis generator to generate classes from a database schema and have a look at how inserts and updates are handled.

Specifically, the generator produces "example" classes that are used as temporary containers to pass around data.

Actually, it's possible to do it, with the @Options annotation (provided you're using auto_increment or something similar in your database) : @Insert("insert into table3 (id, name) values(null, #{name})") @Options(useGeneratedKeys=true, keyProperty="idName") int insertTable3(SomeBean myBean); Note that the keyProperty="idName" part is not necessary if the key property in SomeBean is named "id". There's also a keyColumn attribute available, for the rare cases when MyBatis can't find the primary key column by himself. Please also note that by using @Options, you're submitting your method to some default parameters ; it's important to consult the doc (linked below -- page 60 in the current version)!(Old answer) The (quite recent) @SelectKey annotation can be used for more complex key retrieval (sequences, identity() function...).

Here's what the MyBatis 3 User Guide (pdf) offers as examples : This example shows using the @SelectKey annotation to retrieve a value from a sequence before an insert: @Insert("insert into table3 (id, name) values(#{nameId}, #{name})") @SelectKey(statement="call next value for TestSequence", keyProperty="nameId", before=true, resultType=int. Class) int insertTable3(Name name); This example shows using the @SelectKey annotation to retrieve an identity value after an insert: @Insert("insert into table2 (name) values(#{name})") @SelectKey(statement="call identity()", keyProperty="nameId", before=false, resultType=int. Class) int insertTable2(Name name).

You can get your generated ids from save methods, lets say a bean with ID and name properties, bean. SetName("xxx"); mapper. Save(bean); // here is your id logger.

Debug(bean. GetID).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions