mybatis-spring-boot-starter mysql insert get id

860 views
Skip to first unread message

davaa...@gmail.com

unread,
Sep 22, 2017, 5:44:52 AM9/22/17
to mybatis-user
used xml mapper. mySql db.
not return value of column "id". return value is 1.
<insert id="insert" parameterType="User" keyColumn="id" keyProperty="id" parameterMap="parameters" useGeneratedKeys="true">
and tried
       <selectKey keyProperty="id" resultType="int" order="after">
        SELECT MAX(id) FROM lut_users;
       </selectKey>
how get id or value of last inserted data?

Tim

unread,
Sep 22, 2017, 2:01:39 PM9/22/17
to mybati...@googlegroups.com
Why do you have both parameterMap and parameterType set? you can remove parameterMap for mybatis.

If that doesn't fix it then:

What version are you using?
Can you show the model and insert statement?

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Guy Rouillier

unread,
Sep 22, 2017, 4:55:54 PM9/22/17
to mybati...@googlegroups.com

mysql_insert_id();

should retrieve the last auto-increment value used.  I don't have a lot of experience with MySQL, but make sure this all happens within a single transaction to ensure you get the correct value.  In general, using MAX() is not a reliable method in a multi-user database due to race conditions.  Someone else may insert a row immediately after you do, and MAX() would retrieve the id of *that* row, not yours.

--
Guy Rouillier
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages