hibernate04

hql查询

查询所有对象


        @Test
	public void fun1() {
		Session session = HibernateUitl.getOpenSession();
		Transaction transaction = session.beginTransaction();
		String hql="from java.lang.Object ";
		Query<Object[]> query = session.createQuery(hql);
		List<Object[]> list = query.list();
		System.out.println(list);
		transaction.commit();
		session.close();
	}

排序查询

        @Test
	public void fun2() {
		Session session = HibernateUitl.getOpenSession();
		Transaction transaction = session.beginTransaction();
		String hql="from Customer order by cust_id desc ";
		Query<Customer> query = session.createQuery(hql);
		List<Customer> list = query.list();
		System.out.println(list);
		transaction.commit();
		session.close();
	}

聚合函数 count sum max  min  avg

       @Test
	public void fun3() {
		Session session = HibernateUitl.getOpenSession();
		Transaction transaction = session.beginTransaction();
		String hql1="select count(*) from Customer";
		String hql2="select avg(cust_id) from Customer";
		Query query = session.createQuery(hql2);
		//number是所有基本数据类型的包装类的分类
		Number number =(Number) query.uniqueResult();
		System.out.println(number);
		transaction.commit();
		session.close();
	}

投影查询

将查询出来的值放到对象中

注意:封装到对象中 依赖的是对象中的构造方法

        @Test
	public void fun4() {
		Session session = HibernateUitl.getOpenSession();
		Transaction transaction = session.beginTransaction();
		String hql="select cust_id,cust_name from Customer ";
		String hql1="select new Customer(cust_id,cust_name) from Customer ";
		Query<Customer> query = session.createQuery(hql1);
		List<Customer> list = query.list();
		System.out.println(list);
              //Query<Object[]> query = session.createQuery(hql);
              //List<Object[]> list = query.list();
              //for (Object[] objects : list) {
              //	System.out.println(Arrays.toString(objects));
              //		}
		
		transaction.commit();
		session.close();
	}

内连接查询

隐式内连接

select * from A a,B b where a.id=b.id;

显示内连接

select * from A a join B b  on a.id=b.id;

        @Test
	public void fun1() {
		Session session = HibernateUitl.getOpenSession();
		Transaction transaction = session.beginTransaction();
		String hql="from Customer c join c.linkmans ";
		Query<Object[]> query = session.createQuery(hql);
		List<Object[]> list = query.list();
		for (Object[] objects : list) {
			System.out.println(Arrays.toString(objects));
		}
		transaction.commit();
		session.close();
	}

外链接

 左外链接

select * from A a left join B b  on a.id=b.id;

右外链接

select * from A a right join B b  on a.id=b.id;

        @Test
	public void fun2() {
		Session session = HibernateUitl.getOpenSession();
		Transaction transaction = session.beginTransaction();
		//普通外链接  返回object[]
		//fetch(迫切外链接) 加在join后  把查询出来的值封装到了对象中 返回对象类型
		String hql="from Customer c left join  c.linkmans ";
		Query<Object[]> query = session.createQuery(hql);
		List<Object[]> list = query.list();
		//System.out.println(list);
		for (Object[] objects : list) {
			System.out.println(Arrays.toString(objects));
		}
		transaction.commit();
		session.close();
	}

离线查询

        @Test
	public void fun3() {
		String username="蓝";
		//创建离线查询对象
		//参数表示你要查询的对象.class
		DetachedCriteria dc=DetachedCriteria.forClass(Customer.class);
		//添加查询条件
		dc.add(Restrictions.like("cust_name","%"+ username+"%"));
		//将对象向下传 到dao层
		//使用session执行这个离线对象
		Session session = HibernateUitl.getOpenSession();
		Transaction transaction = session.beginTransaction();
		//离线查询(脱离session时也可以添加查询条件)
		//让离线对象与session产生关系
		Criteria criteria = dc.getExecutableCriteria(session);
		List list = criteria.list();
		System.out.println(list);
		session.close();
	}







展开阅读全文

没有更多推荐了,返回首页