setProjection(Projection projection)
引數說明:Criteria criteria = session.createCriteria(UserForm.class); //範例化Criteria物件 criteria.setProjection(Projections.id()); //設定查詢列 list = criteria.list(); //執行查詢 Iterator it = list.iterator(); //獲取查詢結果疊代器 while(it.hasNext()){ //迴圈遍歷查詢結果 System.out.println(it.next()); }
Criteria criteria = session.createCriteria(UserForm.class); //範例化Criteria物件 criteria.setProjection(Projections.projectionList() .add(Projections.id()) .add(Projections.property("username")) //查詢資料表全部使用者ID和使用者名稱 ); List list = criteria.list(); //獲取查詢結果 Iterator it = list.iterator(); //獲取查詢結果疊代器 while(it.hasNext()){ //迴圈遍歷疊代器 Object[] o = (Object[])it.next(); System.out.println("ID:"+o[0]+"使用者名稱:"+o[1]); }
Criteria criteria = session.createCriteria(OrderForm.class); //獲取Criteria範例 criteria.setProjection(Projections.sum("money")); //設定查詢列 double sumMoney = (Double)criteria.uniqueResult(); //獲取訂單資訊表總金額 System.out.println("合計金額:"+sumMoney);