国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

hibernate項(xiàng)目的HQL(SQL區(qū)別、Select語句、返回list、map、object數(shù)組與單個對象)筆記整理

2019-11-10 18:40:27
字體:
供稿:網(wǎng)友

HQL語句

hibernate配備了一種非常強(qiáng)大的查詢語言,這種語言看上去很像SQL。但是不要被語法結(jié)構(gòu) 上的相似所迷惑,HQL是非常有意識的被設(shè)計(jì)為完全面向?qū)ο蟮牟樵儯梢岳斫馊缋^承、多態(tài) 和關(guān)聯(lián)之類的概念。

HQL: Hibernate Query Language.  映射配置的持久化類以及其屬性。是一種面向?qū)ο蟮牟樵冋Z言。

SQL:數(shù)據(jù)庫表。主題是表,對大小寫不敏感。

 

HQL語句形式:

Select   … from  ….   Where … group by…  having… order by…

serlect..對象中的屬性 from該對象 where

 

特點(diǎn):  

 1,與SQL相似,SQL中的語法基本上都可以直接使用。  

 2,SQL查詢的是表和表中的列;HQL查詢的是對象與對象中的屬性。  

 3,HQL的關(guān)鍵字不區(qū)分大小寫,java類名與屬性名是區(qū)分大小寫的。  

 4,SELECT可以省略.  

 

Org.hibernate.Query接口

1. Query接口有執(zhí)行查詢方法

2.      Query接口支持方法鏈編程,使得程序代碼方便簡潔。執(zhí)行完畢以后可以調(diào)用別的方法。

 

 

Query實(shí)例創(chuàng)建

1. 通過session的createQuery()方法創(chuàng)建Query實(shí)例。

2.      createQuery方法包含一個HQL語句參數(shù),createQuery(hql)。就是要執(zhí)行的查詢語句。

3.      執(zhí)行查詢。

 

Query查詢

1.  Query接口的list()方法執(zhí)行查詢。

2.  List方法返回的結(jié)果數(shù)據(jù)類型為java.util.List,List中存放符合查詢條件的持久化對象。

實(shí)體類代碼:

/*

 * 不需要更改

 * 屬性

 * newsid  newstitle author

 * content pubtime  newspic

 * newsTypebean 關(guān)聯(lián)對象

 * */

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;   //默認(rèn)格式為'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;

  }

  //構(gòu)造方法

  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;

  }

  //構(gòu)造方法

  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;//創(chuàng)建session,org.hibernate.Session

  

  

   //創(chuàng)建一個test方法

   @Test

   publicvoid testnewsbean(){

      //編寫執(zhí)行查詢的語句

      Stringsql="from NewsBean";

      //創(chuàng)建query實(shí)例對象

      Queryquery=session.createQuery(sql);//import org.hibernate.Query

      //query.list();//查詢結(jié)果list集合,符合條件的實(shí)例對象。

      //接受返回的結(jié)果,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 {

      //使用完畢后要關(guān)閉session

      session.close();

   }

}

 運(yùn)行結(jié)果:

HQL:需要from語句

SQL:需要select和from語句

 

(1)          HQL最簡形式

(2)          From指定了HQL查詢主體——持久化類及其屬性

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 {

   private Session session=null;//創(chuàng)建session,org.hibernate.Session

 

   //from子句查詢持久化類,把關(guān)聯(lián)的類詳細(xì)信息也顯示出來

   @Test

   public void fromTest(){

      //編寫執(zhí)行查詢的語句

      String sql="from NewsBean";

      //創(chuàng)建query實(shí)例對象

      Query query=session.createQuery(sql);//import org.hibernate.Query

      //query.list();//查詢結(jié)果list集合,符合條件的實(shí)例對象。

      //接受返回的結(jié)果,import java.util.List

      Listnews=query.list();

   //測試,在控制臺打印測試

      for(NewsBean newsBean:news){

   // System.out.println(newsBean);

   //獲取關(guān)聯(lián)對象的信息,先打印關(guān)聯(lián)對象的名稱

//獲取關(guān)聯(lián)對象的信息,如果不打印關(guān)聯(lián)對象的信息,則不會查詢兩次,只查詢一次。就是只查詢新聞

         System.out.println("新聞頭條:"+newsBean.getNewstitle()+",作者:"+newsBean.getAuthor()+",新聞類別名稱:"+newsBean.getNewsTypebean().getTypename());

      }    

   }

   @Before

   public void setUp() throws Exception {

      //獲得session

      session=HibernateSessionFactory.getSession();    

   }

   @After

   public void tearDown() throws Exception {

      //使用完畢后要關(guān)閉session

      session.close();

   } 

}

結(jié)果:成功地找出,找了兩次 

關(guān)于全限定名。

1.       不需要引入持久化類的全限定名,直接引入類名

2.       是auto-import(自動引入)缺省情況。方便,符合編程習(xí)慣

 

