Mybatis自动生成key值(selectKey和useGeneratedKeys)
来源:互联网 发布:帝国cms好用吗 编辑:程序博客网 时间:2024/05/29 06:46
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使用
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
通过selectKey在插入操作前或者操作后获取key值,做为字段插入或返回字段。(此段代码获取的序列值id作为字段值插入到users表中)
- 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属性的使用
阅读全文
0 0
- Mybatis自动生成key值(selectKey和useGeneratedKeys)
- Mybatis自动生成key值(selectKey和useGeneratedKeys)
- mybatis番外篇(一)自动生成键selectkey
- Mysql-ibatis 自动生成主键(selectKey)
- Mysql-ibatis 自动生成主键(selectKey)
- 用ibatis selectKey 返回自动生成的主键值
- ibatis自动生成键selectkey(Oracle、MYSQL、MSSQL、SQLITE)
- ibatis自动生成键selectkey(Oracle、MYSQL、MSSQL、SQLITE)
- mybatis的selectKey用来给不支持自动生成主键的数据库用,什么意思?
- Mysql-ibatis 自动生成主键(selectKey)
- Mybatis - SelectKey
- mybatis的selectKey和sql标签
- Insight mybatis insert useGeneratedKeys
- mybatis useGeneratedKeys = "true"
- Mybatis 配置文件 useGeneratedKeys 参数
- mybatis中,<insert>标签中useGeneratedKeys和keyColumn作用
- Mybatis——<selectKey>生成主键/获取插入数据主键
- Mybatis 示例之 SelectKey
- 【iOS笔记-2】iOS MVP总结
- c#利用SWIG调用c++dll学习总结
- Leetcode 241. Different Ways to Add Parentheses
- python
- ASP Cookie
- Mybatis自动生成key值(selectKey和useGeneratedKeys)
- 朴素贝叶斯算法--机器学习
- 刚装 Linux 的IP设置
- css背景铺满
- win10任务栏开始菜单等系统图标点击无反应的解决方法
- 水仙花数
- C/S B/S架构区别
- Oracle之物化视图
- linux 找出两个txt文件中内容不同的部分并输出