<!- Hibernate 批量插入 -->
public void batchAddSendCus(final List<ShpSendVouchDto> shpSendVouchs) throws Exception {
super.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session){
int rows = 0;
for(ShpSendVouchDto dto: shpSendVouchs)
{
session.save(dto);
rows++;
if ( rows % 10000 == 0 ) {
//flush a batch of inserts and release memory:
//将本批插入的对象立即写入数据库并释放内存
session.flush();
session.clear();
}
}
return new Integer(rows);
}
});
}
ShpSendVouchDto为要插入的对向
List<ShpSendVouchDto> shpSendVouchs插入的对向的集合
------------------------------------------------------------------------------
<!-JDBC批量插入-->
public void batchAddSendCus(final List<ShpSendVouchDto> shpSendVouchs) throws Exception {
if (shpSendVouchs != null) {
(数据库针对MySql)
String sql = " insert into SHPSENDVOUCH(vouchid,vouchmodelid,Cusid,shopid) values(?,?,?,?) ";
(数据库针对Oracle,SEQ_SHP_SENDVOUCH.NEXTVAL为ORACLE中的序列)
// String sql = " insert into shpsendvouch(id,vouchid,vouchmodelid,Cusid,shopid)values(SEQ_SHP_SENDVOUCH.NEXTVAL,?,?,?,?) ";
this.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
String vouchid = shpSendVouchs.get(i).getVouchid();
Long vouchmodelid = shpSendVouchs.get(i).getVouchmodelid();
Long cusid = shpSendVouchs.get(i).getCusid();
Long shopid = shpSendVouchs.get(i).getShopid();
Long difstatus = shpSendVouchs.get(i).getDifstatus();
String vouchpassword = shpSendVouchs.get(i).getVouchpassword();
Date gettime = shpSendVouchs.get(i).getGettime();
Long status = shpSendVouchs.get(i).getStatus();
ps.setString(1, vouchid);
ps.setLong(2, vouchmodelid);
ps.setLong(3, cusid);
ps.setLong(4, shopid);
}
public int getBatchSize() {
return shpSendVouchs.size();
}
});
}
}
ShpSendVouchDto为要插入的对向
List<ShpSendVouchDto> shpSendVouchs插入的对向的集合