07-Hibernate查询HQL,Criteria,原生SQL查询总结及语法详解

07-Hibernate查询HQL,Criteria,原生SQL查询总结及语法详解

  • 查询HQL,Criteria,原生SQL查询总结及语法详解

    • 查询HQL语法

      • OID查询—get

      • 对象属性导航查询

      • HQL

      • 单表查询语法练习

        //基本语法
        	@Test
        	public void fun1(){
        		Session session = HibernateUtils.openSession();
        		Transaction tx = session.beginTransaction();
        		//-----------------------------------------
        		String hql = "from com.qingzi.domain.Customer";
        		String hql1 = "from Customer";
        		String hql2 = "from java.lang.Object";
        		
        		Query query = session.createQuery(hql2);
        		List list = query.list();
        		System.out.println(list);
        		
        		//-----------------------------------------
        		tx.commit();
        		session.close();
        	}
        
        //排序
        	@Test
        	public void fun2(){
        		Session session = HibernateUtils.openSession();
        		Transaction tx = session.beginTransaction();
        		//-----------------------------------------
        		String hql = "from com.qingzi.domain.Customer order by cust_id asc";
        		String hql1 = "from com.qingzi.domain.Customer order by cust_id desc";
        		Query query = session.createQuery(hql1);
        		List list = query.list();
        		System.out.println(list);
        		//-----------------------------------------
        		tx.commit();
        		session.close();
        	}
        
        //条件查询
        	@Test
        	public void fun3(){
        		Session session = HibernateUtils.openSession();
        		Transaction tx = session.beginTransaction();
        		//-----------------------------------------
        		String hql = "from com.qingzi.domain.Customer where cust_id = ?";
        		String hql1 = "from com.qingzi.domain.Customer where cust_id = :cust_id";
        		Query query = session.createQuery(hql1);
        		
        		//query.setParameter(0, 1l);
        		query.setParameter("cust_id", 1l);
        		List list = query.list();
        		System.out.println(list);
        		//-----------------------------------------
        		tx.commit();
        		session.close();
        	}
        
        	//分页查询
        	@Test
        	public void fun4(){
        		Session session = HibernateUtils.openSession();
        		Transaction tx = session.beginTransaction();
        		//-----------------------------------------
        		String hql = "from com.qingzi.domain.Customer";
        		Query query = session.createQuery(hql);
        		
        		//(当前页码-1)*每页条数
        		query.setFirstResult(1);
        		query.setMaxResults(2);
        		
        		List list = query.list();
        		System.out.println(list);
        		//-----------------------------------------
        		tx.commit();
        		session.close();
        	}
        
        // 统计查询
        	//count 计数
        	//sum  求和
        	//avg  平均数
        	//max 求最大值
        	//min 求最小值
        	@Test
        	public void fun5(){
        		Session session = HibernateUtils.openSession();
        		Transaction tx = session.beginTransaction();
        		//-----------------------------------------
        		String hql1 = "select count(*) from com.qingzi.domain.Customer ";//完整写法
        		String hql2 = "select sum(cust_id) from com.qingzi.domain.Customer ";//完整写法
        		String hql3 = "select avg(cust_id) from com.qingzi.domain.Customer ";//完整写法
        		String hql4 = "select max(cust_id) from com.qingzi.domain.Customer ";//完整写法
        		String hql5 = "select min(cust_id) from com.qingzi.domain.Customer ";//完整写法
        		Query query = session.createQuery(hql5);
        		Number number = (Number) query.uniqueResult();
        		System.out.println(number);
        		//-----------------------------------------
        		tx.commit();
        		session.close();
        	}
        
        //投影查询
        	@Test
        	public void fun6(){
        		Session session = HibernateUtils.openSession();
        		Transaction tx = session.beginTransaction();
        		//-----------------------------------------
        		String hql1 = "select cust_name from com.qingzi.domain.Customer ";
        		String hql2 = "select cust_name,cust_id from com.qingzi.domain.Customer ";
        		String hql3 = "select new Customer(cust_id,cust_name) from com.qingzi.domain.Customer ";
        		Query query = session.createQuery(hql3);
        		
        		List list = query.list();
        		System.out.println(list);
        		//-----------------------------------------
        		tx.commit();
        		session.close();
        	}
        
      • 多表查询语法练习(不常用)

        • 内连接(迫切)
        • 外链接
          • 左外(迫切)
          • 右外(迫切)
        //基本语法
        	@Test
        	public void fun1(){
        		//HQL 内连接=》将连接的两端对象分别返回,放到数据库中。
        		Session session = HibernateUtils.openSession();
        		Transaction tx = session.beginTransaction();
        		//---------------------------------------------------------
        		String hql = "from Customer c inner join c.linkMens ";
        		
        		Query query = session.createQuery(hql);
        			List<Object[]> list = query.list();
        			for(Object[]arr :list){
        				System.out.println(Arrays.toString(arr));
        				
        			}
        		
        		
        		//----------------------------------------------------------
        		tx.commit();
        		session.close();
        	}
        
        //基本语法
        		@Test
        		public void fun2(){
        			
        			//HQL 迫切内连接 =》帮我们封装,返回值就是一个对象
        			Session session = HibernateUtils.openSession();
        			Transaction tx = session.beginTransaction();
        			//---------------------------------------------------------
        			String hql = "from Customer c inner join fetch c.linkMens ";
        			
        			Query query = session.createQuery(hql);
        			List<Customer> list = query.list();
        				
        			System.out.println(list);
        			
        			//----------------------------------------------------------
        			tx.commit();
        			session.close();
        	}
        
        //基本语法
        	@Test
        	public void fun3(){
        		
        		//HQL  左外连接=》将连接的两端对象分别返回,放到数据库中。
        		Session session = HibernateUtils.openSession();
        		Transaction tx = session.beginTransaction();
        		//---------------------------------------------------------
        		String hql = "from Customer c left join c.linkMens ";
        		
        		Query query = session.createQuery(hql);
        			List<Object[]> list = query.list();
        			for(Object[]arr :list){
        				System.out.println(Arrays.toString(arr));
        				
        			}
        		
        		
        		//----------------------------------------------------------
        		tx.commit();
        		session.close();
        	}
        
        //基本语法
        	@Test
        	public void fun4(){
        		
        		//HQL  右外连接=》将连接的两端对象分别返回,放到数据库中。
        		Session session = HibernateUtils.openSession();
        		Transaction tx = session.beginTransaction();
        		//---------------------------------------------------------
        		String hql = "from Customer c right join c.linkMens ";
        		
        		Query query = session.createQuery(hql);
        			List<Object[]> list = query.list();
        			for(Object[]arr :list){
        				System.out.println(Arrays.toString(arr));
        				
        			}
        		
        		
        		//----------------------------------------------------------
        		tx.commit();
        		session.close();
        	}
        
    • 学习Criteria语法详解

      //学习criteria
      public class Demo {
      	
      	//基本语法
      	@Test
      	public void fun1(){
      		Session session = HibernateUtils.openSession();
      		Transaction tx = session.beginTransaction();
      		//---------------------------------------------------------
      		
      		Criteria c = session.createCriteria(Customer.class);
      		
      		List<Customer> list = c.list();
      		System.out.println(list);
      		//----------------------------------------------------------
      		tx.commit();
      		session.close();
      	}
      		
      	//条件查询
      	@Test
      	public void fun2(){
      		Session session = HibernateUtils.openSession();
      		Transaction tx = session.beginTransaction();
      		//---------------------------------------------------------
      		Criteria criteria = session.createCriteria(Customer.class);
      		criteria.add(Restrictions.idEq(1l));
      		criteria.add(Restrictions.eq("cust_id", 1l));
      		
      		List<Customer> list = criteria.list();
      		System.out.println(list);
      		//----------------------------------------------------------
      		tx.commit();
      		session.close();
      	}
      	
      	//分页语法  与HQL一样
      	@Test
      	public void fun3(){
      		Session session = HibernateUtils.openSession();
      		Transaction tx = session.beginTransaction();
      		//---------------------------------------------------------
      		Criteria c = session.createCriteria(Customer.class);
      		//limit ?,?
      		c.setFirstResult(1);
      		c.setMaxResults(2);
      		
      		List<Customer> list = c.list();
      		System.out.println(list);
      		//----------------------------------------------------------
      		tx.commit();
      		session.close();
      	}
      	
      	//排序语法
      	@Test
      	public void fun4(){
      		Session session = HibernateUtils.openSession();
      		Transaction tx = session.beginTransaction();
      		//---------------------------------------------------------
      		Criteria c = session.createCriteria(Customer.class);
      		c.addOrder(Order.asc("cust_id"));
      		c.addOrder(Order.desc("cust_id"));
      		
      		List<Customer> list = c.list();
      		System.out.println(list);
      		//----------------------------------------------------------
      		tx.commit();
      		session.close();
      	}
      	
      	//统计语法
      	@Test
      	public void fun5(){
      		Session session = HibernateUtils.openSession();
      		Transaction tx = session.beginTransaction();
      		//---------------------------------------------------------
      		Criteria c = session.createCriteria(Customer.class);
      		
      		//设置查询目标
      		c.setProjection(Projections.rowCount());
      		
      		List<Customer> list = c.list();
      		System.out.println(list);
      		//----------------------------------------------------------
      		tx.commit();
      		session.close();
      	}
      }
      
    • 学习离线Criteria

      public class Demo2 {
      	@Test
      	public void fun1(){
      		//Service/web
      		DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);
      		
      		dc.add(Restrictions.idEq(1l));//瓶装条件(全部与普通Criteria一致)
      		
      		//--------------------------------------------------
      		Session session = HibernateUtils.openSession();
      		Transaction tx = session.beginTransaction();
      		
      		//------------------------------------------------
      		Criteria c = dc.getExecutableCriteria(session);
      		List list = c.list();
      		
      		System.out.println(list);
      		
      		tx.commit();
      		session.close();
      	}
      }
      

你可能感兴趣的:(Hibernate)