Mybatis获取自增的id

获取自增的id
第一种方式

获取自增id的方式:
<insert id="add2" parameterType="employee">
        insert into employee(name,age, sex,phone) values(#{name},#{age},#{sex},#{phone})
        <!--  order 执行selectKey标签中语句的先后顺序,resultType 获取获取的自增值的类型 -->
        <selectKey order="AFTER" keyProperty="id" resultType="int">
            <!--  mysql中,针对同一个连接对象,插入数据后 直接执行如下语句,可以获取自增的id-->
            select last_insert_id()
        </selectKey>
</insert>

第二种方式

<insert id="add2" parameterType="employee"  useGeneratedKeys="true" keyProperty="id">
	insert into employee(username) values(#{username})
</insert>
发布了15 篇原创文章 · 获赞 11 · 访问量 2795

猜你喜欢

转载自blog.csdn.net/YCarlos/article/details/99486358