mybatis insert 返回主鍵id
阿新 • • 發佈:2019-01-04
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)