Article Directory
In MyBatis, allowing setting name is ' useGeneratedKeys
' present position three parameters:
- In
settings
setting element useGeneratedKeys parameters - In
xml
setting mapper useGeneratedKeys parameters - In
接口
setting mapper useGeneratedKeys parameters
In the different positions of useGeneratedKeys
the 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
JDBC
support 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 settinguseGeneratedKeys
the 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 mapper,Xml 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 useGeneratedKeys
parameters
so annotation Mapper useGeneratedKeys
parameter value overrides the global parameter settings useGeneratedKeys elements set.
For example:
first insettings
setting element globaluseGeneratedKeys
parameter valuetrue
,
and then set the interface mapperuseGeneratedKeys
parameter valuefalse
,
.
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 settings
global settings in the element useGeneratedKeys
parameters for the xml
mapper 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>
xml
Mapper configured useGeneratedKeys
parameter will only xml
affect 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 useGeneratedKeys
parameters 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
【reference】
-
http://blog.csdn.net/suwu150/article/details/52895855 - primary key generated automatically Mybatis
-
https://www.cnblogs.com/nuccch/p/9069644.html - in simple terms mybatis usage parameters of useGeneratedKeys
-
https://www.cnblogs.com/woyujiezhen/p/11643944.html - in keyColumn of about mybatis