mybatis_id生成

主键自增长


关于mysql的selectKey 标签设置 通过调用SELECT LAST_INSERT_ID()
mysql表的主键必须为自动增长的AUTO_INCREMENT
<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >
    SELECT LAST_INSERT_ID()
</selectKey>

具体的例子为

<insert id="insert" parameterType="com.liangbo.xing.flexibletranscation.domain.ftm.TranscationMsgSubDo" >
    <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER" >
        SELECT LAST_INSERT_ID()
    </selectKey>
    
    insert into t_transcation_msg_sub (messageId, status, topicId, 
    subId, maxRetry, retryInterval, 
    retry, nextRetryTime, version, 
    createTime, updateTime)
    values (#{messageid,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, #{topicid,jdbcType=VARCHAR}, 
    #{subid,jdbcType=VARCHAR}, #{maxretry,jdbcType=INTEGER}, #{retryinterval,jdbcType=INTEGER}, 
    #{retry,jdbcType=INTEGER}, #{nextretrytime,jdbcType=TIMESTAMP}, #{version,jdbcType=VARCHAR}, 
    #{createtime,jdbcType=TIMESTAMP}, #{updatetime,jdbcType=TIMESTAMP})
</insert>

执行完了 transcationMsgSubDao.insert(TranscationMsgSubDo transcationMsgSubDo);后再执行
                                          transcationMsgSubDo.getId();   的方法时会发现ID属性是有值的 其实是通过selectKey回写的

主键为UUID

mysql表的主键为varchar类型
mysql的selectKey 标签设置 select  replace(uuid(),'-','')   from dual
 

<insert id="save" parameterType="com.qbsea.modules.zexample1.model.Zexample1Model">
    <selectKey keyProperty="id" resultType="String" order="BEFORE">  
        select  replace(uuid(),'-','')   from dual  
    </selectKey>
    INSERT INTO TB_ZEXAMPLE1(ID,NAME,AGE,AMOUNT,ADD_TIME,STATUS)
    VALUES(#{id},#{name},#{age},#{amount},#{addTime},#{status})
</insert>

猜你喜欢

转载自blog.csdn.net/maqingbin8888/article/details/81697802
今日推荐