how to learn primary keys of all inserted records?

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

how to learn primary keys of all inserted records?

oleksandr.hlapov
i'm using mybatis 3.4.5

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>

lets say i have 2 insert methods. First accept only one record, other – list of records

<insert id="insert" parameterType="Commission">
<selectKey keyProperty="id" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO commission (ID, StartDate, EndDate, Value, Version)
values (#{id,jdbcType=INTEGER}, #{partyId,jdbcType=INTEGER}, #{toPartyId,jdbcType=INTEGER},
#{startDate,jdbcType=DATE}, #{endDate,jdbcType=DATE}, #{value,jdbcType=DOUBLE},
#{typeValue,jdbcType=BIT}, #{type,jdbcType=BIT}, #{version,jdbcType=TIMESTAMP})
</insert>
<insert id="insertAll" parameterType="java.util.List">
INSERT INTO commission
(<include refid="Base_Column_List" />)
VALUES
 (<foreach item="model" index="index" collection="list" open="" close="" separator="),(">
#{model.id},
#{model.startDate},
#{model.endDate},
#{model.value},
#{model.version}
</foreach>)
</insert>

now when i try to call first

Commission commission = new CommissionBuilder().build();

DbAdapter.getSession().getMapper(CommissionMapper.class).insert(commission);
DbAdapter.commit();

System.out.println(commission.getId());

everything work fine, i see some correct (expected) number. Is it possible to learn primary key (id) after 'insert all' query?

--
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: how to learn primary keys of all inserted records?

oleksandr.hlapov
PS i've been trying to add

  <selectKey keyProperty="id" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>

into insert all method (before INSERT) with no result

On Tuesday, November 14, 2017 at 2:28:02 PM UTC+2, [hidden email] wrote:
i'm using mybatis 3.4.5

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>

lets say i have 2 insert methods. First accept only one record, other – list of records

<insert id="insert" parameterType="Commission">
<selectKey keyProperty="id" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO commission (ID, StartDate, EndDate, Value, Version)
values (#{id,jdbcType=INTEGER}, #{partyId,jdbcType=INTEGER}, #{toPartyId,jdbcType=INTEGER},
#{startDate,jdbcType=DATE}, #{endDate,jdbcType=DATE}, #{value,jdbcType=DOUBLE},
#{typeValue,jdbcType=BIT}, #{type,jdbcType=BIT}, #{version,jdbcType=TIMESTAMP})
</insert>
<insert id="insertAll" parameterType="java.util.List">
INSERT INTO commission
(<include refid="Base_Column_List" />)
VALUES
 (<foreach item="model" index="index" collection="list" open="" close="" separator="),(">
#{<a href="http://model.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmodel.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFWSbRdkPumn7mJd8bEfCB-Sox3mA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmodel.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFWSbRdkPumn7mJd8bEfCB-Sox3mA&#39;;return true;">model.id},
#{model.startDate},
#{model.endDate},
#{model.value},
#{model.version}
</foreach>)
</insert>

now when i try to call first

Commission commission = new CommissionBuilder().build();

DbAdapter.getSession().getMapper(CommissionMapper.class).insert(commission);
DbAdapter.commit();

System.out.println(commission.getId());

everything work fine, i see some correct (expected) number. Is it possible to learn primary key (id) after 'insert all' query?

--
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: how to learn primary keys of all inserted records?

oleksandr.hlapov
never mind, got a solution

<insert id="insertAll" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id" keyColumn="ID">

On Tuesday, November 14, 2017 at 2:30:17 PM UTC+2, [hidden email] wrote:
PS i've been trying to add

  <selectKey keyProperty="id" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>

into insert all method (before INSERT) with no result

On Tuesday, November 14, 2017 at 2:28:02 PM UTC+2, [hidden email] wrote:
i'm using mybatis 3.4.5

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>

lets say i have 2 insert methods. First accept only one record, other – list of records

<insert id="insert" parameterType="Commission">
<selectKey keyProperty="id" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO commission (ID, StartDate, EndDate, Value, Version)
values (#{id,jdbcType=INTEGER}, #{partyId,jdbcType=INTEGER}, #{toPartyId,jdbcType=INTEGER},
#{startDate,jdbcType=DATE}, #{endDate,jdbcType=DATE}, #{value,jdbcType=DOUBLE},
#{typeValue,jdbcType=BIT}, #{type,jdbcType=BIT}, #{version,jdbcType=TIMESTAMP})
</insert>
<insert id="insertAll" parameterType="java.util.List">
INSERT INTO commission
(<include refid="Base_Column_List" />)
VALUES
 (<foreach item="model" index="index" collection="list" open="" close="" separator="),(">
#{<a href="http://model.id" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmodel.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFWSbRdkPumn7mJd8bEfCB-Sox3mA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fmodel.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFWSbRdkPumn7mJd8bEfCB-Sox3mA&#39;;return true;">model.id},
#{model.startDate},
#{model.endDate},
#{model.value},
#{model.version}
</foreach>)
</insert>

now when i try to call first

Commission commission = new CommissionBuilder().build();

DbAdapter.getSession().getMapper(CommissionMapper.class).insert(commission);
DbAdapter.commit();

System.out.println(commission.getId());

everything work fine, i see some correct (expected) number. Is it possible to learn primary key (id) after 'insert all' query?

--
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.