日期:2014-05-19  浏览次数:20882 次

批删除
Action获取了jsp传过来的一组checkbox,checkbox的值是id,可以用hibernate中的deleteall()根据id进行批删除吗?

------解决方案--------------------
/**
* 删除多条 记录
*
* @param entityClass
* @param att
* @param val
*/
public void attrDeletes(Class<T> entityClass, Object att, Object... val) {
StringBuilder sb = new StringBuilder();
sb.append("DELETE FROM ");
sb.append(entityClass.getSimpleName());
sb.append(" WHERE ");
sb.append(att.toString());
sb.append(" in (:val)");
// String hql = "DELETE FROM Article WHERE aid in (:ids)";
Query q = this.getSession().createQuery(sb.toString());
sb.delete(0, sb.length());
q.setParameterList("val", val).executeUpdate();
}

/**
* 批量删除 ? 待续
*
* @param list
*/
public void deleteBulk(List<T> list) {
int i = 1000;
for (T e : list) {
i++;
this.deleteObject(e);
if (i % 20 == 0) {
// 将本批数据插入数据库,并释放内存
this.flush();
this.clear();
}
if (i % 1000 == 0) {
try {
this.commit();
} catch (HibernateException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}

}

/**
*
*/
public void deletes(Class<T> entityClass, String idArr) {
String[] ids = idArr.split(",");
Long[] ls = new Long[ids.length];
String hql = "DELETE FROM " + entityClass.getSimpleName();
hql += " WHERE "
+ HibernateConfigurationHelper.getPrimaryKeyName(entityClass);
hql += " in (:ids)";
Query q = getSession().createQuery(hql);
for (int i = 0; i < ids.length; i++) {
logger.debug(ids[i]);
ls[i] = Long.parseLong(ids[i]);
}

都是的,随便用
------解决方案--------------------
批删除很简单,你用ck获取的值,比如说ID,结果为params=1,3,5,6,8
然后你在删除方法sql=delete text where id in (params);
这样就好了