Problems using mybatis to insert statements based on ID auto-increment?
Resolved in mybatis core configuration file
MYSQL solution:
<!-- public void addDept(Dept dept); MySQL supports self-incrementing primary keys; to obtain self-incrementing primary keys, mybatis also uses statement.getGeneratedKeys() To get the added number in the foreground, add two properties useGeneratedKeys="true": Use the auto-incrementing primary key to get the primary key value keyProperty: The corresponding primary key property, that is, after mybatis gets the primary key value, which property of javaBean will this value be encapsulated to --> <insert id="addDept" useGeneratedKeys="true" keyProperty="deptno" databaseId="mysql" > insert into dept(dname,loc) values (#{dname},#{loc}); </insert>
Oracle solution:
<!--Oracle does not support auto-increment; Oracle uses sequence sequences to simulate auto-increment; The primary key of the data inserted each time is the value obtained from the sequence: if the song gets this value? --> <insert id="addDept" databaseId="oracle" > <!-- keyProperty: Which property of the JavaBean the detected primary key is encapsulated to order="BEFORE": the current SQL is run before insert AFTER: The current SQL is run after the insert resultType: find out the return value type of the data BEFORE running order: First run selectKey to query id's sql; find out that the value of id is encapsulated to the id attribute of javaBean Then run the inserted SQL; you can take out the value corresponding to the id attribute AFTER: Run the inserted SQL first (take the new value from the sequence as id) Then run the selectKey query id sql --> <selectKey keyProperty="deptno" order="BEFORE" resultType="Integer"> <!-- Write a SQL statement to query the primary key--> <!-- BEFORE:--> SELECT seq_deptno.nextval FROM dual <!-- AFTER: SELECT seq_deptno.currval FROM dual --> </selectKey> <!-- The primary key when inserting is taken from the sequence --> <!-- BEFORE: --> insert into dept(deptno,dname,loc) values (#{deptno},#{dname},#{loc}) <!-- AFTER: insert into dept(deptno,dname,loc) values (seq_deptno.nextval,#{dname},#{loc}) --> </insert>