mybatis insert 返回主键id

mybatis insert 返回主键id

一、如果是springMVC框架的话。

前提条件:

需要在ibatis  Configuration中设置UseGeneratedKeys为true

Configuration cfg = new org.apache.ibatis.session.Configuration();

cfg.setUseGeneratedKeys(true);

设置完以后有2种方式返回insert 主键

方式1:

<insert id="insertSelect" parameterType="com.heque.pojo.fileinfo.FileInfo" keyProperty="id" useGeneratedKeys="true">

方式2:再查

<insert id="insertSelective" parameterType="com.heque.user.pojo.queqiaouser.QueqiaoUser">

  <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
       select LAST_INSERT_ID()
   </selectKey>

</insert>

二、比较方便,使用注解,springMVC和springboot(不需要前提条件)项目都可以使用

在mapper中的insert方法上加入注解:@Options(useGeneratedKeys = true)

猜你喜欢

转载自blog.csdn.net/wang20y8/article/details/85766963