榪囨護鏌ヨ緇撴灉涓殑閲嶅鍏冪礌聽 聽聽聽聽聽浣跨敤Set闆嗗悎鏉ュ幓闄ら噸澶嶅厓绱狅紱鎴栨槸浣跨敤distinct鍏冪礌 聽聽聽聽聽Iterator聽iterator聽=聽session.createQuery("select聽distinct聽c.name聽from聽customer聽").list().iterator(); 聽聽聽聽聽while(iterator.hasnext()){ 聽聽聽聽聽聽聽聽聽聽聽String聽name聽=聽(String)聽it.next()聽; 聽聽聽聽聽} ///////////浣跨敤鑱氶泦鍑芥暟 count();聽璁板綍鐨勬潯鏁?br />min();聽聽聽姹傛渶灝忓?br />max();聽聽聽姹傛渶澶у?br />avg();聽聽聽姹傚鉤鍧囧?br />sum();聽聽聽姹傚拰 1'鏌ヨcustomer涓殑鎵鏈夎褰曟潯鏁?br />聽聽聽integer聽i聽=聽(Integer)聽session.createQuery("select聽count(*)聽from聽customer").uniqueResult(); 鍙湁鍦ㄧ‘瀹氬彧鏈変竴涓璞℃椂鍊欎嬌鐢╱niqueResult(); 2'鏌ヨcustomer涓殑鎵鏈夊鎴風殑騫沖潎騫撮緞 聽聽聽integer聽i聽=聽(Integer)聽session.createQuery("select聽avg(c.age)聽from聽customer聽c聽").uniqueResult(); 3'鏌ヨcustomer涓殑瀹㈡埛騫撮緞鐨勬渶澶у箋佹渶灝忓?br />涓涓暟緇勫彲浠ョ湅浣滀竴涓璞?br />聽聽聽object聽[]聽i聽=聽(Integer)聽session.createQuery("select聽max(c.age),min(c.age)聽from聽customer聽c聽").uniqueResult(); 聽聽聽Integer聽Maxage聽=聽(Integer)聽i聽[0]; 聽聽聽Integer聽Minage聽=聽(Integer)聽i聽[1]; 4'緇熻customer涓殑瀹㈡埛鐨勫悕縐版暟鐩紝蹇界暐閲嶅鐨勫鍚?br />聽聽聽Integer聽cout聽=聽(Integer)聽session.createQuery("select聽count(distinct聽c.name)聽from聽customer聽c").uniqueResult(); ////////////浣跨敤鍒嗙粍鏌ヨ 1'鎸夊鍚嶅垎緇勶紝緇熻customer涓殑鐩稿悓濮撳悕鐨勮褰曟暟鐩?br />聽聽聽Iterator聽iterator聽=聽(Integer)聽session.createQuery("select聽c.name聽,count(c)聽from聽customer聽c聽group聽by聽c.name").list.iterator(); 聽聽聽while(iterator.hasnext()){ 聽聽聽聽聽聽聽聽聽object[]聽p聽=聽(objcet[])iterator.next(); 聽聽聽聽聽聽聽聽聽String聽name聽=聽p[0]; 聽聽聽聽聽聽聽聽聽Integer聽cout聽=聽p[1]; 聽聽聽} 2'鎸夊鎴峰垎緇勶紝緇熻姣忎釜瀹㈡埛鐨勮鍗曟暟閲?br />聽聽聽Iterator聽iterator聽=聽session.crateQuery("select聽c.id聽,c.name聽,聽count(o)聽from聽customer聽c聽join聽c.order聽o聽group聽by聽c.id聽").list().iterator; 聽聽聽while(iterator.hasnext()){ 聽聽聽聽聽聽聽聽聽object[]聽p聽=聽(objcet[])iterator.next(); 聽聽聽聽聽聽聽聽聽Integer聽id聽=聽p[0] 聽聽聽聽聽聽聽聽聽String聽name聽=聽p[1]; 聽聽聽聽聽聽聽聽聽Integer聽cout聽=聽p[2]; 聽聽聽} 3'緇熻姣忎釜瀹㈡埛鐨勮鍗曟諱環 聽聽聽Iterator聽iterator聽=聽session.crateQuery("select聽c.id聽,c.name,sum(o.price)聽from聽customer聽c聽join聽c.order聽o聽group聽by聽c.id").list.iterator(); 聽聽聽while(iterator.hasnext()){ 聽聽聽聽聽聽聽聽聽object[]聽p聽=聽(objcet[])iterator.next(); 聽聽聽聽聽聽聽聽聽Integer聽id聽=聽p[0] 聽聽聽聽聽聽聽聽聽String聽name聽=聽p[1]; 聽聽聽聽聽聽聽聽聽Double聽cout聽=聽p[2]; 聽聽聽} //////////鎶ヨ〃鐨勪紭鍖?br />浣跨敤HQL鏃跺鏋滃彧鏌ヨ瀵硅薄鐨勪竴閮ㄥ垎灞炴т笉浼氳繑鍥炴寔涔呭寲瀵硅薄 from聽customer聽c聽join聽c.order聽o聽group聽by聽c.age;//榪斿洖鎸佷箙鍖栧璞★紝鍗犵敤緙撳瓨銆?br /> select聽c.id聽,聽c.name聽,聽c.age聽,o.id聽,o.price聽from聽customer聽c聽join聽c.order聽o聽group聽by聽c.age聽//榪斿洖鍏崇郴鎬ф暟鎹紝涓嶅崰鐢╯ession鐨勭紦瀛橈紝鍙互涓篔VM鍥炴敹 /////////HQL瀛愭煡璇㈢殑闆嗗悎鍑芥暟灞炴?br />size()聽榪斿洖闆嗗悎涓厓绱犵殑鏁扮洰 minIndex()聽寤虹珛绱㈠紩鐨勯泦鍚堣幏寰楁渶灝忕殑绱㈠紩 maxIndex()聽寤虹珛绱㈠紩鐨勯泦鍚堣幏寰楁渶澶х殑绱㈠紩 minElement()聽瀵逛簬鍖呭惈鍩烘湰鍏冪礌鐨勯泦鍚堣幏寰楅泦鍚堟渶灝忕殑鍏冪礌 maxElement()聽瀵逛簬鍖呭惈鍩烘湰鍏冪礌鐨勯泦鍚堣幏寰楅泦鍚堟渶澶х殑鍏冪礌 element()聽鑾峰緱闆嗗悎涓殑鎵鏈夊厓绱?br />///////////Hibernate鐨勭紦瀛樼鐞?br />綆$悊涓綰х紦瀛橈紝涓嶅緩璁嬌鐢ㄤ笅鍒楁柟娉曟潵綆$悊涓綰х紦瀛?br />evict(Object聽o)浠庣紦瀛樹腑娓呴櫎鍙傛暟鎸囧畾鐨勬寔涔呭寲瀵硅薄 clear()娓呯┖緙撳瓨涓墍鏈夌殑鎸佷箙鍖栧璞?br />///////////鎵歸噺鏇存柊鍜屾壒閲忓垹闄?br />鎵歸噺鏇存柊customer琛ㄤ腑鐨勫勾榫勫ぇ浜庨浂鐨勬墍鏈夎褰曠殑AGE瀛楁錛?br />聽聽聽聽tx聽=聽session.beginTransaction(); 聽聽聽聽Iterator聽customer聽=聽session.find("from聽customer聽c聽where聽c.age聽>聽0").iterator(); 聽聽聽聽while(iterator.hasnext()){ 聽聽聽聽聽聽聽聽customer聽customer聽=聽(customer)聽customer.next(); 聽聽聽聽聽聽聽聽customer.setAge(customer.getAge()+1); 聽聽聽聽} 聽聽聽聽tx.commit(); 聽聽聽聽session.close();//涓婅堪浠g爜瀵規暟鎹簱鎿嶄綔鏁堟灉涓嶅ソ鍙互浣跨敤涓嬪垪鐨凧DBCAPI鐩磋繛 聽聽聽 聽聽聽聽tx聽=聽session.beginTranscation(); 聽聽聽聽Connection聽conn聽=聽session.connection(); 聽聽聽聽Preparedstatement聽ps聽=聽conn.preparedstatment("update聽customer聽set聽age聽=聽age聽+聽1聽where聽age聽>聽0聽"); 聽聽聽聽ps.executeupdate(); 聽聽聽聽tx.commit(); |