对于数据表中的主键id,我们一般会使用uuid产生随机数来保证唯一性,那么在项目编写中我们怎么来创建uuid呢,下面是两种方式
- 在mybatis中创建uuid
1 <insert id="add" useGeneratedKeys="true" parameterType="com.linyr.bean.Person">
2 <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
3 select UUID()
4 </selectKey>
5 insert into person(id,name,sex,age,address)
6 values(#{id},#{name},#{sex},#{age},#{address})
7 </insert>
- 在上面mybatis中生成的uuid在工作中出现过重复,所以后来修改成使用java中产生的uuid来插入:
productEntity.setId(UUID.randomUUID().toString())
在插入就行了,目前为止还没有出现过重复