mybatis随机生成可控制主键的方式
mybatis生成的主键,一般都是用数据库的序列,可是还有不同的写法,比如:
一、NUMBER类型的主键
<insert id="insertPeriodical" parameterType="cn.edu.hbcf.plugin.periodical.pojo.PeriodicalType">
<selectKey keyProperty="id" resultType="java.lang.Integer" order="BEFORE">
select nvl(max(ID),0)+1 from PERIODICAL_TYPE
</selectKey>
insert into PERIODICAL_TYPE
(ID, YEAR, PERIODICAL_NO, OPER_USER, OPER_DATE)
values
(#{id},
#{year,jdbcType=CHAR},
#{periodicalNo,jdbcType=INTEGER},
#{operUser,jdbcType=VARCHAR},
#{operDate,jdbcType=DATE})
</insert>
重点是:select nvl(max(ID),0)+1 from PERIODICAL_TYPE
二、VARCHAR类型的主键
<insert id="insertSmsRdSort" parameterType="SmsRdSort">
<selectKey resultType="java.lang.String" keyProperty="sortId" order="BEFORE">
select sys_guid() from dual
</selectKey>
insert into SMS_RD_SORT(SORT_ID,SORT_NAME,SORT_REMARK,TYPE_ID,UPDATE_USER,UPDATE_TIME,SORT_INDEX)
values(#{sortId},#{sortName,jdbcType=VARCHAR},#{sortRemark,jdbcType=VARCHAR},#{typeId,jdbcType=VARCHAR},#{updateUser},#{updateTime,jdbcType=TIMESTAMP},#{sortIndex,jdbcType=VARCHAR})
</insert>
重点是:select sys_guid() from dual