JPA怎麼使用count和sum函數?最近在使用JPQL時遇到了一個問題,關於count函數和sum函數不知該如何取值,總算找到了一種解決方法,與你們分享:函數
具體代碼以下:spa
public long getObjectsCountByQueryString(String queryString) { 代理
//queryString = "select count(o) from MyModel as o";
long count = 0;
count = ((Long)getJpaTemplate().find(queryString).get(0)).longValue();
return count;
}
public long getObjectsSumByQueryString(String queryString) { 指針
//queryString = "select sum(o.price) from MyModel as o";
List list = getJpaTemplate().find(queryString);get
//nod32使用sum函數要注意這裏要先判斷是否爲null,在線代理否則會出空指針異常,count函數不用判斷null
if(list.get(0) == null){
return 0;
}
else{
return ((Long)list.get(0)).longValue();
}
}List