mybatis批量插入获取主键

service层代码:

        List<PlanDetail> planDetails = new ArrayList<>();
        for (int i=0;i<=500;i++){
            PlanDetail planDetail = new PlanDetail();
            planDetail.setCreditId(1);
            planDetails.add(planDetail);
        }
        planDetailMapper.batchInsert(planDetails);

mapper接口:

   /**
     * 批量插入
     * @param planDetails
     */
    void batchInsert(@Param("list") List<PlanDetail> planDetails);  //@Param注解里的值一定为'list';

实体类:PlanDetail

public class PlanDetail {
    private Integer id;

    private Integer creditId;
    
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getCreditId() {
        return creditId;
    }

    public void setCreditId(Integer creditId) {
        this.creditId = creditId;
    }
}

xml代码:

<insert id="batchInsert" useGeneratedKeys="true" keyProperty="id"  keyColumn="id">
    insert into t_plan_detail
      (id, credit_id)
    values
    <foreach collection="list" item="planDetail" separator=",">
      (#{planDetail.id}, #{planDetail.creditId})
    </foreach>
  </insert>
注 : 简单记录一下, 有不足的地方, 欢迎各位朋友评论留言!!!



展开阅读全文

没有更多推荐了,返回首页