mybatis新增数据后获取自增主键
Posted 花语苑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis新增数据后获取自增主键相关的知识,希望对你有一定的参考价值。
mybatis对应mysql
<insert id="insert" parameterType="com.timestech.wsgk.web.model.SysOrg" useGeneratedKeys="true"> --------------即可获取service中实体类的自增ID insert into BJLT.SYS_ORG (ID, NAME, P_ID, P_NAME, REMARK, LEVELS, STATUS, CREATE_TIME, CREATE_ID, CREATE_NAME, UPDATE_TIME, UPDATE_ID, UPDATE_NAME, TREE_PATH, RESERVE1, RESERVE2, RESERVE3, RESERVE4, RESERVE5, IS_ROOT, IS_BUILT_IN ) values (#{id,jdbcType=DECIMAL}, #{name,jdbcType=VARCHAR}, #{pId,jdbcType=DECIMAL}, #{pName,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, #{levels,jdbcType=DECIMAL}, #{status,jdbcType=DECIMAL}, #{createTime,jdbcType=DATE}, #{createId,jdbcType=VARCHAR}, #{createName,jdbcType=VARCHAR}, #{updateTime,jdbcType=DATE}, #{updateId,jdbcType=VARCHAR}, #{updateName,jdbcType=VARCHAR}, #{treePath,jdbcType=VARCHAR}, #{reserve1,jdbcType=VARCHAR}, #{reserve2,jdbcType=VARCHAR}, #{reserve3,jdbcType=VARCHAR}, #{reserve4,jdbcType=VARCHAR}, #{reserve5,jdbcType=VARCHAR}, #{isRoot,jdbcType=DECIMAL}, #{isBuiltIn,jdbcType=DECIMAL} ) </insert>
mybatis对应oracle
<insert id="insert" parameterType="com.timestech.wsgk.web.model.SysOrg" > <selectKey keyProperty="id" resultType="_long" order="BEFORE"> -------------------获取oracle的自增序列,在新增之前先赋值 SELECT BJLT.SYS_ORG_SEQUENCE.Nextval from DUAL </selectKey> insert into BJLT.SYS_ORG (ID, NAME, P_ID, P_NAME, REMARK, LEVELS, STATUS, CREATE_TIME, CREATE_ID, CREATE_NAME, UPDATE_TIME, UPDATE_ID, UPDATE_NAME, TREE_PATH, RESERVE1, RESERVE2, RESERVE3, RESERVE4, RESERVE5, IS_ROOT, IS_BUILT_IN ) values (#{id,jdbcType=DECIMAL}, #{name,jdbcType=VARCHAR}, #{pId,jdbcType=DECIMAL}, #{pName,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, #{levels,jdbcType=DECIMAL}, #{status,jdbcType=DECIMAL}, #{createTime,jdbcType=DATE}, #{createId,jdbcType=VARCHAR}, #{createName,jdbcType=VARCHAR}, #{updateTime,jdbcType=DATE}, #{updateId,jdbcType=VARCHAR}, #{updateName,jdbcType=VARCHAR}, #{treePath,jdbcType=VARCHAR}, #{reserve1,jdbcType=VARCHAR}, #{reserve2,jdbcType=VARCHAR}, #{reserve3,jdbcType=VARCHAR}, #{reserve4,jdbcType=VARCHAR}, #{reserve5,jdbcType=VARCHAR}, #{isRoot,jdbcType=DECIMAL}, #{isBuiltIn,jdbcType=DECIMAL} ) </insert>
以上是关于mybatis新增数据后获取自增主键的主要内容,如果未能解决你的问题,请参考以下文章
mybatis框架---mybatis插入数据后获取自增主键