hibernate之13.hql(一)

查询所有数据:

@Test
	public void queryAllTest(){
		Session session=null;
		try{
			session=HibernateUtil.openSession();
			String hql="from Student";
			Query query=session.createQuery(hql);
			List<Student> list=query.list();
			for(Student student: list){
				System.out.println(student);
			}
		}finally{
			session.close();
		}
	}


基于参数查询:

@Test
	public void queryWithParamTest(){
		Session session=null;
		try{
			session=HibernateUtil.openSession();
			String hql="from Student where studentId=?";
			Query query=session.createQuery(hql);
			query.setInteger(0, 83);
			List<Student> list=query.list();
			for(Student student: list){
				System.out.println(student);
			}
		}finally{
			session.close();
		}
	}


基于命令参数查询

@Test
	public void queryWithEqualsNamedParamTest(){
		Session session=null;
		try{
			session=HibernateUtil.openSession();
			String hql="from Student where studentId=:studentId";
			Query query=session.createQuery(hql);
			query.setInteger("studentId", 83);
			List<Student> list=query.list();
			for(Student student: list){
				System.out.println(student);
			}
		}finally{
			session.close();
		}
	}


Like:

@Test
	public void queryWithLikeTest(){
		Session session=null;
		try{
			session=HibernateUtil.openSession();
			String hql="from Student where studentName like ?";
			Query query=session.createQuery(hql);
			query.setString(0, "%王%");
			List<Student> list=query.list();
			for(Student student: list){
				System.out.println(student);
			}
		}finally{
			session.close();
		}
	}


in:

@Test
	public void queryWithInTest(){
		Session session=null;
		try{
			session=HibernateUtil.openSession();
			String hql="from Student where studentId in (:stuIds)";
			Query query=session.createQuery(hql);
			query.setParameterList("stuIds", new Integer[]{63,83});
			List<Student> list=query.list();
			for(Student student: list){
				System.out.println(student);
			}
		}finally{
			session.close();
		}
	}


查询部份字段:

@Test
	public void queryColumnsTest(){
		Session session=null;
		try{
			session=HibernateUtil.openSession();
			String hql="select studentId,studentName from Student where studentName like ?";
			Query query=session.createQuery(hql);
			query.setString(0, "%王%");
			List<Object[]> list=query.list();
			for(Object[] objs: list){
				System.out.println(objs[0]+" "+objs[1]);
			}
		}finally{
			session.close();
		}
	}


查询单个字段:

@Test
	public void querySingleColumnTest(){
		Session session=null;
		try{
			session=HibernateUtil.openSession();
			String hql="select studentName from Student where studentName like ?";
			Query query=session.createQuery(hql);
			query.setString(0, "%王%");
			List<String> list=query.list();
			for(String name: list){
				System.out.println(name);
			}
		}finally{
			session.close();
		}
	}


查询统计:

@Test
	public void queryCountTest(){
		Session session=null;
		try{
			session=HibernateUtil.openSession();
			String hql="select count(*) from Student where studentName like ?";
			Query query=session.createQuery(hql);
			query.setString(0, "%王%");
			List<Long> list=query.list();
			for(Long cnt: list){
				System.out.println(cnt);
			}
		}finally{
			session.close();
		}
	}


单个结果集

@Test
	public void queryUniqueResultTest(){
		Session session=null;
		try{
			session=HibernateUtil.openSession();
			String hql="select stu from Student stu where stu.studentId=?";
			Query query=session.createQuery(hql);
			query.setInteger(0, 83);
			Student stu=(Student)query.uniqueResult();
			System.out.println(stu);
		}finally{
			session.close();
		}
	}


分页查询:

@Test
	public void queryPagerTest(){
		Session session=null;
		try{
			session=HibernateUtil.openSession();
			String hql="select stu from Student stu where studentName like ?";
			Query query=session.createQuery(hql);
			//从第几条记录开始
			query.setFirstResult(1);
			//每页大小
			query.setMaxResults(20);
			query.setString(0, "%王%");
			List<Student> list=query.list();
			for(Student stu: list){
				System.out.println(stu);
			}
		}finally{
			session.close();
		}
	}



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。