mybatis insert returns auto increment

Add the properties "useGeneratedKeys" and "keyProperty" in the Mybatis Mapper file, where keyProperty is the property name of the Java object, not the field name of the table .

method 1:

    <insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id">
        insert into person(name,pwd) values(#{name},#{pwd})
    </insert>

 

Method 2:

    <insert id="insert" parameterType="Person">
        <selectKey keyProperty="id" resultType="long">
            select LAST_INSERT_ID()
        </selectKey>
        insert into person(name,pwd) values(#{name},#{pwd})
    </insert>

 

The entity id attribute is 0 before insertion;

After insertion, the entity id attribute is the id that is automatically incremented after saving;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327099205&siteId=291194637