全限定名:from com.imooc.model.Seller

直接使用類名就可以了:from Seller,方便快捷。常用

 

From子句中別名的使用

1.  對查詢的類指定別名

2.  在HQL語句其他部分通過別名引用該類

3.  別名命名習(xí)慣(嚴(yán)格要求,參考java命名習(xí)慣,保證可讀性)

如:from Seller 

 別名:from seller或者s,全小寫,一樣的字母

(1)//別名不對查詢結(jié)果有任何的變化

String sql="from News as newsbean ";

可以去掉as等價(jià)于

String sql=”from News  news”;

 

(2)多個持久化類可以用逗號隔開

String sql="from News as newsbean ,Type as type";

可以去掉as等價(jià)于

String sql=”from News  n,type t”;

Select 子句關(guān)于返回Object數(shù)組和單個對象整理:

代碼:

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 {

       private Session session=null;//創(chuàng)建session,org.hibernate.Session

       //做一個循環(huán)打印信息,用object[]返回查詢結(jié)果

       @Test

       public void testObject(){

      //(1)當(dāng)查詢只是一個屬性的時(shí)候,如果用for循環(huán)打印會報(bào)錯,原因是如果只查詢一個屬性字段,他就是對象而不是obj數(shù)組。

              String hql="select n.newsid,n.newstitle,n.author from NewsBean n";

              //創(chuàng)建query實(shí)例對象

              Query query=session.createQuery(hql);//import org.hibernate.Query

              //接受返回的結(jié)果,import java.util.List

              Listlist=query.list();

              //測試,在控制臺打印測試

              for(Object[] objs:list){

                     System.out.println("newsid:"+objs[0]);//顯示信息

                     System.out.println("newstitle:"+objs[1]);

                     System.out.println("author:"+objs[2]);

              }

       }

       @Before

       public void setUp() throws Exception {

              //獲得session

              session=HibernateSessionFactory.getSession();

       }

       @After

       public void tearDown() throws Exception {

              //使用完畢后要關(guān)閉session

              session.close();

       }

 

}

運(yùn)行結(jié)果:

(2)單個字段的時(shí)候,就不是object數(shù)據(jù),而是一個對象。

@Test

   public void testObject2(){

      //String hql="select n.newsid,n.newstitle,n.author from NewsBean n";

      String hql="select n.newstitle from NewsBean n";

      //創(chuàng)建query實(shí)例對象

      Query query=session.createQuery(hql);//import org.hibernate.Query

      //接受返回的結(jié)果,import java.util.List

      Listlist=query.list();

      //測試,在控制臺打印測試

      for(Object objs:list){

      // System.out.println("newsid:"+objs[0]);

         System.out.println("newstitle:"+objs);

      // System.out.println("author:"+objs[2]);

      }

   }

 

 

//查詢一個的時(shí)候,返回的是對象類型,而不是對象數(shù)組,

Select  n.name from news n;

注明:別名對后期排查有用,養(yǎng)成習(xí)慣。

List形式返回

1,        select子句中使用new  list指定

   //list方法

   @Test

   public void testObject(){

      String hql="select new list(n.newsid,n.newstitle,n.author)from NewsBean n";

      //創(chuàng)建query實(shí)例對象

      Query query=session.createQuery(hql);//import org.hibernate.Query

      //接受返回的結(jié)果,import java.util.List

      List<<u>List> list2=query.list();

      //測試,在控制臺打印測試

      for(List list:list2){

         System.out.println("newsid:"+list.get(0));

         System.out.println("newstitle:"+list.get(1));    System.out.println("author:"+list.get(2));

      }

 

   }

以Map形式返回

1.    select語句語句中使用new map指定

2.    key值為索引值,字符串類型

 

//使用map方法

   //使用map方法

   @Test

   public void testMap(){

      String hql="select new map(n.newsid,n.newstitle,n.author as author)from NewsBean n";

      //創(chuàng)建query實(shí)例對象

      Query query=session.createQuery(hql);//import org.hibernate.Query

      List<<u>Map> maps=query.list();

      for(Map mapss:maps){

         System.out.println("name_map:"+mapss.get("0"));

         System.out.println("newstitle_map:"+mapss.get("1"));

         System.out.println("author_map:"+mapss.get("author"));//通過別名來獲取

      }

 

   }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 横峰县| 新郑市| 浦江县| 布拖县| 克山县| 邵阳市| 都匀市| 黄浦区| 涪陵区| 齐齐哈尔市| 黎城县| 井研县| 莎车县| 黔西| 买车| 清涧县| 白山市| 景德镇市| 综艺| 夏邑县| 子洲县| 合川市| 莆田市| 安西县| 京山县| 罗源县| 乐至县| 沙河市| 长汀县| 大渡口区| 吉隆县| 乌什县| 衡水市| 江阴市| 仙桃市| 古蔺县| 岳西县| 息烽县| 寿阳县| 凤阳县| 利辛县|