0

I’m using MySQL 5.5.46 with MyBatis 3.3 and Spring 4.2. I have a table with a primary key column that is of type VARCHAR(32). In my mapping file, I have this statement. Notice that I use MySql’s uuid() to auto-generate an ID. How do I then take this ID and populate it in the returned object? I’m trying LAST_INSERT_ID() but that isn’t doing it …

<insert id="insertMyObject" parameterType="org.mainco.myproject.domain.MyObject" keyProperty="id"  keyColumn="id">
    insert into cb_myproject_sync_entity_link(id, links_self, links_schools, links_teachers, links_students, links_sections, links_grade_levels, links_contacts)
    values(replace(uuid(), '-', ''),#{linksSelf}, #{linksSchools}, #{linksTeachers}, #{linksStudents}, #{linksSections}, #{linksGradeLevels}, #{linksContacts})     
   <selectKey keyProperty="id" resultType="String" order="AFTER">SELECT LAST_INSERT_ID();</selectKey>
</insert>
cdmh
  • 3,294
  • 2
  • 26
  • 41
Dave
  • 15,639
  • 133
  • 442
  • 830
  • Possible duplicate of [MyBatis, how to get the auto generated key of an insert? \[MySql\]](http://stackoverflow.com/questions/18507508/mybatis-how-to-get-the-auto-generated-key-of-an-insert-mysql) – Lucky Nov 02 '15 at 07:14

1 Answers1

0

Try to use this setting in mybatis configuration:

<settings>
   <setting name="useGeneratedKeys" value="true"/>
</settings>
KillSwitch
  • 132
  • 2
  • 10