一、前言
这个东西,难度几乎没有,就是繁琐。
一条简单的select count(*) from table_name
都能有多种书写方式。
总是忘,这里记录下。
一 、通过Criteria 查询
Criteria criteriaCount = getCriteria(); criteriaCount = criteriaCount.add(Restrictions.eq("dispatchTaskId", dispatchTaskId)); criteriaCount.setProjection(Projections.rowCount()); Integer totalCount = ((Long) criteriaCount.uniqueResult()).intValue();
二、通过原生sql查询
SQLQuery queryCount = getSession().createSQLQuery("SELECT COUNT(*) FROM incidentInformation WHERE ii.incidentInformationId = :incidentInformationId AND dti.taskstate = :taskstate"); queryCount.setParameter("incidentInformationId", incidentInformationId); queryCount.setParameter("taskstate", ETaskStatus.STATUS_INIT.getStatusValue()); int count = ((BigDecimal) queryCount.uniqueResult()).intValue(); return count;
三、通过hibernate的查询语言查询
String countHql = "select count(*) from a where and a.approveResult = :approveResult and a.approverId = :approverId"; Query countQuery = getSession().createQuery(countHql); countQuery.setParameter("approverId", approverId); int count = ((Long) countQuery.uniqueResult()).intValue();
原文地址:https://www.cnblogs.com/grey-wolf/p/10209946.html
时间: 2024-11-08 20:27:52