hibernate利用crm当中进行组合查询

(1)hibernate检索方式


hibernate利用crm当中进行组合查询_第1张图片

hibernate利用crm当中进行组合查询_第2张图片

hibernate利用crm当中进行组合查询_第3张图片




package cn.itcast.a_hql;

import java.util.Arrays;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import cn.itcast.a_hibernateUtils.HibernateUtils;
import cn.itcast.domain.Customer;

//对hql语句的规范以及使用
public class Demo {

	// hql基本查询
	@Test
	public void fun1() {
		Session session = HibernateUtils.openSession();
		Transaction transaction = session.beginTransaction();
		// 基本语句,此处的类若为唯一的,则不用使用包名
		String hql = "from cn.itcast.domain.Customer";
		// 别名查询
		String hql2 = "select c from Customer";
		// 排序查询
		String hql3 = "from Customer order by cust_name desc";
		Query query = session.createQuery(hql);
		List list = query.list();
		for (Customer customer : list) {
			System.out.println(customer);
		}
		transaction.commit();
		session.close();
	}

	// 条件查询
	@Test
	public void fun2() {
		Session session = HibernateUtils.openSession();
		Transaction transaction = session.beginTransaction();
		// 按位置绑定参数
		String hql = "from Customer where cust_name=?";

		Query query = session.createQuery(hql);
		// query.setString(0, "li");
		query.setParameter(0, "li");
		List list = query.list();
		for (Customer customer : list) {
			System.out.println(customer.getCust_name().toString());
		}
		// 按名称绑定参数
		String hql1 = "from Customer where cust_name=:name";

		Query query2 = session.createQuery(hql1);
		query2.setParameter("name", "li");

		transaction.commit();
		session.close();
	}
	// 分页查询

	@Test
	public void fun3() {
		Session session = HibernateUtils.openSession();
		Transaction transaction = session.beginTransaction();
		// 基本语句,此处的类若为唯一的,则不用使用包名
		String hql = "from cn.itcast.domain.Customer";

		Query query = session.createQuery(hql);
		query.setFirstResult(1);
		query.setMaxResults(3);
		List list = query.list();
		for (Customer customer : list) {
			System.out.println(customer.getCust_id().toString());
		}
		transaction.commit();
		session.close();
	}

	// 统计查询

	@Test
	public void fun4() {
		Session session = HibernateUtils.openSession();
		Transaction transaction = session.beginTransaction();
		// 基本语句,此处的类若为唯一的,则不用使用包名
		String hql = "select count(*) from cn.itcast.domain.Customer";

		Query query = session.createQuery(hql);
		// Number类为几乎所有的数字类型的基类
		Number list = (Number) query.uniqueResult();
		System.out.println(list.toString());
		transaction.commit();
		session.close();
	}

	// 投影查询(即某列的投影)
	@Test
	public void fun5() {
		Session session = HibernateUtils.openSession();
		Transaction transaction = session.beginTransaction();
		// 第一种:基本语句,此处的类若为唯一的,则不用使用包名
		String hql = "select cust_name from cn.itcast.domain.Customer";

		Query query = session.createQuery(hql);
		// 查询某一列,此处的泛型封装利用属性的性质进行对应包装
		List cust_names = query.list();
		System.out.println(cust_names);
		// 第二种:查询多列,则此处的泛型封装为Object类型的数组
		String hql1 = "select cust_id,cust_name from cn.itcast.domain.Customer";
		Query query1 = session.createQuery(hql1);

		List list = query1.list();
		for (Object[] objects : list) {
			System.out.println(Arrays.toString(objects));
		}
		// 第三种:使用投影构造方法进行构造,将字段映射成类(做法:new一下,然后在实体类当中添加构造方法)
		String hql3 = "select new Customer(cust_id,cust_name) from Customer";
		Query query3 = session.createQuery(hql3);
		List list2 = query3.list();
		for (Customer customer : list2) {
			System.out.println(customer);
		}
		transaction.commit();
		session.close();
	}

}






你可能感兴趣的:(hibernate)