hibernate项目的HQL(SQL区别、Select语句、返回list、map、object数组与单个对象)笔记整理

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. 通过sessioncreateQuery()方法创建Query实例。

2.      createQuery方法包含一个HQL语句参数,createQueryhql)。就是要执行的查询语句。

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 String newstitle;

  private String author;

  private String content;   //存储的是文本的路径

  private String pubtime;   //默认格式为'0000-00-00 00:00'

// privateint typeid;

  private String newspic;   //存储的是图片的路径

  private NewstypeBean newsTypebean

 

  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;

 

public classNewsBeanTest {

 

   private Session session=null;//创建sessionorg.hibernate.Session

  

  

   //创建一个test方法

   @Test

   public void 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

   public void setUp() throws Exception {

      //获得session

      session=HibernateSessionFactory.getSession();

     

   }

 

   @After

   public void tearDown() throws Exception {

      //使用完毕后要关闭session

      session.close();

   }

}

 运行结果:



HQL:需要from语句

SQL:需要selectfrom语句

 

(1)          HQL最简形式

(2)          From指定了HQL查询主体——持久化类及其属性

import java.util.List;

import org.hibernate.Query;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值