批量更新,插入方法
http://www.iteye.com/news/19094 jquery
public boolean doSaveNinfoshowcontrol(final List<NinfoShowControlDto> list)throws ApplicationException
{
try {
getHibernateTemplate().execute(new HibernateCallback(){
@Override
public Object doInHibernate(Session sess)
throws HibernateException, SQLException {
int rows=0;
for (NinfoShowControlDto ninfoShowControlDto : list) {
sess.save(ninfoShowControlDto);
rows++;
if(rows%50==0)
{
sess.flush();
sess.clear();
}
}
return new Integer(rows);
}
});
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean editStatusBatch(final ArrayList list,Integer status) throws ApplicationException {
try {
String sql = "";
if(status != null && status == 0){
sql = "UPDATE PUBHOTSHOW SET STATUS = 1 WHERE ID = ?";
}else {
sql = "UPDATE PUBHOTSHOW SET STATUS = 0 WHERE ID = ?";
}
BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i)
throws SQLException {
ps.setLong(1,(Integer)list.get(i) );
}
public int getBatchSize() {
return list.size();
}
};
this.getJdbcTemplate().batchUpdate(sql, setter);
return true;
} catch (Exception e) {
e.printStackTrace();
ExceptionCause ec = new ExceptionCause();
ec.setMessageKey("修改状态批处理异常!");
throw new ApplicationException(ec);
}
}
@Override
public boolean saveOrUpdateBatchData(List<AdminPubHotShowDto> list)
throws ApplicationException {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
try {
// 当list不为空的
if(list != null && list.size()>0){
Session session = this.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
for(int i = 0; i< list.size(); i++){
AdminPubHotShowDto dto = list.get(i);
if(dto.getId() != null){
session.update(dto);
}else {
session.save(dto);
}
if ( i != 0 && i % 20 == 0 ) {
session.flush();
session.clear();
}
}
tx.commit();
if(session.isOpen()){
session.close();
}
}
return true;
} catch (java.lang.Throwable t) {
t.printStackTrace();
ExceptionCause ec = new ExceptionCause();
ec.setMessageKey("批量添加或修改资源数据异常!");
throw new ApplicationException(ec);
}
}