HQL:hibernate query language
在hibernate下查询使用的是HQL;
Classes代表的是班级bean;
HQL语句中是面向类的操作,不是面向表!比如:from Classes,Classes代表的是类,不能小写!
SELECT * FROM classes;
SELECT COUNT(*) FROM classes;
SELECT c.cname ,c.cid FROM classes c;
SELECT c.cname FROM classes c WHERE cid IN(SELECT cid FROM classes WHERE cid<10 );
测试代码:
/**
* 1.查询所有
* sql:select * from classes;
* hql:from Classes;Classes代表的是类;
*/
@Test
public
void testQueryAllClasses(){
Session session =
sessionFactory.openSession();
List<Classes> classesList = session.createQuery(
"from Classes"
).list();
session.close();
}
/**
* 2.带属性的查询
* 得到的list中装的是object[],数组中元素的顺序为 hql元素中的顺序
*/
@Test
public
void testQueryAllClasses_Property(){
Session session =
sessionFactory.openSession();
List list = session.createQuery(
"select cname,cid from Classes"
).list();
session.close();
}
/**
* 3.带属性的查询,将得到的结果封装成bean,在bean中需要提供对应的构造函数,同时也要提供一个空的构造函数!
* 得到的list中装的是classes;
*/
@Test
public
void testQueryAllClasses_Property_Constructor(){
Session session =
sessionFactory.openSession();
List list = session.createQuery(
"select new cn.itheima03.hibernate.domain.Classes(cid,cname) from Classes"
).list();
session.close();
}
/**
* 4.带条件的查询
*/
@Test
public
void testQueryClasses_Condition_1(){
Session session =
sessionFactory.openSession();
Query query = session.createQuery(
"from Classes where cid=?");
//给参数设置值
query.setLong(0, 1L);
Classes classes = (Classes)query.uniqueResult();
//得到的只有一个结果
System.
out.println(classes.getCname());
session.close();
}
/**
* 4
-2:条件查询的另外一种形式
*/
@Test
public
void testQueryClasses_Condition_2(){
Session session =
sessionFactory.openSession();
Query query = session.createQuery(
"from Classes where cid=:cid");
query.setLong(
"cid", 1L);
Classes classes = (Classes)query.uniqueResult();
System.
out.println(classes.getCname());
session.close();
}
/**
* 5.count()函数
*/
@Test
public
void testQueryClasses_Count(){
Session session =
sessionFactory.openSession();
Query query = session.createQuery(
"select count(*) from Classes");
query.uniqueResult();
session.close();
}
/**
* 6.使用子查询:
* sql:select * from classes where cid in(select cid from classes where cid in(1,2,3))
*/
@Test
public
void testQueryClasses_SubSelect(){
Session session =
sessionFactory.openSession();
Query query = session.createQuery(
"from Classes where cid in(select cid from Classes where cid in(1,2,3))");
System.
out.println(query.list().size());
session.close();
}
/**
* 7.子查询在select中出现
*/
@Test
public
void testQueryClasses_SubSelect_Select(){
Session session =
sessionFactory.openSession();
Query query = session.createQuery(
"select c.cname from Classes c where cid in(select cid from Classes where cid in(1,2,3))");
query.list();
session.close();
}
/**
* 8.当查询结果的数据量太大时,可以使用setMaxResult控制获取结果的数据量;
*/
@Test
public
void testQueryClasses_MaxSize(){
Session session =
sessionFactory.openSession();
Query query = session.createQuery(
"from Classes");
query.setMaxResults(5);
//可以利用该方法设置query的最大结果集的数量
System.
out.println(query.list().size());//5
session.close();
}
/**
* 9.分页
* sql:select * from classes limit 0,3;
* query.setFirstResult( int begin);//从哪条记录开始
* query.setMaxResults( int pagesize );//获取的数量
*/
@Test
public
void testQueryClasses_DisPage(){
Session session =
sessionFactory.openSession();
Query query = session.createQuery(
"from Classes");
/**
* 下标是0,第一个数据
*/
query.setFirstResult(0);
query.setMaxResults(3);
//1,2,3
List<Classes> classesList = query.list();
for(Classes classes:classesList){
System.
out.println(classes.getCid());
}
}
|