/**
* 批量插入
* @param answers
*/
public boolean batchSave(List<SurAnswers> answers){
System.out.println("批量插入数据条数:"+answers.size());
boolean flag = false;
Transaction tx = null;
try {
tx = getSession().beginTransaction();
int i =0;
for(SurAnswers as:answers){
i++;
getSession().save(as);
if(i%10==0){
getSession().flush();
getSession().clear();
tx.commit();
tx = getSession().beginTransaction();
}
}
tx.commit();
flag = true;
} catch (Exception e) {
e.printStackTrace();
flag = false;
}finally{
closeSession();
}
return flag;
}
/**Hibernate批量插入
最新推荐文章于 2025-10-04 13:10:01 发布
本文介绍了如何使用Java实现批量数据插入操作,并通过事务管理确保数据一致性。详细解释了如何利用Session进行数据保存、flush、clear和事务提交,以优化数据库性能。

937

被折叠的 条评论
为什么被折叠?



