MyBatis 插入时候获取自增主键(1:写sql。2:注解@generatedvalue)

时间:2017-06-29 20:29:48   收藏:0   阅读:2328

MyBatis 插入时候获取自增主键

方法有二

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

 

 

@generatedvalue

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!