Hibernate的三种查询数据方式

1.HQL(Hibernate Query Language)是hibernate专门用于查询数据的语句,有别于SQL,HQL 更接近于面向对象的思维方式。 比如使用的是类的名字Product,而非表格的名字product_

下面使用HQL,根据name进行模糊查询

package com.how2java.test;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.how2java.pojo.Product;

public class TestHibernate {
	public static void main(String[] args) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session s = sf.openSession();
		s.beginTransaction();
                //下面为核心代码
		String name = "iphone";
		Query q =s.createQuery("from Product p where p.name like ?");//1. 首先根据hql创建一个Query对象.使用hql的时候,用的是类名Product,而不是表名product_
使用hql的时候,不需要在前面加 select *
		q.setString(0, "%"+name+"%");//2. 设置参数(和基1的PreparedStatement不一样,Query是基0的)
		List<Product> ps= q.list();//3. 通过Query对象的list()方法即返回查询的结果了。
		for (Product p : ps) {
			System.out.println(p.getName());
		}
		
		s.getTransaction().commit();
		s.close();
		sf.close();
	}
}

2.Criteria:使用Criteria进行数据查询。 与HQL和SQL的区别是Criteria 完全是 面向对象的方式在进行数据查询,将不再看到有sql语句的痕迹.

下面是使用Criteria,根据name进行模糊查询:

package com.how2java.test;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import com.how2java.pojo.Product;
 
public class TestHibernate {
    public static void main(String[] args) {
        SessionFactory sf = new Configuration().configure().buildSessionFactory(); 
        Session s = sf.openSession();
        s.beginTransaction();
        //下面是核心代码
        String name = "iphone";
        Criteria c= s.createCriteria(Product.class);//1. 通过session的createCriteria创建一个Criteria 对象
        c.add(Restrictions.like("name", "%"+name+"%"));//2. Criteria.add 增加约束。 在本例中增加一个对name的模糊查询(like)
        List<Product> ps = c.list();//3. 调用list()方法返回查询结果的集合
        for (Product p : ps) {
            System.out.println(p.getName());
        }
        s.getTransaction().commit();
        s.close();
        sf.close();
    }
}

除此之外,Criteria 还可以很方便的进行进行分页查询和获取总数。

3.SQL:通过标准SQL语句进行查询 。Hibernate依然保留了对标准SQL语句的支持,在一些场合,比如多表联合查询,并且有分组统计函数的情况下,标准SQL语句依然是效率较高的一种选择。

使用标准SQL,根据name进行模糊查询:

package com.how2java.test;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class TestHibernate {
	public static void main(String[] args) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session s = sf.openSession();
		s.beginTransaction();
                //下面是核心代码
		String name = "iphone";	
		String sql = "select * from product_ p where p.name like '%"+name+"%'";//这里直接对表product_进行查询
		Query q= s.createSQLQuery(sql);//使用Session的createSQLQuery方法执行标准SQL语句
		List<Object[]> list= q.list();//因为标准SQL语句有可能返回各种各样的结果,比如多表查询,分组统计结果等等。 不能保证其查询结果能够装进一个Product对象中,所以返回的集合里的每一个元素是一个对象数组。 然后再通过下标把这个对象数组中的数据取出来。
		for (Object[] os : list) {
			for (Object filed: os) {
				System.out.print(filed+"\t");
			}
			System.out.println();
		}
		
		s.getTransaction().commit();
		s.close();
		sf.close();
	}
}



猜你喜欢

转载自blog.csdn.net/IT_boy_Jason/article/details/79940888