Inserting a plurality of primary batches Mybatis return key
Forum someone in need when returning multiple bulk insert the corresponding primary key
Tony enthusiastic teacher to help write a test example. Mybatis tested two versions, low version does not support, you pay attention.
<insert id="insertBatchInterest" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
insert into xxxxx ( `uid`)
values
<foreach collection="list" item="item" index="index"
separator=",">
(#{item.uid,jdbcType=INTEGER} )
</foreach>
</insert>
Java code execution
@Test
public void test() {
List<CouponInterestPO> batchList = new ArrayList<>();
CouponInterestPO po = new CouponInterestPO();
po.setUid(111);
batchList.add(po);
po = new CouponInterestPO();
po.setUid(222);
batchList.add(po);
couponInterestDao.insertBatchInterest(batchList);
for (CouponInterestPO c : batchList) {
LOG.info("返回主键====>"+c.getId());
}
Test is successful
mybatis
3.4.1 successful test, the test results shown above
mybatis
Version 3.27 is not supported will complain, report
org.apache.ibatis.binding.BindingException: Parameter 'id' not found.
Available parameters are [list]
Under concern, Bowen first time to push.
There are technical problems can increase my number of public questions. Sweep swept away, promotion and pay rise is you.