Mybatis source parsing --KeyGenerator

KeyGenerator

The interface master key used in generating insert, when the primary key automatically generated and injected into the insert to user parameters.

public interface KeyGenerator {

//在执行insert前执行,设置属性order=“BEFORE”
  void processBefore(Executor executor, MappedStatement ms, Statement stmt, Object parameter);
  
//在执行insert后执行,设置属性order=“AFTER”
  void processAfter(Executor executor, MappedStatement ms, Statement stmt, Object parameter);

}

Jdbc3KeyGenerator

Can only be used to retrieve the database-generated auto-increment primary keys can not be used for primary keys are not supported from the growing database.
Corresponds useGeneratedKeys global mybatis-config.xml configuration, and <Insert>useGeneratedKeys attribute node
Jdbc3KeyGenerator.processBefore () is empty implementation, only implements processAfter ()

example:

<insert id = "test" useGeneratedKeys = "true" KeyProperty="Id">
	insert into user(username,pwd) values 
		<froeach item = "item" collection="list" separator=",">
			(#{item.username},#{item.pwd})
		</foreach>
</insert>

Process is performed as shown in FIG.

Here Insert Picture Description

SelectKeyGenerator

For all databases, regardless of whether the support increment primary keys are supported.

  1. BEFORE
    before performing an insert <SelectKey>acquired primary key values, and then provided to the user according to the arguments ResultSet obtained to get the complete user argument, and then to insert into the database
  2. AFTER
    principle is after performing insert, performing <SelectKey>the obtaining primary key values, and then provided to the user according to the arguments to the resulting ResultSet

Process is performed as shown in FIG.
Here Insert Picture Description

Published 98 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/Mutou_ren/article/details/102785378