hbwf / mybatis

Automatically exported from code.google.com/p/mybatis
0 stars 0 forks source link

mybatis return ID #412

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the MyBatis are you using?
MyBatis 3.0.5

Please describe the problem.  Unit tests are best!
I applied MBG generated myabtis code, the database is mySQL, ID is the 
automatic growth

 <insert id="insertSelective" parameterType="com.devt2.pojo.Dept">
    <!--
      WARNING - @mbggenerated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Tue Aug 30 09:36:53 CST 2011.
    -->
    <selectKey keyProperty="deptid" order="AFTER" resultType="java.lang.Integer">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into dept
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="deptname != null">
        deptname,
      </if>
      <if test="status != null">
        status,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="deptname != null">
        #{deptname,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>

What is the expected output? What do you see instead?

Expected output: ID = 2
But in fact output: ID = 1

Please provide any additional information below.
I began to doubt my spring affairs, but then I found this article, I think the 
problem this time myabtis, return return 1 if the number of records that 
MBG1.3.1 let alone return to the help documentation is recorded ID value

Original issue reported on code.google.com by 229594...@163.com on 10 Oct 2011 at 3:09

GoogleCodeExporter commented 9 years ago
I saw the article

http://code.google.com/p/mybatis/issues/detail?id=287

Original comment by 229594...@163.com on 10 Oct 2011 at 3:11

GoogleCodeExporter commented 9 years ago
If the table has an auto generated key (an identity column or from a sequence), 
and the <generatedKey> configuration element is specified, then MBG will 
generate an appropriate <selectKey> element and will return the value of the 
generated key.

Original comment by 229594...@163.com on 10 Oct 2011 at 3:14

Attachments:

GoogleCodeExporter commented 9 years ago
I need to change the MBG documentation.  For MyBatis3, the method will not 
return the new key - it will return the number of rows inserted.  This is a 
change in the MyBatis3 API over iBATIS2.

To get the new key value, you need to check the property in the parameter 
object (deptid in your example).

Original comment by jeffgbut...@gmail.com on 10 Oct 2011 at 6:36

GoogleCodeExporter commented 9 years ago
I updated the documentation to explain the differences between iBATIS2 and 
MyBatis3 in the insert methods.  See here for details:

http://code.google.com/p/mybatis/source/detail?r=3943

Original comment by jeffgbut...@gmail.com on 10 Oct 2011 at 6:58