jdbc 批量插入 和mybaits框架批量插入对比

来源:互联网 发布:软件公寓什么时候预约 编辑:程序博客网 时间:2024/05/19 10:41


    不写理由 直接上源码

    mybaits框架批量插入:


在xml 中写法

 <insert id="aa" parameterType="java.util.List">
  insert into table a
  (a.id)
  <foreach collection="list" item="item" index="index"
   separator="union all">
   select
   #{item.id,jdbcType=VARCHAR}
   from dual
  </foreach>
 </insert>



jdbc批量插入

public void insertcrjjkxxhx(List<CrjHxJkxxPojo> crjHxJkxxPojos) {
  int count = crjHxJkxxPojos.size();
  try {
   DataSource ds = (BasicDataSource) SpringContextUtil.getBean("dataSource");
   Connection conn = ds.getConnection();
   String sql = "insert into aa (id,ywbh,jyh,yhlsh,yhh,stfsj,stfbz,stffs,sldw,stfdw,stfje) values (?)
   conn.setAutoCommit(false);
   PreparedStatement pst = conn.prepareStatement(sql);
   for (int i = 0; i < count; i++) {
    CrjHxJkxxPojo crjHxJkxxPojo = crjHxJkxxPojos.get(i);
    pst.setString(1, crjHxJkxxPojo.getId());
    pst.addBatch();
    if (i % 1000 == 0 || i == count - 1) {
     pst.executeBatch();
     conn.commit();
    }
   }
   pst.close();
   conn.close();
  } catch (Exception e) {
   e.printStackTrace();
  }

 }


经对比,jdbc插入 快N倍