Mybatis自动生成key值(selectKey和useGeneratedKeys)
2017-12-23 22:18
435 查看
insert和update操作中会常常用到自动生成主键的问题。
selectKey和useGeneratedKeys属性
useGeneratedKeys (insert and update only) This tells MyBatis to use the JDBC getGeneratedKeys method to retrieve keys generated internally by the database (e.g.auto increment fields in RDBMS like MySQL or SQL Server). Default: false
(( 仅 对 insert 和update有 用 ) 这 会 告 诉 MyBatis 使 用 JDBC 的 getGeneratedKeys 方法来取出由数据(比如:像 MySQL 和 SQL Server 这样的数据库管理系统的自动递增字段)内部生成的主键。默认值:false。)
keyProperty
(insert and update only) Identifies a property into which MyBatis will set the key value returned by
getGeneratedKeys , or by a selectKey child element of the insert statement. Default: unset .
Can be a comma separated list of property names if multiple generated columns are expected.
((仅对 insert和update有用) 标记一个属性, MyBatis会通过 getGeneratedKeys 或者通过 insert 语句的 selectKey 子元素设置它的值。默认: 不设置。)
keyColumn
(insert and update only) Sets the name of the column in the table with a generated key. This is only required
in certain databases (like PostgreSQL) when the key column is not the first column in the table. Can be a
comma separated list of columns names if multiple generated columns are expected.
selectKey和useGeneratedKeys使用
2
3
4
5
6
7
8
9
10
11
通过selectKey在插入操作前或者操作后获取key值,做为字段插入或返回字段。(此段代码获取的序列值id作为字段值插入到users表中)
2
3
4
如果数据库支持自增长主键字段(比如mysql、sql server)设置useGeneratedKeys=”true”和keyProperty,这样就可以插入主键id值
oracle则不支持自增长id,设置useGeneratedKey=”false”,如果设置true则会有报错信息。通过nextval函数,如SEQ_table.Nextval生成id
3.插入更新一条数据时,可以使用selectKey获取id操作。当做多条数据插入更新时,而selectKey只能使用一次,此时应该使用useGeneratedKeys操作。
从Mybatis源码分析selectKey和useGeneratedKeys属性的使用
selectKey和useGeneratedKeys属性
useGeneratedKeys (insert and update only) This tells MyBatis to use the JDBC getGeneratedKeys method to retrieve keys generated internally by the database (e.g.auto increment fields in RDBMS like MySQL or SQL Server). Default: false
(( 仅 对 insert 和update有 用 ) 这 会 告 诉 MyBatis 使 用 JDBC 的 getGeneratedKeys 方法来取出由数据(比如:像 MySQL 和 SQL Server 这样的数据库管理系统的自动递增字段)内部生成的主键。默认值:false。)
keyProperty
(insert and update only) Identifies a property into which MyBatis will set the key value returned by
getGeneratedKeys , or by a selectKey child element of the insert statement. Default: unset .
Can be a comma separated list of property names if multiple generated columns are expected.
((仅对 insert和update有用) 标记一个属性, MyBatis会通过 getGeneratedKeys 或者通过 insert 语句的 selectKey 子元素设置它的值。默认: 不设置。)
keyColumn
(insert and update only) Sets the name of the column in the table with a generated key. This is only required
in certain databases (like PostgreSQL) when the key column is not the first column in the table. Can be a
comma separated list of columns names if multiple generated columns are expected.
selectKey和useGeneratedKeys使用
<insert id="insert"> <selectKey keyProperty="id" resultType="int" order="BEFORE"> <if test="_databaseId == 'oracle'"> select seq_users.nextval from dual </if> <if test="_databaseId == 'db2'"> select nextval for seq_users from sysibm.sysdummy1" </if> </selectKey> insert into users values (#{id}, #{name}) </insert>1
2
3
4
5
6
7
8
9
10
11
通过selectKey在插入操作前或者操作后获取key值,做为字段插入或返回字段。(此段代码获取的序列值id作为字段值插入到users表中)
<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id"> insert into Author (username,password,email,bio) values (#{username},#{password},#{email},#{bio}) </insert>1
2
3
4
如果数据库支持自增长主键字段(比如mysql、sql server)设置useGeneratedKeys=”true”和keyProperty,这样就可以插入主键id值
oracle则不支持自增长id,设置useGeneratedKey=”false”,如果设置true则会有报错信息。通过nextval函数,如SEQ_table.Nextval生成id
3.插入更新一条数据时,可以使用selectKey获取id操作。当做多条数据插入更新时,而selectKey只能使用一次,此时应该使用useGeneratedKeys操作。
从Mybatis源码分析selectKey和useGeneratedKeys属性的使用
相关文章推荐
- Mybatis自动生成key值(selectKey和useGeneratedKeys)
- Mybatis自动生成key值(selectKey和useGeneratedKeys)
- MyBatis自动生成id useGeneratedKeys
- mysql数据库插入数据获取自增主键的三种方式(jdbc PreparedStatement方式、mybatis useGeneratedKeys方式、mybatis selectKey方式)
- Mybatis插入时返回自增主键(selectKey和useGeneratedKeys)
- mybatis 中哪些数据库支持 useGeneratedKeys="true"
- mybatis中,<insert>标签中useGeneratedKeys和keyColumn作用
- Mybatis插入语句useGeneratedKeys="true"的用法
- Mybatis 配置文件 useGeneratedKeys 参数
- mybatis新增时返回主键id,useGeneratedKeys的用法
- mybaties中的selectKey和useGeneratedKeys=true
- mybatis自动生成mapper中的insert与insertseletive,updateByPrimaryKey,updateByPrimaryKeySelective方法的区别
- mysql获取自动生成的主键报错:Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.execu
- Mybatis利用useGeneratedKeys返回最新插入记录的id Parameter 'id' not found. Available
- mybatis番外篇(一)自动生成键selectkey
- mybatis自动生成mapper中的insert与insertseletive,updateByPrimaryKey,updateByPrimaryKeySelective方法的区别
- mybatis insert 自动生成key
- Item5: Explicitly disallow the use of compiler-generated functions you do not want--若不想使用编译器自动生成的函数,就该明确拒绝
- mybatis 数据库表中外键关系存在时 useGeneratedKeys="true" keyProperty="id"
- MyBatis之主键自增——useGeneratedKeys