mybatis设置添加新对象数据的时候id使用uuid类型自动写入数据库

在mapper.xml文件中的insert中增加以下标红的代码 实现id采用自增方式写入数据库,并且写入一个uuid字符

<insert id="insert" parameterType="ssm.item.admin.bean.Depart" useGeneratedKeys="true" keyProperty="departId">
    <selectKey keyProperty="departId" resultType="java.lang.String"
    order="BEFORE">
    select replace(uuid(),'-','') from dual
    </selectKey>
    insert into depart (depart_id, depart_name, depart_code
      )
    values (#{departId,jdbcType=VARCHAR}, #{departName,jdbcType=VARCHAR}, #{departCode,jdbcType=VARCHAR}
      )
  </insert>

<insert id="insertSelective" parameterType="ssm.item.admin.bean.Depart" useGeneratedKeys="true" keyProperty="departId"> <selectKey keyProperty="departId" resultType="java.lang.String" order="BEFORE"> select replace(uuid(),'-','') from dual </selectKey> insert into depart <trim prefix="(" suffix=")" suffixOverrides=","> <if test="departId != null"> depart_id, </if> <if test="departName != null"> depart_name, </if> <if test="departCode != null"> depart_code, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="departId != null"> #{departId,jdbcType=VARCHAR}, </if> <if test="departName != null"> #{departName,jdbcType=VARCHAR}, </if> <if test="departCode != null"> #{departCode,jdbcType=VARCHAR}, </if> </trim> </insert>

 

posted @ 2021-06-16 10:39  panchanggui  阅读(346)  评论(0编辑  收藏  举报