search

Home  >  Q&A  >  body text

java - 请问mybatis新增操作使用UUID 如何返回UUID

ringa_leeringa_lee2802 days ago680

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:48:40

    Before calling the persistence layer, generate a UUID yourself, put it in the object and the method returns the UUID.

    reply
    0
  • 阿神

    阿神2017-04-18 10:48:40

    When the dao interface defines the save method, try to modify the return type to String to see if it is received.
    The default is to return the number of affected rows. If selectKey is configured, the content of selectKey may be returned.

    When using MyBatis as the persistence layer, the insert statement does not return the primary key value of the record by default, but returns the number of inserted records; if the business layer needs to obtain the primary key of the record, this function can be completed through configuration

    For the Sequence primary key, you must specify a primary key value for the record to be inserted before executing insert sql, such as Oracle and DB2. You can use the following configuration method:

    <insert id="add" parameterType="vo.Category">
    <selectKey resultType="java.lang.Short" order="BEFORE" keyProperty="id">
    SELECT SEQ_TEST.NEXTVAL FROM DUAL
    </selectKey>
    
    insert into category (name_zh, parent_id,
    
    show_order, delete_status, description
    )
    values (#{nameZh,jdbcType=VARCHAR},
    #{parentId,jdbcType=SMALLINT},
    #{showOrder,jdbcType=SMALLINT},
    #{deleteStatus,jdbcType=BIT},
    #{description,jdbcType=VARCHAR}
    )
    </insert>
    

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:48:40

    Mybatis returns the number of affected rows by default. To return the ID, you need to write it separately.
    If it is an oracle database, uuid can be written as sys_guid()

    If it is mysql, it must be the same as mentioned above, give an ID first and then save it

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:48:40

    There is no need to generate a uuid in advance in the code. This problem can be solved by understanding the keyProperty attribute in selectKey.


    In this way, when selectKey is generated, the generated UUID will be set into the current object

    reply
    0
  • Cancelreply