MyBatis插入后返回主键
2017-12-30 21:59
162 查看
插入单条记录返回主键:
<insert id="insertOne" parameterType="User" useGeneratedKeys="true" keyProperty="id"> INSERT INTO user (id,name,age,sex,job,birthday,authority_id) VALUES (#{id},#{name},#{age},#{sex},#{job},#{birthday}, #{authorityId}) </insert>
批量插入返回主键:
<insert id="insertBatch" parameterType="List" useGeneratedKeys="true" keyProperty="id"> INSERT INTO user( id,name,age,sex,job,birthday,authority_id) VALUES <foreach collection="list" item="item" index="index" separator=","> (#{item.id},#{item.name},#{item.age},#{item.sex}, #{item.job},#{item.birthday},#{item.authorityId}) </foreach> </insert>
返回主键主要是useGeneratedKeys=”true”和keyProperty=”id”在起作用。
useGeneratedKeys=”true”表示开启JDBC的getGenereatedKeys方法获取主键,keyProperty=”id”表示将主键赋值到对象的id属性中。
注意:批量插入返回主键需要将MyBatis的版本升级到3.3.1以上。并且在数据库的url后要加上开启批量操作的指令:
&allowMultiQueries=true
相关文章推荐
- mybatis+mysql返回插入数据主键
- Mybatis插入后返回主键
- Mybatis返回插入的主键
- MyBatis插入数据返回主键
- MyBatis与IBatis插入返回主键
- 使用Mybatis时,在插入一条数据记录返回主键id值
- MyBatis在Oracle中插入数据并返回主键的问题解决
- MyBatis+MySQL 返回插入的主键ID
- MyBatis在insert插入操作时返回主键ID的配置(推荐)
- Mybatis+Mysql 批量插入的时候返回主键ID
- mybatis 插入数据时返回主键
- hao947 : Mybatis resultMap配置插入和主键自增返回 : 好947
- MyBatis在insert插入操作时返回主键ID的配置
- MyBatis+MySQL 返回插入的主键ID
- MyBatis插入语句返回主键值
- mybatis 进行插入操作,如何返回主键问题
- MyBatis+MySQL 返回插入的主键ID
- MyBatis在insert插入操作时返回主键ID的配置
- MyBatis+MySQL 返回插入的主键ID
- MyBatis :Insert (返回主键、批量插入)