In layman's language mybatis - useGeneratedKeys parameter usage, keyColumn and keyProperty

Here Insert Picture Description

In MyBatis, allowing setting name is ' useGeneratedKeys' present position three parameters:

  • In settingssetting element useGeneratedKeys parameters
  • In xmlsetting mapper useGeneratedKeys parameters
  • In 接口setting mapper useGeneratedKeys parameters

In the different positions of useGeneratedKeysthe parameters,The final results are identical, but different sphere of influence

# Set useGeneratedKeys parameter settings element

The official argument is that the role of this parameter is:

"Allow JDBCsupport automatic generation of primary keys, is necessary to drive compatible"

How to understand the meaning of this sentence?

Its intention is to say:

Support for automatic generation of database record in the primary key
, such as: MySQL, SQL Server
this time setting useGeneratedKeysthe parameter valuetrue
After performing adding records to the database may be acquired automatically generated主键ID.
.
In fact, settings元素the set is a useGeneratedKeysGlobal Parameters
butOnly affect the interface mapperXml mapper to afford effective

<settings>
<!-- 
允许JDBC支持自动生成主键,需要驱动兼容。 

如果设置为true则这个设置强制使用自动生成主键,

尽管一些驱动不能兼容但仍可正常工作(比如 Derby)。 
-->
<setting name="useGeneratedKeys" value="true" />
</settings>

In this case, after the addition of the interface in record map updating primary key values ​​entity class, number of rows Effect

Such as:
increment = 50 ID
User data entity class, insert MySQL,
the user.id to true // == 50

public interface TestMapper {
    // 受全局useGeneratedKeys参数控制,添加记录之后将返回主键id
    @Insert("insert into test(name,descr,url,create_time,update_time) values(#{name},#{descr},#{url},now(),now())")
    Integer insertOneTest(Test test);
}

But
note that if this time the interface Mapper and explicitly set the useGeneratedKeysparameters
so annotation Mapper useGeneratedKeysparameter value overrides the global parameter settings useGeneratedKeys elements set.

For example:
first in settingssetting element global useGeneratedKeysparameter value true,
and then set the interface mapper useGeneratedKeysparameter value false,
.
After the addition recording will not return 注解ID.

// 在接口映射器中设置的useGeneratedKeys参数值将会覆盖在settings元素中设置全局useGeneratedKeys参数值
@Options(useGeneratedKeys = false, keyProperty = "id", keyColumn = "id")
@Insert("insert into test(name,descr,url,create_time,update_time) values(#{name},#{descr},#{url},now(),now())")
Integer insertOneTest(Test test);

Further, the settingsglobal settings in the element useGeneratedKeysparameters for the xmlmapper is invalid .

After return to the main key ID, if desired in the xml mapper performs adding records must explicitly set in the argument of true useGeneratedKeys xml mapper.

# UseGeneratedKeys configuration parameters mapper xml

<!-- 插入数据:返回记录的id值 -->
<insert id="insertOneTest" 
		parameterType="org.chench.test.mybatis.model.Test" 
		useGeneratedKeys="true" 
		keyProperty="id" 
		keyColumn="id">
    insert into test(name,descr,url,create_time,update_time) 
    values(#{name},#{descr},#{url},now(),now())
</insert>

xmlMapper configured useGeneratedKeysparameter will only xmlaffect the mapper
and useGeneratedKeys global parameter values set in the settings for the xml element mapping does not have any effect

Parameter Set # useGeneratedKeys interface mapper

// 设置useGeneratedKeys为true,返回数据库自动生成的记录主键id
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
@Insert("insert into test(name,descr,url,create_time,update_time) values(#{name},#{descr},#{url},now(),now())")
Integer insertOneTest(Test test);

Note: the interface provided mapper useGeneratedKeysparameters covering <settings>元the corresponding parameter value set in the pixel.

# KeyColumn and keyProperty

  • keyColumn Database column name (or alias)

  • keyProperty Java parameter names to be encapsulated in the class

Here Insert Picture Description


【reference】

Published 296 original articles · won praise 61 · views 7118

Guess you like

Origin blog.csdn.net/LawssssCat/article/details/103937578