MyBatis, how to get the auto generated key of an insert? [MySql]

For me it is working like this (mybatis 3.x) ..
The id must be set auto increment in mysql table

<insert id="createEmpty" parameterType="Project" useGeneratedKeys="true" keyProperty="project.projectId" keyColumn="PROJECT_ID">
    INSERT INTO PROJECT (TITLE,DESCRIPTION)
    VALUES
    (#{title},#{description})
</insert>

NOTE keyProperty="project.projectId" and useGeneratedKeys="true"

my interface is:

public int createEmpty(@Param("project") Project project, @Param("title") String title,
    @Param("description") String description);

finally to get the value (that will be automatically assigned to the pojo’s id property) i use:

projectRepository.createEmpty(p, "one", "two");
System.err.print(p.getProjectId() + "\n");

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)