Hibernate4增加了执行基本sql的功能
这里用Hibernate原生sql执行了一个批量处理。
boolean result = true;
try {
getSession().doWork(new Work(){
public void execute(Connection conn) throws SQLException {
String sql = "delete from t_user_users where id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
for(int i = 0;i<ids.length;i++){
pstmt.setInt(1, Integer.parseInt(ids[i].trim()));
pstmt.addBatch();
}
//执行批量操作
pstmt.executeBatch();
}
});
} catch (Exception e) {
result = false;
e.printStackTrace();
logger.error(e.getMessage(), e);
} finally{
return result;
}
本文介绍如何使用Hibernate4执行原生SQL进行批量删除操作。通过示例代码展示了如何利用Hibernate的doWork方法配合PreparedStatement实现对数据库表t_user_users中特定记录的批量删除。
368

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



