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 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;//创建session,org.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:需要select和from语句
(1)
(2)
import java.util.List;
import org.hibernate.Query;