HQL(动态查询,分页查询)

Posted 苦逼的人生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HQL(动态查询,分页查询)相关的知识,希望对你有一定的参考价值。

动态查询
   Session session;
Transaction tx;
@Before
public void mybefor() {
//1构建配置对象COnfiguration
Configuration cfg = new Configuration().configure("hibernate02.cfg.xml");
//2.构建工厂SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
//3.构建回话Session
session = factory.openSession();//从连接池中随机取出连接
//4.开始事务
tx = session.beginTransaction();
}
@Test
public void RunSelectDept(){
String hql = "select new Dept(d.dno,d.dname,d.loc) from Dept d";
Query query = session.createQuery(hql);
List <Dept>list = query.list();
for (Dept dept: list) {
System.out.println(dept.getDname());
}
}
@Test
public void RunSelectDept2(){
String hql = "from Dept d where d.dname=? and d.loc=?";
Query query = session.createQuery(hql);
query.setParameter(0,"LENK");
query.setParameter(1,"CHINA");
List <Dept>list = query.list();
for (Dept dept: list) {
System.out.println(dept.getDname());
}
}
@Test
public void RunSelectDept3(){
String hql = "from Dept d where d.dname=:dname and d.loc=:loc";
Query query = session.createQuery(hql);
query.setParameter("dname","LENK");
query.setParameter("loc","CHINA");
List <Dept>list = query.list();
for (Dept dept: list) {
System.out.println(dept.getDname());
}
}
@After
public void myafter(){
tx.commit();
session.close();
}

以上是关于HQL(动态查询,分页查询)的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate之HQL检索(查询)方式

Hibernate(十三):HQL查询

HQL分页查询 对象导航查询外置命名查询连接查询查询过滤器统计查询

SQL分页和HQL分页查询

HQL实用技术

Hibernate检索(上)