Tag Archives: mybatis statement.getGenreatedKeys()

Using mybatis statement.getGenreatedKeys(); usegeneratedkeys = “true”; using self incrementing primary key to get primary key value policy and Oracle do not support self incrementing, Oracle uses sequence

Parametertype: parameter type, which can be omitted
MySQL supports auto-incrementing primary key, which is also used by mybatis statement.getGenreatedKeys ();
usegeneratedkeys = “true”; use self incrementing primary key to get the primary key value strategy
keyproperty; specify the corresponding primary key property, that is, after mybatis gets the primary key value, which property of JavaBean encapsulates the value

Oracle does not support self incrementing; Oracle uses sequence to simulate self incrementing;
the primary key of data inserted each time is the value obtained from the sequence; how to get this value

<insert id="addEmp" databaseId="oracle">
        <!-- 
        keyProperty:Which property of the javaBean the primary key value is encapsulated to find out
        order="BEFORE":the current sql is run before inserting sql
               AFTER: the current sql is run after the insert sql
        resultType:the return value type of the checked out data

        BEFORE running order.
            first run selectKey query id sql; find out the id value encapsulated to the id property of the javaBean
            In the run insert sql; you can take out the value corresponding to the id property
        AFTER running order.
            Run the insert sql first (take the new value from the sequence as id)
            then run the selectKey query sql for id.
         -->
        <selectKey keyProperty="id" order="BEFORE" resultType="Integer">
            <!-- Write sql statement to query primary key -->
            <!-- BEFORE-->
            select EMPLOYEES_SEQ.nextval from dual 
            <!-- AFTER:
             select EMPLOYEES_SEQ.currval from dual -->
        </selectKey>

        <!-- The primary key at the time of insertion is obtained from the sequence -->
        <!-- BEFORE:-->
        insert into employees(EMPLOYEE_ID,LAST_NAME,EMAIL) 
        values(#{id},#{lastName},#{email<!-- ,jdbcType=NULL -->}) 
        <!-- AFTER:
        insert into employees(EMPLOYEE_ID,LAST_NAME,EMAIL) 
        values(employees_seq.nextval,#{lastName},#{email}) -->
    </insert>