HQL查询
,对象化查询Criteria方法,动态查询DetachedCriteria,例子查询,sql查询,命名查询。
如果单纯的使用hibernate查询数据库只需要懂其中的一项就可以完成想要实现的一般功能,但是
从一个点,让我们掌握6中方法,则提供了更多选择。每一种方法都有其适用的情况与前提。
HQL查询
HQL是hibernate自己的一套查询语言,于SQL语法不同,具有跨数据库的优点。示例代码:
static
void
query(String name){
Session s = null ;
try {
s = HibernateUtil.getSession();
// from后面是对象,不是表名
String hql = " from Admin as admin where admin.aname=:name " ; // 使用命名参数,推荐使用,易读。
Query query = s.createQuery(hql);
query.setString( " name " , name);
List < Admin > list = query.list();
for (Admin admin:list){
System.out.println(admin.getAname());
}
} finally {
if (s != null )
s.close();
}
}
Session s = null ;
try {
s = HibernateUtil.getSession();
// from后面是对象,不是表名
String hql = " from Admin as admin where admin.aname=:name " ; // 使用命名参数,推荐使用,易读。
Query query = s.createQuery(hql);
query.setString( " name " , name);
List < Admin > list = query.list();
for (Admin admin:list){
System.out.println(admin.getAname());
}
} finally {
if (s != null )
s.close();
}
}
适用情况:常用方法,比较传统,类似jdbc。缺点:新的查询语言,适用面有限,仅适用于Hibernate框架。
对象化查询Criteria方法:
static
void
cri(String name,String password){
Session s = null ;
try {
s = HibernateUtil.getSession();
Criteria c = s.createCriteria(Admin. class );
c.add(Restrictions.eq( " aname " ,name)); // eq是等于,gt是大于,lt是小于,or是或
c.add(Restrictions.eq( " apassword " , password));
List < Admin > list = c.list();
for (Admin admin:list){
System.out.println(admin.getAname());
}
} finally {
if (s != null )
s.close();
}
}
Session s = null ;
try {
s = HibernateUtil.getSession();
Criteria c = s.createCriteria(Admin. class );
c.add(Restrictions.eq( " aname " ,name)); // eq是等于,gt是大于,lt是小于,or是或
c.add(Restrictions.eq( " apassword " , password));
List < Admin > list = c.list();
for (Admin admin:list){
System.out.println(admin.getAname());
}
} finally {
if (s != null )
s.close();
}
}
适用情况:面向对象操作,革新了以前的数据库操作方式,易读。缺点:适用面较HQL有限。
动态分离查询DetachedCriteria
static
List dc(DetachedCriteria dc) {
Session s = HibernateUtil.getSession();
Criteria c = dc.getExecutableCriteria(s);
List rs = c.list();
s.close();
return rs;
}
Session s = HibernateUtil.getSession();
Criteria c = dc.getExecutableCriteria(s);
List rs = c.list();
s.close();
return rs;
}
DetachedCriteria dc
=
DetachedCriteria.forClass(User.
class
);
int id = 1 ;
if (id != 0 )
dc.add(Restrictions.eq( " id " , id));
Date age = new Date();
if (age != null )
dc.add(Restrictions.le( " birthday " , age));
List users = dc(dc);
System.out.println( " 离线查询返回结果: " + users);
int id = 1 ;
if (id != 0 )
dc.add(Restrictions.eq( " id " , id));
Date age = new Date();
if (age != null )
dc.add(Restrictions.le( " birthday " , age));
List users = dc(dc);
System.out.println( " 离线查询返回结果: " + users);
适用情况:面向对象操作,分离业务与底层,不需要字段属性摄入到Dao实现层。 缺点:适用面较HQL有限。
例子查询
static
List example(User user) {
Session s = HibernateUtil.getSession();
List < User > users = s.createCriteria(User. class ).add(
Example.create(user)).list();
// List<User>
// users2=s.createCriteria(User.class).add((Example.create(user)).ignoreCase())
// .createCriteria("child").add((Example.create(user))).list();
return users;
}
Session s = HibernateUtil.getSession();
List < User > users = s.createCriteria(User. class ).add(
Example.create(user)).list();
// List<User>
// users2=s.createCriteria(User.class).add((Example.create(user)).ignoreCase())
// .createCriteria("child").add((Example.create(user))).list();
return users;
}
适用情况:面向对象操作。 缺点:适用面较HQL有限,不推荐。
sql查询
static
List sql() {
Session s = HibernateUtil.getSession();
Query q = s.createSQLQuery( " select * from user " ).addEntity(User. class );
List < User > rs = q.list();
s.close();
return rs;
}
Session s = HibernateUtil.getSession();
Query q = s.createSQLQuery( " select * from user " ).addEntity(User. class );
List < User > rs = q.list();
s.close();
return rs;
}
适用情况:不熟悉HQL的朋友,又不打算转数据库平台的朋友,万能方法 缺点:破坏跨平台,不易维护,不面向对象。
命名查询
static
List namedQuery(
int
id) {
Session s = HibernateUtil.getSession();
Query q = s.getNamedQuery( " getUserById " );
q.setInteger( " id " , id);
return q.list();
}
Session s = HibernateUtil.getSession();
Query q = s.getNamedQuery( " getUserById " );
q.setInteger( " id " , id);
return q.list();
}
<?
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 >
< class name ="com.sy.vo.User" table ="user" catalog ="news" >
</ class >
<!-- 命名查询:定义查询条件 -->
< query name ="getUserById" >
<![CDATA[ from User where id=:id ]]>
</ query >
<!-- 命名查询中使用sql,不推荐使用,影响跨数据库
<sql-query name="getUserById2">
<![CDATA[select * from User where ]]>
</sql-query> -->
</ hibernate-mapping >
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping >
< class name ="com.sy.vo.User" table ="user" catalog ="news" >
</ class >
<!-- 命名查询:定义查询条件 -->
< query name ="getUserById" >
<![CDATA[ from User where id=:id ]]>
</ query >
<!-- 命名查询中使用sql,不推荐使用,影响跨数据库
<sql-query name="getUserById2">
<![CDATA[select * from User where ]]>
</sql-query> -->
</ hibernate-mapping >
适用情况:万能方法,有点像ibatis轻量级框架的操作,方便维护。 缺点:不面向对象。基于hql和sql,有一定缺陷。