Mybatis Insert get the primary key is inserted into the ID of the self-energizing

<insert id="接口方法明" parameterType="传参类型" useGeneratedKeys="true" 
keyColumn="数据库中主键名称" keyProperty="(实体类中主键对应的名字或者自定义名称)">
         .... sql 代码.....
</insert>

useGeneratedKeys = "true": generating a primary key is turned on

keyColumn = "name database primary key"

eyProperty = "(corresponding to the primary key of the entity class name or custom name)">

If you pass a parameter entity type , please note keyProperty corresponding entity class should be the primary key ID;

If you pass a parameter is the Map , so you can customize the name.

Note: The newly added primary key primary key id id field when the add operation does not directly returned, but a new record is added after the add operation returns to the parameter passed class.

E.g:

<insert id="addUser" parameterType="map" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO saishiguanli.users
        (createDate, modifyDate, isEnabled, isLocked, lastLoginDate, lastLoginIp, lockDate, name, encodePassword)
        VALUES
        (#{createDate},#{modifyDate},#{isEnabled},#{isLocked},#{lastLoginDate},#{lastLoginIp},#{lockDate},#{name},#{encodePassword})
    </insert>
After calling addUser method 
int id = Integer.parseInt (hashMap.get ( " id") toString ().); You can get the primary key ID;

If the entity class is passed, then the method call int id = user.getId (); to

Published 372 original articles · won praise 89 · Views 230,000 +

Guess you like

Origin blog.csdn.net/sizaif/article/details/105018148