MyBatis insert语句selectKey标签和返回主键

    科技2025-06-22  5

    一、简要概述

    keyProperty:对应的domain 对象中需要被赋值的属性,一般是主键resultType:表示的是返回主键的类型order:如果设置为 BEFORE,那么它会首先选择主键,设置 keyProperty 然后执行插入语句。如果设置为 AFTER,那么先执行插入语句,然后是 selectKey 元素 注意:SelectKey需要注意order属性,像MySQL一类支持自动增长类型的数据库中,order需要设置为after才会取到正确的值,像Oracle这样取序列的情况,需要设置为before。mybatis代码片段 <selectKey keyProperty="tpl_inst_id" resultType="long" order="BEFORE"> SELECT SEQ_BST_SMS_TPL_INST.NEXTVAL FROM DUAL </selectKey>

    二、使用场景

    当我们往数据库插入一条记录以后,有时我们接下来的操作需要这条记录的主键。如果在插入后在去查询一下数据库,显然不太遵循Java开发的规范(不够优雅和效率),mybatis正好提供了insert之后返回主键的功能。

    三、代码介绍

    BstSmsTplInstMapper.xml文件代码如下 <insert id="save" parameterType="com.asiainfo.bst.entity.BstSmsTplInst"> <selectKey keyProperty="tpl_inst_id" resultType="long" order="BEFORE"> SELECT SEQ_BST_SMS_TPL_INST.NEXTVAL FROM DUAL </selectKey> INSERT INTO BST_SMS_TPL_INST <trim prefix="(" suffix=")" suffixOverrides=","> <if test="tpl_inst_id != null"> tpl_inst_id, </if> <if test="busi_intf_seq != null"> busi_intf_seq, </if> <if test="msg_code != null"> msg_code, </if> <if test="sys_code != null"> sys_code, </if> <if test="scene_code != null"> scene_code, </if> <if test="rule_code != null"> rule_code, </if> <if test="tpl_code != null"> tpl_code, </if> <if test="tpl_form != null"> tpl_form, </if> <if test="tpl_engine != null"> tpl_engine, </if> <if test="tpl_param_inst != null"> tpl_param_inst, </if> <if test="src_nbr != null"> src_nbr, </if> <if test="dest_nbr_array != null"> dest_nbr_array, </if> <if test="staff_code != null"> staff_code, </if> <if test="send_priority != null"> send_priority, </if> send_time, create_time, </trim> <trim prefix="VALUES (" suffix=")" suffixOverrides=","> <if test="tpl_inst_id != null"> #{tpl_inst_id,jdbcType=NUMERIC}, </if> <if test="busi_intf_seq != null"> #{busi_intf_seq,jdbcType=VARCHAR}, </if> <if test="msg_code != null"> #{msg_code,jdbcType=VARCHAR}, </if> <if test="sys_code != null"> #{sys_code,jdbcType=VARCHAR}, </if> <if test="scene_code != null"> #{scene_code,jdbcType=VARCHAR}, </if> <if test="rule_code != null"> #{rule_code,jdbcType=VARCHAR}, </if> <if test="tpl_code != null"> #{tpl_code,jdbcType=VARCHAR}, </if> <if test="tpl_form != null"> #{tpl_form,jdbcType=TIMESTAMP}, </if> <if test="tpl_engine != null"> #{tpl_engine,jdbcType=TIMESTAMP}, </if> <if test="tpl_param_inst != null"> #{tpl_param_inst,jdbcType=VARCHAR}, </if> <if test="src_nbr != null"> #{src_nbr,jdbcType=VARCHAR}, </if> <if test="dest_nbr_array != null"> #{dest_nbr_array,jdbcType=VARCHAR}, </if> <if test="staff_code != null"> #{staff_code,jdbcType=VARCHAR}, </if> <if test="send_priority != null"> #{send_priority,jdbcType=VARCHAR}, </if> sysdate, sysdate, </trim> </insert> 业务逻辑代码如下: try { ### xxx实例表实例化后保存数据库 bstSmsTplInstDao.save(bstSmsTplInst); ### 获取主键ID resMap.put("tplInstId",bstSmsTplInst.getTpl_inst_id().toString()); } catch (DataAccessException dae) { updateMsgStatus(bstBusiMsg, ErrorMsg.HANDLE_FAIL_STATUS, countMap, dae.getMessage()); log.error("插入模板实例表异常,请检查!", dae); errorMap.put("status", ErrorMsg.HANDLE_FAIL_STATUS); return errorMap; } //8. 写入实时任务表 long tplInstId = Long.valueOf(resultMap.get("tplInstId"));

    四、代码介绍

    以上就是我在工作中所遇到的场景,以及如何处理该问题的方法。如果有什么问题欢迎大家在评论区进行探讨!!!

    Processed: 0.012, SQL: 8