批量保存,批量插入数据

/**
* 批量保存
*/
public boolean save(List<T> list) {
Session session = HibernateUtils.getSession();
Transaction transaction = session.beginTransaction();
try {
for (int i = 0; i < list.size(); i++) {
session.save(list.get(i));
if (i % 2000 == 0 || i == list.size() - 1) {
session.flush();
transaction.commit();
}
}
} catch (HibernateException e) {
e.printStackTrace();
transaction.rollback();
return false;
} finally {
HibernateUtils.closeSession(session);
}
return true;
}

你可能感兴趣的:(批量保存,批量插入数据)