公司使用
[java]
DetachedCriteria detachedCriteria = DetachedCriteria
detachedCriteria
detachedCriteria
detachedCriteria
detachedCriteria
detachedCriteria
detachedCriteria
DetachedCriteria detachedCriteria = DetachedCriteria
detachedCriteria
detachedCriteria
detachedCriteria
detachedCriteria
detachedCriteria
detachedCriteria
這種方式的最大好處是幾乎完全面向對象
但是他有一個最大的弊端就是效率問題
果然QBC提供了字段的部分查詢
言歸正傳
Hibernate 使用查詢部分/指定字段
第一種是使用高級查詢DetachedCriteria實現
[java]
String alias =
DetachedCriteria dc = DetachedCriteria
ProjectionList pList = Projections
pList
pList
pList
pList
dc
dc
resultList = memberService
String alias =
DetachedCriteria dc = DetachedCriteria
ProjectionList pList = Projections
pList
pList
pList
pList
dc
dc
resultList = memberService
第二種方式是通過HQL語句new POJO()實現
[java]
package com
public class Link {
private String id;
private String name;
private String url;
private Integer index;
public Link(){}
//因為
//所以必須要有接受
public Link(String id
this
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this
}
}
package com
public class Link {
private String id;
private String name;
private String url;
private Integer index;
public Link(){}
//因為
//所以必須要有接受
public Link(String id
this
this
}
public String getName() {
return name;
}
public void setName(String name) {
this
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this
}
}
通過HQL語句查詢
[java]
String hql =
Query query = session
//默認查詢出來的list裡存放的是一個Object對象
List<Link> links = query
for(Link link : links){
String id = link
String name = link
System
}
String hql =
Query query = session
//默認查詢出來的list裡存放的是一個Object對象
List<Link> links = query
for(Link link : links){
String id = link
String name = link
System
}
第三種方式是通過HQL語句實現
[java]
String hql =
Query query = session
//默認查詢出來的list裡存放的是一個Object數組
List<Object[]> links = query
for(Object[] link : links){
String id = link[
String name = link[
System
}
String hql =
Query query = session
//默認查詢出來的list裡存放的是一個Object數組
List<Object[]> links = query
for(Object[] link : links){
String id = link[
String name = link[
System
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28800.html