MyBatis插入时候获取自增主键方法

MyBatis 3.2.6插入时候获取自增主键方法有二ide

 

以MySQL5.5为例:it

 

方法1:class

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

 

方法2:方法

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

 

插入前实体id属性为0;view

插入后实体id属性为保存后自增的id;vi

相关文章
相关标签/搜索