hibernate的查询方式及抓取策略

查询方式

      根据唯一标识id进行检索:

                  get与load方法(https://blog.csdn.net/qq_40605913/article/details/81460410)。

      对象导航检索:

                  根据一个已经查询到的对象,获得其关联的对象。

     HQL查询:

             Hibernate Query Language,Hibernate的查询语言,是一种面向对象的方式的查询语言,语法类似SQL。通过session.createQuery(),用于接收一个HQL进行查询方式。

简单查询:

       //HQL的简单查询
	public void test(){
		Session session = HibernateUtils.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		
		Query query = session.createQuery("from Customer");
		
		List list = query.list();
		for (Customer customer : list) {
			System.out.println(customer);
		}
		transaction.commit();
	}
      //HQL的简单查询之别名查询
	public void test(){
		Session session = HibernateUtils.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		//c是别名,但HQL不支持*,聚合函数中的*除外
		Query query = session.createQuery("select c from Customer c");
		
		List list = query.list();
		for (Customer customer : list) {
			System.out.println(customer);
		}
		transaction.commit();
	}

排序查询:

      //HQL的排序查询
	public void test3(){
		Session session = HibernateUtils.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		//默认
//		Query query = session.createQuery(" from Customer order by cust_id");
		//降序
		Query query = session.createQuery(" from Customer order by cust_id desc");
		//升序
//		Query query = session.createQuery("  from Customer order by cust_id asc");

		List list = query.list();
		for (Customer customer : list) {
			System.out.println(customer);
		}
		transaction.commit();
	}

条件查询:

     public void test3(){
		Session session = HibernateUtils.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		
		//条件查询
		//1.按位置绑定,多个条件时类推
//		Query query = session.createQuery("from Customer where cust_id=?");
//		query.setParameter(0, 1l);
//		List list = query.list();
		//2.按名称绑定--根据参数位置进行绑定
														//aaa,bbb这些名字随便起,但与:之间不能有空格
		Query query = session.createQuery("from Customer where cust_id = :aaa and cust_name = :bbb");
		//设置参数
		query.setParameter("aaa", 1l);
		query.setParameter("bbb", "22");
		
		List list = query.list();
		
		transaction.commit();
	}

投影查询:

       public void test3(){
		Session session = HibernateUtils.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		//单个属性
//		List list = session.createQuery("select c.cust_name from Customer c").list();
		//多个属性
//		List list = session.createQuery("select c.cust_name,c.cust_id from Customer c").list();
		
//		for (Object [] object : list) {
//			System.out.println(Arrays.toString(object));
//		}
		//将查询结果封装到对象中
		List list = session.createQuery("select new Customer(cust_name,cust_id) from Customer").list();
		for (Customer customer : list) {
			System.out.println(customer);
		}
		transaction.commit();
	}
	 
  

分页查询:

      //HQL的分页查询
	public void test3(){
		Session session = HibernateUtils.getCurrentSession();
		Transaction transaction = session.beginTransaction();

		Query query = session.createQuery("from Customer");
		
		query.setFirstResult(0);
		query.setMaxResults(2);
		
		List list = query.list();
		
		for (Object object : list) {
			System.out.println(object);
		}
		
		transaction.commit();
	}

分组统计查询:

      //HQL的分组统计查询
	public void test3(){
		Session session = HibernateUtils.getCurrentSession();
		Transaction transaction = session.beginTransaction();

		//聚合函数:count(),sum(),max(),min(),avg()
//		Object object = session.createQuery("select count(*) from Customer").uniqueResult();
//		System.out.println(object);
		
		Query query = session.createQuery("select cust_id,count(*) from Customer group by cust_name");
		List object = query.list();
		for (Object[] object2 : object) {
			System.out.println(Arrays.toString(object2));
		}
		transaction.commit();
	}

QBC:

 Query By Criteria,条件查询。是一种更加面向对象化的查询的方式。

简单查询:

	//简单查询
	public void test(){
		Session session = HibernateUtils.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		
		//获得Criteria对象
		Criteria criteria = session.createCriteria(Customer.class);
		List list = criteria.list();
		for (Customer customer : list) {
			System.out.println(customer.toString());
		}
		transaction.commit();
	}

排序查询:

          //排序查询
		public void test1(){
			Session session = HibernateUtils.getCurrentSession();
			Transaction transaction = session.beginTransaction();
			
			//获得Criteria对象
			Criteria criteria = session.createCriteria(Customer.class);
			//升序查询,对应降序为desc
			criteria.addOrder(Order.asc("cust_id"));
			
			List list = criteria.list();
			for (Customer customer : list) {
				System.out.println(customer.toString());
			}
			transaction.commit();
		}

分页查询:

          //分页查询
		public void test1(){
			Session session = HibernateUtils.getCurrentSession();
			Transaction transaction = session.beginTransaction();
			
			//获得Criteria对象
			Criteria criteria = session.createCriteria(Customer.class);
			
			criteria.setFirstResult(0);
			criteria.setMaxResults(2);
			
			List list = criteria.list();
			for (Customer customer : list) {
				System.out.println(customer);
			}
			transaction.commit();
		}

条件查询:

          //条件查询
		public void test1(){
			Session session = HibernateUtils.getCurrentSession();
			Transaction transaction = session.beginTransaction();
			
			//获得Criteria对象
			Criteria criteria = session.createCriteria(Customer.class);
			
			/**
			 * =  eq
			 * >  gt
			 * >= ge
			 * <  lt
			 * <= le
			 * <> ne 不等于
			 */
			//设置条件
			criteria.add(Restrictions.eq("cust_name", "11"));
			
			List list = criteria.list();
			for (Customer customer : list) {
				System.out.println(customer);
			}
			transaction.commit();
		}

统计查询:

          //统计查询
		@Test
		public void test1(){
			Session session = HibernateUtils.getCurrentSession();
			Transaction transaction = session.beginTransaction();
			
			//获得Criteria对象
			Criteria criteria = session.createCriteria(Customer.class);
		
			/**
			 * add :普通的条件,where后面事件
			 * addOrder:排序
			 * setProjection:聚合函数和group by having
			 */
			criteria.setProjection(Projections.rowCount());
			Long num = (Long)criteria.uniqueResult();
			
			System.out.println(num);
			
			transaction.commit();
		}

离线查询:

        //离线查询
	public void test1(){
	
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
		detachedCriteria.add(Restrictions.ilike("cust_name", "1%"));
		
		
		Session session = HibernateUtils.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		
		Criteria  criteria = detachedCriteria.getExecutableCriteria(session);
		List list = criteria.list();
		for (Customer customer : list) {
			System.out.println(customer);
		}
		
	}

多表查询:

        //多表查询
	public void test1(){	
		Session session = HibernateUtils.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		//SQL:SELECT * FROM cst_customer c INNER JOIN cst_linkman l ON c.cust_id = l.lkm_cust_id;
		
		//HQL(内连接):from Customer c inner join c.linkMans
		//List list = session.createQuery("from Customer c inner join c.linkMans").list();
		
		//HQL(迫切内连接):在普通内连接inner join后添加一个关键字fetch。它发的SQL语句与
		//内连接一样,作用:通知hibernate将数据封装到对象里,因为Customer中有LinkMan的集合,所以把
		//LinkMan封装到Customer中,返回的list集合中放的是Customer(select distincr c 是为了去掉重复)
		List list = session.createQuery("select distinct c from Customer c inner join fetch c.linkMans").list();
		
	}

SQL:

        public void test1(){	
			Session session  = HibernateUtils.getCurrentSession();
			Transaction transaction = session.beginTransaction();
			
			SQLQuery sqlQuery = session.createSQLQuery("select * from cst_customer");
			
			sqlQuery.addEntity(Customer.class);
			
			List list = sqlQuery.list();
			
			for (Customer customer : list) {
				System.out.println(customer);
			}
			
			transaction.commit();
		}			

延迟加载:

       延迟加载:lazy(懒加载)。执行到该行代码的时候,不会发送语句去进行查询,在真正使用这个对象的属性的时候才会发送SQL语句进行查询。

 类级别延迟加载:

    //类级别延迟加载,在标签上配置lazy=true,这样load方法就和get方法一样
	//在使用时才发送查询语句
	public void test1(){
		Session session = HibernateUtils.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		
		session.load(Customer.class, 1l);
		
		transaction.commit();
	}

          让lazy失效:

                        将lazy设置为false

                        持久化类使用final修饰

                         Hibernate. Initialize()

关联级别:

        在上来配置

抓取策略:

 

上的fetch和lazy

             fetch:抓取策略,控制SQL语句格式

                             select                :默认值,发送普通的select语句,查询关联对象

                             join                    :发送一条迫切左外连接查询关联对象

                             subselect         :发送一条子查询查询其关联对象

             lazy:延迟加载,控制查询关联对象的时候是否采用延迟

                             true                   :默认值,查询关联对象的时候,采用延迟加载

                             false                  :查询关联对象的时候,不采用延迟加载

                             extra                 :及其懒惰。

上的fetch和lazy

            fetch :抓取策略,控制SQL语句格式。

                             select       :默认值,发送普通的select语句,查询关联对象。

                             join           :发送一条迫切左外连接。

            lazy   :延迟加载,控制查询关联对象的时候是否采用延迟。

                             proxy        :默认值,proxy具体的取值,取决于另一端的上的lazy的值。

                             false         :查询关联对象,不采用延迟。

                             no-proxy  :(不会使用)

批量抓取:

                 一批关联对象一起抓取,batch-size

                 上可以配置batch-size,但one-many中不能配置,需要在一的一方的class标签中配置这个属性

你可能感兴趣的:(hibernate的查询方式及抓取策略)