参考来自:mybatis mysql 批量insert 返回主键 注意:必须要在mybatis3.3.1及其以上才能实现. 1.service List branchEntryList = (ArrayList<Entry>)entryMap.get("branchEntryList"); branchDao.insertBatch(branchEntryList); 2.dao 3.xml 注意这里是list,不管参数名叫什么,这里都是list. 4.效果 执行前,bra…
需求:批量插入数据,并返回每条数据的主键(序列),因为这里是采用序列生成唯一的主键的, 其实oracle批量 插入操作有几种,网上百度都是有相关资源的.但是笔者现在的需求是,不仅批量插入数据后,并返回每条数据的主键, 网上查阅了一番始终没有找到,相应办法,倒是针对mysql貌似mybatsi是支持批量返回主键的,因为笔者没有测试,所有不敢妄下言论. 好了,说了这么多,直接进入正题: 1.参照网上的相关资源,如图所示 <insert id="saveEPDetail" useGen…
需求:使用批量插入后,需要insert之后的每一条记录的ID 注意:Mybatis3.3.1的版本以后支持批量插入后返回主键ID 示例: domin.java: public class User { private int d; private String name; private String pwd; public long getId() { return id; } public void setId(long id) { this.id = id; } public String…
在myBatis中获取刚刚插入的数据的主键id是比较容易的 , 一般来说下面的一句话就可以搞定了 , 网上也有很多相关资料去查. @Options(useGeneratedKeys = true, keyProperty = "money_record_id") 但是相比较 , 批量插入数据时获取相数据的主键Id就会变得非常难了 , 上面的办法是没用的 . 可以按照如下办法去解决 : 1.新建一个sql如下 , 在一个事务中 , 可以通过如下sql获取到批量插入的数据的第一条数据的主键…
方式一: 需要在映射文件中添加如下片段: <insert id="insertProduct" parameterType="domain.model.ProductBean" > <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="productId"> SELECT LAST_INSERT_ID()…
开发过程中经常遇到需要插入一条数据,并且返回这条数据自增的主键,在MyBatis中只需要在mapper中添加keyProperty属性即可 在mapper中添加keyProperty属性 <insert id="insertAndGetId" parameterType="com.lili.log.dto.LogPay" useGeneratedKeys="true" keyProperty="payId" > i…
参考来自:https://stackoverflow.com/questions/40647600/postgresql-multi-value-upserts/46233907#46233907 1.before insert 2.insert sql 3.after insert 注意这里有两个容易出错的点:1.如果label字段不是必填的,要注意语法会不会出错.万一label为空. 2.数据库版本太低就不支持这段语法.比如9.3…
<insert id="insertUser" parameterType="User"> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> SELECT LAST_INSERT_ID() </selectKey> insert into users(name, age, g…
需要在insert方法中添加 <insert id="insertSelective" parameterType="com.midou.ott.model.MDActivity" useGeneratedKeys="true" keyProperty="id"> 加上上面红色部分,keyProperty中的id,是MDActivity对象的中的Id 使用时直接从MDActivity对象中获取到ID…
<insert id="batchInsert" parameterType="java.util.List"> INSERT INTO TEST( A, B, C, D, E ) <foreach collection="list" index="index" item="item" separator="UNION ALL"> SELECT #{item.a,…