<!--批量插入-->
<select id="batchInsert" parameterType="java.util.List">
insert into t_goods(title,sub_title,original_cost,current_price,discount,is_free_delivery,category_id)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.title},#{item.subTitle},#{item.originalCost},#{item.currentPrice},#{item.discount},
#{item.isFreeDelivery},#{item.categoryId})
</foreach>
</select>
public void TestBatchInsert() throws Exception {
SqlSession sqlSession = null;
try {
//获取sql对象
sqlSession = MybatisUtils.openSession();
//记录开始时间
Long st = new Date().getTime();
List<GoodsEntity> list = new ArrayList();
for (int i = 0; i <30000 ; i++) {
GoodsEntity goodsEntity = new GoodsEntity();
goodsEntity.setTitle("测试商品");
goodsEntity.setSubTitle("测试商品子标题");
goodsEntity.setOriginalCost(1000f);
goodsEntity.setCurrentPrice(800f);
goodsEntity.setDiscount(0.8f);
goodsEntity.setIsFreeDelivery(1);
goodsEntity.setCategoryId(43);
list.add(goodsEntity);
}
//执行sql
sqlSession.insert("goods.batchInsert",list);
//提交数据
sqlSession.commit();
//记录结束时间
Long et = new Date().getTime();
System.out.println("总花费时间:"+(et-st)+"--");
//查看连接状态
Connection conn = MybatisUtils.getConnection(sqlSession);
}catch (Exception e){
sqlSession.rollback();//数据回滚
throw e;
}finally {
MybatisUtils.release(sqlSession);
}
}