Hibernate查询语言(HQL)是面向对象的查询语言,其结构与SQL查询相似,不同的是,HQL查询的主体是持久化类而不是数据表。 HQL提供了一种应用和数据库之间的抽象,提高了可移植性。
Hibernate语言查询( Hibernate Query Language,HQL) :它是完全面向对象的查询语句,查询功能非常强大,具备继承、多态和关联等特性 。Hibernate官方推荐使用HQL进行查询。
Hibernate查询语言(HQL)是面向对象的查询语言,其结构与SQL查询相似,不同的是,HQL查询的主体是持久化类而不是数据表。HQL提供了一种应用和数据库之间的抽象,提高了可移植性。HQL子句本身大小写无关,但是其中出现的类名和属性名必须注意大小写。
以下是我对HQL查询进行的一部分基本练习:
实体类:
package org.test.entity;
public class User { private int id; private String uname; private int age; private String sex; private String city; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } //重写Object类下的toString()方法 public String toString(){ return "User [id="+id+",uname="+uname+",age="+age+",sex="+sex+",city="+city+"]"; }
public User(){ } public User(int id,String uname,int age){ this.id=id; this.uname=uname; this.age=age; } } |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping> <!-- name代表实体类名 ,table代表表名 --> <class name="org.test.entity.User" table="user"> <!-- name=id代表user类中属性 column=id代表的是table表中的字段-->
<id name="id" column="id"> <generator class="native"/><!-- 主键生成策略 --> </id> <!-- 其他属性使用property标签来映射 --> <property name="uname" column="uname" type="string"/> <property name="age" column="age" type="int"/> <property name="sex" column="sex" type="string"/> <property name="city" column="city" type="string"/>
</class> </hibernate-mapping> |
配置文件:hibernate.cfg.xml:
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. --> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <!-- <property name="hbm2ddl.auto">update</property>--> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="format_sql">true</property> <mapping resource="org/test/entity/User.hbm.xml"/> </session-factory> </hibernate-configuration> |
DAO层:
package org.test.demo;
import java.util.List;
import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.query.Query; import org.junit.Test; import org.test.entity.User;
public class UserTest { //定义变量 Configuration config=null; SessionFactory sessionFactory=null; Session session=null; Transaction transaction=null; @Test public void HQLTest() { User user=new User(); String username=""; try { session=HibernateUtils.getSession(); //4.开始事务 transaction=session.beginTransaction(); //5.操作 //简单查询 // 调用session.creatQuery创建查询对象 Query query=session.createQuery("from User") ; //调用List方法查询数据,并将数据放入一个List集合 List users=query.list(); //循环输出集合中的数据
//单个属性查询 query=session.createQuery("select uname from User"); List usernames=query.list(); for(int i=0;i<usernames.size();i++){ username = (String)usernames.get(i); System.out.println(username); }
//多属性查询 query=session.createQuery("select uname,sex,city from User "); List users2=query.list(); for(int i=0;i<users2.size();i++){ Object obj[]=(Object[])users2.get(i); System.out.println(obj[0]+"的年龄为:"+obj[1]+",所在的城市为:"+obj[2]); //System.out.println(obj[1]+"的平均年龄为:"+obj[0]); }
{ user=(User)users.get(i); System.out.println(user); }
//实例化查询 query=session.createQuery("select new User(id,uname,age) from User"); List users3=query.list(); for(int i=0;i<users3.size();i++){ User user1=(User)users3.get(i); System.out.println(user1.getId()+user1.getUname()+"年龄为"+user1.getAge()); }
//统计函数 query=session.createQuery("select count(*) from User where sex='男'"); Object count=(Object)query.uniqueResult(); System.out.println("公有"+count+"条记录"); //子查询 query=session.createQuery("from User where age>(select avg(age) from User)"); List users4=query.list(); for(int i=0;i<users4.size();i++){ User user2=(User)users4.get(i); System.out.println("姓名:"+user2.getUname()+"年龄:"+user2.getAge()); }
//分页查询 //avg() |
//6.提交事务
transaction.commit();
//7.关闭资源
session.close();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
查询结果为:
数据库表为:
1.简单查询:
2.属性查询:
3.实例化查询:
4.统计查询:
5.子查询:
avg()查询:
分组查询:
分页查询: