您的位置:首页 > 其它

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

2017-04-10 16:23 295 查看
SelectKey在Mybatis中是为了解决Insert数据时数据库不支持主键自动生成的问题,useGeneratedKeys是支持主键自增时使用的方法

方法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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: