springboot jap自定義原生sql 接收SELECT count(*) 的返回long型別結果

萬事功到自然成發表於2020-12-06
Long total = null;
Object singleResult=null;
try {
   String sqlStr = "SELECT count(*) from order_product op, `order` o WHERE  op.PRODUCT_UUID =:PRODUCTUUID and o.ORDER_UUID = op.ORDER_UUID and ( (:STARTINGTIME between o.STARTING_TIME and o.ENDING_TIME ) or ( :ENDINGTIME between o.STARTING_TIME and o.ENDING_TIME ) ) and o.IS_DELETED=:isDeleted ";
    singleResult = em.createNativeQuery(sqlStr)
         .setParameter("STARTINGTIME", startingTime)
         .setParameter("ENDINGTIME", endingTime)
         .setParameter("PRODUCTUUID", productUUid)
         .setParameter("isDeleted", Boolean.FALSE)
         .getSingleResult();
      total=Long.parseLong(singleResult.toString());

} catch (NoResultException e) {
   logger.info("No record found");
}
return total == null ? 0 : total.intValue();

相關文章