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>
Read More:
- Mybatis sets the primary key Auto-Increment error: No setter found for the keyProperty
- How to Solve the Primary Key of mybatisPlus Inserted Data is too Large Issue
- When the database table field is set to self incrementing, use the entity class to insert or update the data to solve the error (Hibernate Framework)
- [Solved] Error getting generated key or setting result to parameter object. UnsupportedOperationException
- [Solved] Mybatis-Plus Error: Invalid bound statement (not found)
- [Solved] Specified key was too long; max key length is 767 bytes
- How to Solve Mybatis error: invalid bound statement (not found)
- Mybatis Error: Invalid bound statement (not found)
- H2 memory database Oracle mode page error: rg.springframework.dao.InvalidDataAccessResourceUsageException: could not prepar
- Springboot uses Oracle database to report property ‘sqlsessionfactory’ or ‘sqlsessiontemplate’ are required
- [Solved] Mybatis.generator error: Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2
- Public Key Retrieval is not allowed [How to Solve]
- [Solved] Mybatis Batch Modify Error: multi-statement not allow
- [Solved] stream Convert list to map Error: java.lang.IllegalStateException: Duplicate key
- [Solved] Mybatis Error: attempting to get column ‘XX’ from result set
- Notes for migrating Oracle jdk8 to openjdk11
- Springboot mybatis Integrate Error: Invalid bound statement (not found): com…DepartmentMapper.save
- Mybatis plus configuration console prints complete SQL statement with parameters
- [Solved] Mybatis Error: Could not find resource mybatis-conf.xml
- [Solved] mybatis plus Insert Error: mybatis plus Error setting null for parameter #1 with JdbcType OTHER