MyBatis之主键自增——useGeneratedKeys
来源:互联网 发布:年度质量数据汇总图表 编辑:程序博客网 时间:2024/05/29 08:00
如果你的数据库支持主键自增,例如MySQL和SQL Server,那么你可以简单的设置 useGeneratedKeys="true" ,用keyProperty 去指定主键名称, if the Authortable above had used an auto-generated column type for the id, the statement would be modified as follows:
<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id"> insert into Author (username,password,email,bio) values (#{username},#{password},#{email},#{bio})</insert>
If your database also supports multi-row insert, you can pass a list or an array of Authors and retrieve the auto-generated keys.
<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id"> insert into Author (username, password, email, bio) values <foreach item="item" collection="list" separator=","> (#{item.username}, #{item.password}, #{item.email}, #{item.bio}) </foreach></insert>
MyBatis has another way to deal with key generation for databases that don't support auto-generated column types, or perhaps don't yet support the JDBC driver support for auto-generated keys.
Here's a simple (silly) example that would generate a random ID (something you'd likely never do, but this demonstrates the flexibility and how MyBatis really doesn't mind):
<insert id="insertAuthor"> <selectKey keyProperty="id" resultType="int" order="BEFORE"> select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1 </selectKey> insert into Author (id, username, password, email,bio, favourite_section) values (#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})</insert>
- MyBatis之主键自增——useGeneratedKeys
- MyBatis之主键自增——selectKey
- MyBatis之insert——主键自增和自定义主键
- 【MyBatis】——自增主键与非自增主键返回
- 【MyBatis】——自增主键与非自增主键返回
- 【MyBatis】——自增主键与非自增主键返回
- mybatis新增时返回主键id,useGeneratedKeys的用法
- mybatis自增主键配置
- mybatis 自增主键配置
- mybatis自增主键配置
- mybatis 自增主键配置
- mybatis 自增主键配置
- mybatis自增主键配置
- mybatis自增主键配置
- mybatis获取自增主键
- mybatis自增主键无效
- mybatis+oracle主键自增
- mybatis设置自增主键
- Webpack
- (转) Logistic regression (逻辑回归) 概述
- 标题就是想不出来啊啊啊啊
- 2017届中兴飞流校招大数据测试工程师面经
- 卷积神经网络(CNN)新手指南
- MyBatis之主键自增——useGeneratedKeys
- C++-正确使用memset
- Viewdraghelper解析
- mkdir-yum-tree命令应用案例
- 顺丰查询接口参数及对接demo
- Hadoop2.7.3 + Hive 2.1.0 + HBase 1.2.3 整合
- 如何让html的div内容溢出后显示滚动条
- maven学习笔记
- 如何发送AT指令