为了使用findByCriteria(detachedCriteria),首先写个接口:
protected RetCode findByCriteria(HibernateDaoSupport dao,DetachedCriteria detachedCriteria) {
RetCode ret = new RetCode();
List results = dao.getHibernateTemplate()
.findByCriteria(detachedCriteria);
if (results == null || results.size() == 0) {
ret.setCode(1001);
ret.setDesc("无查询数据!");
ret.setDetail("未找到查询的相应数据");
ret.setObj(null);
} else {
ret.setCode(0);
ret.setDesc("操作成功!");
ret.setObj(results);
}
return ret;
}
然后便可以利用这个接口了:
/**
* 查出所有处于生效状态的活动
* @param dbIcTask
* @param currentPage
* @param pageSize
* @return
*/
public RetCode findAll() {
return this.findByCriteria(dbIcTaskDAO, getAllUsefuleTaskCriteria());
}
最后,便是在getAllUsefuleTaskCriteria()这个方法里面写上SQL语句的条件:
private DetachedCriteria getAllUsefuleTaskCriteria() {
DetachedCriteria crt = DetachedCriteria.forClass(DbIcTask.class);
crt.add(Restrictions.sqlRestriction("state='1'"));
crt.add(Restrictions.sqlRestriction("sysdate <= to_date(endtime,'yyyy-mm-dd hh24:mi:ss')"));
crt.add(Restrictions.sqlRestriction("sysdate >= to_date(begintime,'yyyy-mm-dd hh24:mi:ss')"));
return crt;
}