hibernate批量更新和批量删除的方法有很多。下面举例:
方法一:hql方法。
- /**
- * 更新内容数据,本次更新最新标志.条件是超过7天的数据
- * @return 更新的条数
- */
- public int updateContent() {
- Object[] params = {0, 1, new Date()};
- StringBuilder updateBuffer = new StringBuilder();
- updateBuffer.append("UPDATE TblContent SET ");
- updateBuffer.append("CIsElink=?");
- updateBuffer.append(",screenDate=?");
- updateBuffer.append("WHERE CIsElink=?");
- Type[]types = new Type[3];
- types[0] = Hibernate.INTEGER;
- types[2] = Hibernate.INTEGER;
- types[1] = Hibernate.TIMESTAMP;
- try {
- Query query = getSession().createQuery(updateBuffer.toString());
- query.setParameters(params, types);
- int count = query.executeUpdate();
- //log.info(count+" has been batchUpdated successful!");
- return count;
- //this.commit();
- } catch (HibernateException e) {
- //this.rollback();
- //log.error("batchUpdate failed", e);
- throw e;
- }
- }
方法二:jdbc api方法(预处理语句)。
- public void updateContact(String oldUserId, String newUserId)
- throws HibernateException, SQLException {
- Session session = getSession();
- Transaction transcation = session.beginTransaction();
- Connection con = session.connection();
- StringBuffer sql = new StringBuffer("update TblContent set userId='")
- .append(newUserId).append("'").append(" where userId='")
- .append(oldUserId).append("'");
- PreparedStatement stmt = con.prepareStatement(sql.toString());
- stmt.executeUpdate();
- transcation.commit();
- session.close();
- }
posted on 2017-07-10 14:38 alex5211314 阅读(386) 评论(0) 收藏 举报
浙公网安备 33010602011771号