rt:
插入数据库的操作时调用的存储过程。不过可以改。现在只能传参数执行一次插一次。数据量大概不到10万。for循环10万次岂不是太土了,其中某此出错咋办。rollback?10万次出错的概率应该很大。那我岂不是每次都不能成功插入。不知道问题说清楚了没。求教了。
插入数据库的操作时调用的存储过程。不过可以改。现在只能传参数执行一次插一次。数据量大概不到10万。for循环10万次岂不是太土了,其中某此出错咋办。rollback?10万次出错的概率应该很大。那我岂不是每次都不能成功插入。不知道问题说清楚了没。求教了。
我需求没说清楚。这样的表有6张。字段都不一样。搞新表的话。内存岂不是浪费很多。
6张目标表都是有数据的。
或者存储过程里直接使用insert into oldtable select * from new newtable不行吗?
就算你有条件约束,那就直接在后面跟where子句好了。
一条一条导入,导到何年何月去了
<!- 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插入的对向的集合