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

来源:互联网 发布:linux终端进度条 编辑:程序博客网 时间:2024/05/29 18:00

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

 

以MySQL5.5为例:

 

方法1:

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

 

方法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>

 

插入前实体id属性为0;

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

0 0
原创粉丝点击