hibernate各种简单查询和复杂查询

// 简单查询
	@Test
	public void easyQuery() {
		Session session = HibernateUtil.getSession();
		Query query = session.createQuery("from Person");
		List personList = query.list();
		Person person = null;
		for (int i = 0; i < personList.size(); i++) {
			person = (Person) personList.get(i);
			System.out.println(i + "--" + person);
		}
		HibernateUtil.closeSession();
	}
	
	//属性查询
	@Test
	public void QueryPersonId(){
		Session session = HibernateUtil.getSession();		
		Query query = session.createQuery("select p.person_name from Person p");
		List personNameList = query.list();
		for(int i=0;i personList = query.list();
		Person person = null;
		for (int i = 0; i < personList.size(); i++) {
			person = (Person) personList.get(i);
			System.out.println(i + "--" + person);
		}
		HibernateUtil.closeSession();
	}
	
	//按照性别分组展示平均年龄
	@Test
	public void queryBySex(){
		Session session = HibernateUtil.getSession();
		Query query = session.createQuery("select p.person_sex,avg(p.person_age) from Person p group by p.person_sex ");
		List persons = query.list();
		for(int i=0;i(select avg(person_age) from Person)");
		List personList = query.list();
		Person person = null;
		for (int i = 0; i < personList.size(); i++) {
			person = (Person) personList.get(i);
			System.out.println(i + "--" + person);
		}
		HibernateUtil.closeSession();
	}

你可能感兴趣的:(框架心得)