Hibernate的查询功能

时间:2022-01-16 06:35:32

1.Query对象

  1.使用Query对象,不需要写sql语句,但是写hql语句

    (1)hql:hibernate query language,提供查询语言,这个hql语言和普通sql语句相似

    (2)使用sql操作表和表字段

        使用hql操作类与属性

代码演示:

public class HibernateQueryDemo {
//Query对象
@Test
public void testQuery() {
SessionFactory sf=null;
Session session=null;
Transaction tx=null;
try {
sf=HibernateUtils.getSessionFactory();
session=sf.openSession();
tx=session.beginTransaction();
//
Query query=session.createQuery("from User");//查询语句为from加类名
List<User> list=query.list();
for (User user : list) {
System.out.println(user);
}
//
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}finally {
session.close();
sf.close();
}
}
}

2.Criteria象

@Test
public void testQuery() {
SessionFactory sf=null;
Session session=null;
Transaction tx=null;
try {
sf=HibernateUtils.getSessionFactory();
session=sf.openSession();
tx=session.beginTransaction();
//
Criteria criteria = session.createCriteria(User.class);
List<User> list=criteria.list();
for (User user : list) {
System.out.println(user);
}
//
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}finally {
session.close();
sf.close();
}
}

3.SQLQuery对象

调用底层sql语句

@Test
public void testQuery() {
SessionFactory sf=null;
Session session=null;
Transaction tx=null;
try {
sf=HibernateUtils.getSessionFactory();
session=sf.openSession();
tx=session.beginTransaction();
//
// SQLQuery sqlQuery=session.createSQLQuery("select * from t_user");
// List<Object[]> list=sqlQuery.list();//返回的是数组的形式
// for (Object[] object : list) {
// System.out.println(Arrays.toString(object));
// }
//
SQLQuery sqlQuery=session.createSQLQuery("select * from t_user");
sqlQuery.addEntity(User.class);
List<User> list=sqlQuery.list();//返回User对象
for (User user : list) {
System.out.println(user);
}
//
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}finally {
session.close();
sf.close();
}

Utils模板

package com.littlepage.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class HibernateUtils {
private static Configuration cf;
private static SessionFactory sf;
static {
cf = new Configuration().configure();
sf = cf.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return sf;
}
public static void main(String[] args) { }
public static Session getSessionObject() {
return sf.getCurrentSession();
}
}

事务模板

import java.util.Arrays;
import java.util.List; import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test; public class HibernateQueryDemo {
//Query对象
@Test
public void testQuery() {
SessionFactory sf=null;
Session session=null;
Transaction tx=null;
try {
sf=HibernateUtils.getSessionFactory();
session=sf.openSession();
tx=session.beginTransaction();
//
// 事务内容
//
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
}finally {
session.close();
sf.close();
}
}
}