Mybatis 3 bug
Author: chszs
Reprinted please indicate the source!
Version: mybatis 3.0.4
BUG: the insert statement does not correctly return the value of the auto-increment field. MySQL database script:
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 AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
Corresponding sqlmap ing <insert id = "addworker" parametertype = "worker" usegeneratedkeys = "true" keyproperty = "ID"> <selectkey resulttype = "int" keyproperty = "ID" Order = "after"> select last_insert_id () as value </selectkey> insert into worker (pin, firstname, lastname) values (# {pin}, # {firstname}, # {firstname}) </Insert>
The corresponding entity-class worker. Java, Dao, interface, and configuration are omitted ......
The result of execution is that only 1 is returned and the value of the Self-incrementing field is not obtained.
----------------
The alternative solution to this bug is:
Specifically configure for ID retrieval:
<Select id = "getworkerid" resulttype = "int">
Select last_insert_id ()
</SELECT>
It is equivalent to executing two SQL statements to obtain the required id value. [E01]