mybatis 添加一条新数据并返回此数据的ID(主键)

利用Mybatis 的 selectKey来获得:

方法一:

<!-- 添加部门 返回部门ID -->  
<insert id="addDept" parameterType="com.demo.model.Department" keyProperty="id">  
    <selectKey keyProperty='id' resultType='int' order='AFTER'  >  
        select LAST_INSERT_ID();  
    </selectKey>  
     insert into department(<include refid="departmentAllField"/>)   
        values(#{departmentId},#{departmentName},#{departmentManagerName},#{companyId});  
 </insert> 

方法二:

<insert id="addDept" parameterType="com.demo.model.Department" useGeneratedKeys="true" keyProperty="id">  
  insert into department(<include refid="departmentAllField"/>)   
   values(#{departmentId},#{departmentName},#{departmentManagerName},#{companyId});  
</insert>

注意:insert 标签中的 keyProperty 和 selectKey标签块中的 LAET_INSERT_ID() ,另外 order属性 对于 oracl为 BEFORE; mysql为AFTER

方法三:

<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.edu.archive.business.domain.grade.pojo.model.ApplicationRecord" useGeneratedKeys="true">

  insert into bj_application_record (parent_id, class_no, student_name, 
    kinsfolk_relation, `status`, refuse_remark, 
    create_time, update_time, version
    )
  values (#{parentId,jdbcType=INTEGER}, #{classNo,jdbcType=VARCHAR}, #{studentName,jdbcType=VARCHAR}, 
    #{kinsfolkRelation,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT}, #{refuseRemark,jdbcType=VARCHAR}, 
    #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}, #{version,jdbcType=INTEGER}
    )
</insert>

mybatis 添加一条新数据并返回此数据的ID(主键) - 蓝鲸也是鲸 - 博客园

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值