简体   繁体   中英

How to return key after insert using MyBatis when the id/uuid as is stored as binary?

we currently have triggers in our database that hand out uuid for every record that i insert. When i am inserting records with mybatis i would like to get that uuid back instead of the numbers of rows that have been inserted.

From previous post i read that i could do it with

useGeneratedKeys="true" keyProperty="id"

But we store our uuid as binaries so i would like to get the non-binary uuid back from an insert. When we insert stuff we use functions like 'uuid2bin' and 'bin2uuid' so i was hoping to use a function like this to retrieve the newly generated uuid from the database (MySQL).

Any suggestions on how i could get the newly generated uuid back??

Two options I can think of 1) do the conversion in Java using a MyBatis TypeHandler or 2) wrap your insert with a stored procedure that returns the formatted UUID.

The issue with #1 would be that you are moving load from the DB to your application, which could have performance impacts if MySql is remote.

With #2, you need to use a <select> in MyBatis. But, you need to make sure that it actually commits. Also, if you are using MyBatis caching, you also need to set flushCache=true in the <select> .

I would use the <selectKey> tag inside the <insert> tag

<insert>
   <selectKey keyProperty="pk" resultType="Type" order="AFTER">
     select myDBFunction( (select triggerGeneratedColumnInBinary from myTable where pk = triggerLogicToRetrieveLastGenerated(...) ) );
   </selectKey>
   ...procedure call or insert...
</insert>

If you are sending the object and not a Hashmap this code will set the result of your interpreter function with the trigger generated column after the insertion. The method will still return number of rows but your object will have its key in place.

System.out.println(myObject.getPk()); //0
int rows = myMapper.insertMyClass(myObject); // sets the pk
System.out.println(myObject.getPK()); //324

useGeneratedKeys will not help you because it tells MyBatis to use the JDBC getGeneratedKeys method to retrieve keys generated internally by the database (eg auto increment fields in RDBMS like MySQL or SQL Server).

Method returns a value is the number of updated rows.The id of the incoming parameter is the id of insertd row.as follows:

 <insert id="insertSelectiveReturnKey" parameterType="com.test.dal.model.CostDO" useGeneratedKeys="true" keyProperty="id">
        insert into cost
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="name != null">
                name,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="name != null">
                #{name,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>


 CostDO costDO = new CostDO();
 costDO.setName("test");
 int updateNum = productMapper.insertSelectiveReturnKey(costDO);
 // updateNum is the number of updated rows.

productMapper.insertSelectiveReturnKey(costDO);
int id = costDO.getId();
// id is the id of insertd row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM