1. 程式人生 > 其它 >使用Mybatis 關於資料庫採用UUID策略自增,在xml中設定insert語句

使用Mybatis 關於資料庫採用UUID策略自增,在xml中設定insert語句

主鍵自增方式:採用主鍵自增 欄位為int型別, 第二種為UUID策略自增

此文主要UUID形式自增

由於採用UUID形式,當insert時 主鍵的值無法獲取,所以採用 <selectKey>標籤, 此標籤可以獲取到UUID的value,

  1. <!--selectKey會將
    select replace(uuid(),'-','') AS id 以下為 語句A
    的結果傳入到insert的主鍵裡面,
  2. keyProperty對應的model中的主鍵的屬性名,這裡是user中的id,因為它跟資料庫的主鍵對應
  3. order 有倆個引數1.AFTER 2.BEFORE
  4. 1.AFTER 表示 語句A 在insert執行之後執行,多用與自增主鍵,
  5. 2.BEFORE表示 語句A在insert執行之前執行
  6. resultType主鍵型別
<insert id="createMarketing" parameterType="BizMarketing" useGeneratedKeys="true" keyProperty="id">
        <selectKey keyProperty="id" resultType="java.lang.String"
                   order="BEFORE">
            select replace(uuid(),'-','') AS id
        
</selectKey> insert into tbl_marketing_activities <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null and id != ''">id,</if> <if test="owner != null and owner !=''">owner,</if> <if test="name != null and name !=''"
>name,</if> <if test="startDate != null and startDate !=''">startDate,</if> </trim> <trim prefix="values(" suffix=")" suffixOverrides=","> <if test="id != null and id != ''">#{id},</if> <if test="owner != null and owner !=''">#{owner},</if> <if test="name != null and name !=''">#{name},</if> <if test="startDate != null and startDate !=''">#{startDate},</if> </trim> </insert>