使用Mybatis 关于数据库采用UUID策略自增,在xml中设置insert语句
主键自增方式:采用主键自增 字段为int类型, 第二种为UUID策略自增
此文主要UUID形式自增
由于采用UUID形式,当insert时 主键的值无法获取,所以采用 <selectKey>标签, 此标签可以获取到UUID的value,
- <!--selectKey 会将
select replace(uuid(),'-','') AS id 以下为 语句A的结果传入到insert的主键里面, - keyProperty 对应的model中的主键的属性名,这里是 user 中的id,因为它跟数据库的主键对应
- order 有俩个参数1.AFTER 2.BEFORE
- 1.AFTER 表示 语句A 在insert执行之后执行,多用与自增主键,
- 2.BEFORE 表示 语句A在insert执行之前执行
- 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>

浙公网安备 33010602011771号