HQL語句
hibernate配備了一種非常強大的查詢語言,這種語言看上去很像SQL。但是不要被語法結構 上的相似所迷惑,HQL是非常有意識的被設計為完全面向對象的查詢,它可以理解如繼承、多態 和關聯之類的概念。
HQL: Hibernate Query Language. 映射配置的持久化類以及其屬性。是一種面向對象的查詢語言。
SQL:數據庫表。主題是表,對大小寫不敏感。
HQL語句形式:
Select … from …. Where … group by… having… order by…
serlect..對象中的屬性 from該對象 where
特點:
1,與SQL相似,SQL中的語法基本上都可以直接使用。
2,SQL查詢的是表和表中的列;HQL查詢的是對象與對象中的屬性。
3,HQL的關鍵字不區分大小寫,java類名與屬性名是區分大小寫的。
4,SELECT可以省略.
Org.hibernate.Query接口
1. Query接口有執行查詢方法
2. Query接口支持方法鏈編程,使得程序代碼方便簡潔。執行完畢以后可以調用別的方法。
Query實例創建
1. 通過session的createQuery()方法創建Query實例。
2. createQuery方法包含一個HQL語句參數,createQuery(hql)。就是要執行的查詢語句。
3. 執行查詢。
Query查詢
1. Query接口的list()方法執行查詢。
2. List方法返回的結果數據類型為java.util.List,List中存放符合查詢條件的持久化對象。
實體類代碼:
/*
* 不需要更改
* 屬性
* newsid newstitle author
* content pubtime newspic
* newsTypebean 關聯對象
* */
public classNewsBean {
@Override
public String toString() {
/* return"NewsBean [newsid=" +newsid + ", newstitle="+ newstitle
+", author=" + author + ", content=" + content + ",pubtime="
+pubtime + ", typeid=" +typeid + ", newspic="+ newspic + "]";*/
return "NewsBean [newsid="+ newsid+ ", newstitle=" + newstitle
+", author=" + author+ ", content=" + content+ ", pubtime="
+pubtime+ ", newspic=" + newspic+ "]";
}
PRivate int newsid;
private Stringnewstitle;
private Stringauthor;
private Stringcontent; //存儲的是文本的路徑
private Stringpubtime; //默認格式為'0000-00-00 00:00'
// privateint typeid;
private Stringnewspic; //存儲的是圖片的路徑
private NewstypeBeannewsTypebean;
public NewstypeBeangetNewsTypebean() {
return newsTypebean;
}
public voidsetNewsTypebean(NewstypeBean newsTypebean) {
this.newsTypebean = newsTypebean;
}
public int getNewsid() {
return newsid;
}
public void setNewsid(int newsid) {
this.newsid = newsid;
}
public String getNewstitle() {
return newstitle;
}
public void setNewstitle(Stringnewstitle) {
this.newstitle = newstitle;
}
public String getAuthor() {
return author;
}
public void setAuthor(Stringauthor) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(Stringcontent) {
this.content = content;
}
public String getPubtime() {
return pubtime;
}
public void setPubtime(Stringpubtime) {
this.pubtime = pubtime;
}
/* publicint getTypeid() {
returntypeid;
}
publicvoid setTypeid(int typeid) {
this.typeid= typeid;
}*/
public String getNewspic() {
return newspic;
}
public void setNewspic(Stringnewspic) {
this.newspic = newspic;
}
public NewsBean() {
super();
}
public NewsBean(Stringnewstitle,String newspic,
int newsid,String pubtime ){
super();
this.newstitle = newstitle;
this.newspic = newspic;
this.newsid = newsid;
this.pubtime = pubtime;
}
//構造方法
public NewsBean(int newsid, Stringnewstitle, String author,
Stringcontent, String pubtime, String newspic){
super();
this.newsid = newsid;
this.newstitle = newstitle;
this.author = author;
this.content = content;
this.pubtime = pubtime;
// this.typeid = typeid;
this.newspic = newspic;
}
//構造方法
public NewsBean(Stringnewstitle, String author, String content,
Stringpubtime, String newspic) {
super();
this.newstitle = newstitle;
this.author = author;
this.content = content;
this.pubtime = pubtime;
// this.typeid = typeid;
this.newspic = newspic;
}
}
測試類代碼:
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import entity.NewsBean;
import util.HibernateSessionFactory;
publicclassNewsBeanTest {
private Sessionsession=null;//創建session,org.hibernate.Session
//創建一個test方法
@Test
publicvoid testnewsbean(){
//編寫執行查詢的語句
Stringsql="from NewsBean";
//創建query實例對象
Queryquery=session.createQuery(sql);//import org.hibernate.Query
//query.list();//查詢結果list集合,符合條件的實例對象。
//接受返回的結果,import java.util.List
List<NewsBean>news=query.list();
//測試,在控制臺打印測試
for(NewsBeannewsBean:news){
System.out.println(newsBean);//
}
}
@Before
publicvoid setUp() throws Exception {
//獲得session
session=HibernateSessionFactory.getSession();
}
@After
publicvoid tearDown() throws Exception {
//使用完畢后要關閉session
session.close();
}
}
運行結果:
HQL:需要from語句
SQL:需要select和from語句
(1)
(2)
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import entity.NewsBean;
import entity.NewstypeBean;
import util.HibernateSessionFactory;
public classNewsBeanTest {
//獲取關聯對象的信息,如果不打印關聯對象的信息,則不會查詢兩次,只查詢一次。就是只查詢新聞
}
結果:成功地找出,找了兩次
關于全限定名。
1.
2.
全限定名:from com.imooc.model.Seller
直接使用類名就可以了:from Seller,方便快捷。常用
From子句中別名的使用
1.
2.
3.
如:from Seller
(1)//別名不對查詢結果有任何的變化
String sql="from News as newsbean ";
可以去掉as等價于
String sql=”from News
(2)多個持久化類可以用逗號隔開
String sql="from News as newsbean ,Type as type";
可以去掉as等價于
String sql=”from News
Select 子句關于返回Object數組和單個對象整理:
代碼:
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import entity.NewsBean;
import util.HibernateSessionFactory;
public class TestNewsObject {
}
運行結果:
(2)單個字段的時候,就不是object數據,而是一個對象。
@Test
//查詢一個的時候,返回的是對象類型,而不是對象數組,
Select
注明:別名對后期排查有用,養成習慣。
List形式返回
1,
以Map形式返回
1.
2.
//使用map方法
新聞熱點
疑難解答