批量插入 jdbc及hibernate方法

public void insertUndoRecordsIntoReport(final List<Object[]> recordsNeeded, final int monitoring_report_id) {
		final String sql =" insert into record (case_id, pass, level_id, info, project_id, module_id, monitoring_id, alternative, monitoring_report_id) " +
						  " values ( ?, ?, ?, ?, ?, ?, ?, ?, ?) ";

		this.getHibernateTemplate().execute(
		new HibernateCallback() {
		
			public Object doInHibernate(Session session) throws HibernateException,
			SQLException {
		
				java.sql.Connection con = session.connection();
				java.sql.PreparedStatement stmt = con.prepareStatement(sql);
				for (Object[] recordNeeded:recordsNeeded) {
					
					stmt.setString(1,recordNeeded[1].toString()); 
				    stmt.setLong(2, 0);
				    stmt.setLong(3, 4);
				    stmt.setString(4, "用例未执行");
				    stmt.setString(5, recordNeeded[4].toString());
				    stmt.setString(6, recordNeeded[3].toString());
				    stmt.setString(7, recordNeeded[2].toString());
				    stmt.setString(8, recordNeeded[0].toString());
				    stmt.setLong(9, monitoring_report_id);
				    stmt.addBatch();//将插入的任务添加到批处理中
				}
				stmt.executeBatch();//执行批量任务 
				stmt.close();
				session.flush();    
				session.close();
				
				return null; 
				
			}
		}
		);
	}



public void batchSaveCardDetail(List<TCardInfo> cardDetails) {  
        return this.getHibernateTemplate().execute(new HibernateCallback() {  
            @Override  
            public Object doInHibernate(Session session) throws HibernateException, SQLException {                  
        for (int i = 0; i < cardDetails.size(); i++) {  
                    session.save(cardDetails.get(i));         
                    if (i % 50 == 0) {  
                        session.flush();  
                        session.clear();  
                    }  
                }                  
                return null;  
            }  
        });  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值