新聞中心
本文主要講述了在使用Hibernate時(shí),如何實(shí)現(xiàn)group by and sum and count。希望本文能教會(huì)你更多東西。

專注于為中小企業(yè)提供成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)卡若免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了近1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
Hibernate是JDBC的升級版,專用連接數(shù)據(jù)庫。它比JDBC簡單使用,不需要輸入很多的連接數(shù)據(jù)庫代碼。提取數(shù)據(jù)庫數(shù)據(jù)也不用循環(huán)提取。使用時(shí)的方法為:
1.新建一個(gè)Java普通項(xiàng)目
2.創(chuàng)建user library 加入三個(gè)地方的jar包:兩個(gè)hibernate 一個(gè)MYSQL驅(qū)動(dòng)
3.創(chuàng)建hibernate配置文件,hibernate.cfg.xml
4.建立實(shí)體類user
5.在hibernate文件中尋找eg至底部找出user.hbm.xml映射文件,copy到映射文件所在文件中
6.將映射文件user.hbm.xml部分加入到hibernate.cfg.xml中
7.創(chuàng)建數(shù)據(jù)庫,再利用hibernate將實(shí)體映射導(dǎo)入到數(shù)據(jù)庫中
下面是具體實(shí)現(xiàn)的代碼:
- //使用hibernate,實(shí)現(xiàn)group by and sum and count
- Session sess = this.getSession(false);
- List list = null;
- if (sess != null) {
- Criteria cri = sess.createCriteria(getModelClass());
- cri.add(Expression.allEq(props));
- // always count id
- ProjectionList projList = Projections.projectionList();
- projList.add(Projections.sum(sum));
- projList.add(Projections.groupProperty(group1));
- projList.add(Projections.groupProperty(group2));
- projList.add(Projections.count(count));
- cri.setProjection(projList);
- list = cri.list();
- }
- listlist = list == null ? new ArrayList() : list;
- return list;
- //使用hibernate,實(shí)現(xiàn)group by and sum and count
- List listByGroupSum = dao.getListByGroupSumCP(props);
- Iterator iter = listByGroupSum.iterator();
- if (!iter.hasNext()) {
- System.out.println("No objects to display.");
- }
- while (iter.hasNext()) {
- System.out.println("New object");
- Object[] obj = (Object[]) iter.next();
- for (int i = 0; i < obj.length; i++) {
- System.out.println(obj[i]);
- }
- }
當(dāng)前題目:使用Hibernate代碼實(shí)例
鏈接分享:http://www.5511xx.com/article/dphpggp.html


咨詢
建站咨詢
