本文共 2547 字,大约阅读时间需要 8 分钟。
goods.xml文件
INSERT INTO t_goods(title, sub_title, original_cost, current_price, discount, is_free_delivery, category_Id) VALUES (#{item.title},#{item.subTitle}, #{item.originalCost}, #{item.currentPrice}, #{item.discount}, #{item.isFreeDelivery}, #{item.categoryId})
测试
@Test /* 批量插入测试 */ public void testBatchInsert() throws Exception { SqlSession session = null; try{ session = MyBatisUtils.openSession(); long st = new Date().getTime(); Listlist = new ArrayList (); for( int i = 0; i <10; i++){ Goods goods = new Goods(); goods.setTitle("测试商品"); goods.setSubTitle("测试子标题"); goods.setOriginalCost(200f); goods.setCurrentPrice(100f); goods.setDiscount(0.5f); goods.setIsFreeDelivery(1); goods.setCategoryId(43); //insert()方法返回值代表本次成功插入的记录总数 list.add(goods); } int num = session.insert("goods.batchInsert", list); System.out.println(num); session.commit(); Long bt = new Date().getTime(); System.out.println("执行时间" + (bt - st) + "毫秒"); }catch(Exception e){ throw e; }finally{ MyBatisUtils.closeSession(session); } }
这样批量插入时有一定的缺陷
goods.xml文件
DELETE FROM t_goods WHERE goods_id in #{item}
测试
@Test /* 批量插入测试 */ public void testBatchDelete() throws Exception { SqlSession session = null; try{ session = MyBatisUtils.openSession(); long st = new Date().getTime(); Listlist = new ArrayList (); list.add(15); list.add(16); list.add(17); list.add(18); list.add(19); list.add(20); int num = session.delete("goods.batchDelete", list); System.out.println(num); session.commit(); Long bt = new Date().getTime(); System.out.println("执行时间" + (bt - st) + "毫秒"); }catch(Exception e){ if(session != null){ session.rollback(); } throw e; }finally{ MyBatisUtils.closeSession(session); } }
转载地址:http://ewguk.baihongyu.com/