Auto-increment ID bug in mybatis 3 (continued)

Source: Internet
Author: User

Auto-increment ID bug in mybatis 3 (continued)

 

Author: chszs

Reprinted please indicate the source!


Since my last bug in mybatis 3, see: http://blog.csdn.net/chszs/archive/2011/03/31/6292719.aspx

I submitted the bug to mybatis official website, see http://code.google.com/p/mybatis/issues/detail? Id = 287

The problem is described as follows:

Eclipse Java Project:1. SqlMapConfig.xml<?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE configuration      PUBLIC "-//mybatis.org//DTD Config 3.0//EN"      "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><environments default="development"><environment id="development"><transactionManager type="JDBC" /><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull" /><property name="username" value="root" /><property name="password" value="adminadmin" /></dataSource></environment></environments><mappers><mapper resource="com/sqlmap/WorkerMapper.xml" /></mappers></configuration>  2. mysql.sqlUSE test;CREATE TABLE `worker` (  `id` INT(11) NOT NULL AUTO_INCREMENT,  `pin` VARCHAR(64) DEFAULT NULL,  `firstname` VARCHAR(64) DEFAULT NULL,  `lastname` VARCHAR(64) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=INNODB DEFAULT CHARSET=utf8;3. WorkerMapperpackage com.dao;import com.entity.Worker;public interface WorkerMapper {int insertWorker(Worker worker);}4. Worker.javapackage com.entity;public class Worker {    private Integer id;    private String pin;    private String firstname;    private String lastname;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getPin() {        return pin;    }    public void setPin(String pin) {        this.pin = pin == null ? null : pin.trim();    }    public String getFirstname() {        return firstname;    }    public void setFirstname(String firstname) {        this.firstname = firstname == null ? null : firstname.trim();    }    public String getLastname() {        return lastname;    }    public void setLastname(String lastname) {        this.lastname = lastname == null ? null : lastname.trim();    }}5. WorkerMapper.xml<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><mapper namespace="com.dao.WorkerMapper" >  <resultMap id="BaseResultMap" type="com.entity.Worker" >    <id column="id" property="id" jdbcType="INTEGER" />    <result column="pin" property="pin" jdbcType="VARCHAR" />    <result column="firstname" property="firstname" jdbcType="VARCHAR" />    <result column="lastname" property="lastname" jdbcType="VARCHAR" />  </resultMap>  <insert id="insertWorker" parameterType="com.entity.Worker" >  <selectKey resultType="int" keyProperty="id" order="AFTER"> select LAST_INSERT_ID()</selectKey>    insert into worker    <trim prefix="(" suffix=")" suffixOverrides="," >      <if test="pin != null" >        pin,      </if>      <if test="firstname != null" >        firstname,      </if>      <if test="lastname != null" >        lastname,      </if>    </trim>    <trim prefix="values (" suffix=")" suffixOverrides="," >      <if test="pin != null" >        #{pin,jdbcType=VARCHAR},      </if>      <if test="firstname != null" >        #{firstname,jdbcType=VARCHAR},      </if>      <if test="lastname != null" >        #{lastname,jdbcType=VARCHAR},      </if>    </trim>  </insert></mapper>6. Test.java/** * @Author: Li.Qiang * @Date:   2011-3-9 */package com.test;import java.io.Reader;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import com.entity.Worker;public class Test {public static void main(String[] args) throws Exception{String resource = "conf/SqlMapConfig.xml";Reader reader = Resources.getResourceAsReader(resource);SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);SqlSession session = factory.openSession();Worker worker = new Worker();worker.setPin("123456");worker.setFirstname("Tom");worker.setLastname("Jack");try{for(int i=0; i<20; i++){int tmpId = session.insert("insertWorker", worker);session.commit();System.out.println("Worker ID: " + tmpId);}} finally{session.close();}}}7. Test.java Output:Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1Worker ID: 1can't get the real id.
According to the feedback from the author of mybatis, the <Insert> statement itself has an int type return value, indicating the number of inserts. Therefore, it is impossible to use <selectkey> In the <Insert> statement to return the id value automatically generated by the auto-increment ID. You can use the <SELECT> statement to return the id value automatically generated by the auto-increment ID.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.