mybatis-spring-boot-starter mysql insert get id

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

mybatis-spring-boot-starter mysql insert get id

davaasurenx
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?

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Tim
Reply | Threaded
Open this post in threaded view
|

Re: mybatis-spring-boot-starter mysql insert get id

Tim
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?

On Fri, Sep 22, 2017 at 5:44 AM, <[hidden email]> wrote:
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?

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re[2]: mybatis-spring-boot-starter mysql insert get id

Guy Rouillier-2

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

------ Original Message ------
From: "Tim" <[hidden email]>
Sent: 9/22/2017 2:01:14 PM
Subject: Re: mybatis-spring-boot-starter mysql insert get id

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?

On Fri, Sep 22, 2017 at 5:44 AM, <[hidden email]> wrote:
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?

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.