java - 请问mybatis新增操作使用UUID 如何返回UUID
ringa_lee
ringa_lee 2017-04-18 10:46:40
0
4
687
ringa_lee
ringa_lee

ringa_lee

reply all(4)
Ty80

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

阿神

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>
Ty80

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

伊谢尔伦

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template