准备一个arrayList 把你要更新的实体要有ID值,放进去
然后用getHibernateTemplate().saveOrUpdateAll(arrayList)
把集合放进去执行一下就OK了.
是用循环,最后commit,但是要注意不能一次过循环save10000条数据再commit,因为这样会使hibernate的一级缓存爆满导致内存溢出的异常,所以每隔100条记录就commit一次吧,hibernate的二级缓存会决定该什么时候提交以实现批量插入的高效率的。
try {
tx = session.begintransaction();
for(int i=0;i<=10000;i++) {="" userinfo="" u="new" userinfo();="" u.setusername("fujingzhou");="" u.setpassword("123");="" session.save(u);="" if(i%100||i="=10000)" {="" tx.commit();="">=10000;i++)><10000) tx="session.begintransaction();" }="" }="" tx="null;" }="">10000)>
我只有没用hibernate的一种批处理方法,
希望会对你有点帮助:
public void updateByRoleid(int roleid,String[] userids) {
String delSql = "delete from users_roles where roleid=?";
Connection con = DBOconnection.getConnection();
PreparedStatement psmt = null;
try {
psmt = con.prepareStatement(delSql);
psmt.setInt(1, roleid);
psmt.executeUpdate();
/**
* 运用数据库的批处理,提高数据库执行效率
*/
con.setAutoCommit(false);//设置不自动执行
String insertSql = "insert into users_roles (userid,roleid)values(?,?)";
psmt = con.prepareStatement(insertSql);
if (userids != null) {
for (String userid:userids) {
int id = Integer.valueOf(userid);
psmt.setInt(1, id);
psmt.setInt(2, roleid);
psmt.addBatch();//加入批处理
}
psmt.executeBatch();//执行所有
con.commit();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBUtil.close(psmt);
DBUtil.close(con);
}
}
热门文章更多>>
标签更多>>
专题更多>>
最新文章更多>>
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
- 使用 C# 6.0 功能运行 TFS 构建
姐就是姐_别侮辱姐