Hibernate 学习之Query查询(HQL查询)

时间:2023-12-30 19:47:38
package com.itcloud.test;

import com.itcloud.pojo.Dept;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.junit.After;
import org.junit.Test;
import java.util.List; public class TestHibernate {
public static SessionFactory sessionFactory =null ;
public static Session session = null ;
public static Transaction transaction ;
static {
Configuration configuration = new Configuration().configure() ;
sessionFactory = configuration.buildSessionFactory() ;
session = sessionFactory.openSession() ;
transaction = session.beginTransaction() ;
}
@Test
public void post(){
String hql = "from Dept as d" ; //as请别名
//通过session创建quer查询
Query<Dept> query = session.createQuery(hql,Dept.class) ;
    //上面的语句也可以这样写:Query query = session.createQuery(hql) ;
     List<Dept> allDept = query.list(); System.out.print(allDept); 
}
@After public void destroy(){ transaction.commit(); session.close(); sessionFactory.close(); } }

*******************进行模糊查询

 @Test
public void post(){
String hql = "from Dept as d where d.loc like ?" ; //as请别名
//通过session创建quer查询
Query<Dept> query = session.createQuery(hql,Dept.class) ;
//设置参数
query.setParameter(0,"%"+3+"%") ; //hibernate的参数是从零开始的
List<Dept> allDept = query.list();
System.out.print(allDept); }

*************进行分页查询:

  @Test
public void post(){
String column="loc" ; //表示模糊查询列 ;
String keyword="" ;//空字符串表示查询全部
int currentPage = 1 ;//当前页码,第一页
int lineSize =2 ;//每页显示两条数据 ;
String hql = "From Dept as d where d."+column+" like ?" ; //注意点,Dept必须对应着实体类
//通过session创建quer查询
Query<Dept> query = session.createQuery(hql,Dept.class) ;
//设置参数
query.setParameter(0,"%"+keyword+"%") ; //hibernate的参数是从零开始的 /**
在查询之前进行分页操作
查询行数还是从0开始
* 第1页从0行开始,第二页从(2-1)*2=2行开始,...
* 动态的根据页码的变化,跟新查询的数据
*/
query.setFirstResult((currentPage-1)*lineSize) ;//设置开始行
query.setMaxResults(lineSize) ; //每页查询lineSize行数据
//上面的语句就表示,从第一行开始查询,每次查询2行
List<Dept> allDept = query.list();
System.out.print(allDept); }

查询结果

Hibernate:
select
dept0_.deptno as deptno1_0_,
dept0_.dname as dname2_0_,
dept0_.loc as loc3_0_
from
Dept dept0_
where
dept0_.loc like ? limit ?

********统计查询:

    @Test
public void post(){
String hql = "select count(deptno) From Dept as d" ; //注意点,Dept必须对应着实体类
Query query = session.createQuery(hql) ;
System.out.println(query.uniqueResult());
}

注意点:

query.uniqueResult()返回是的数据类型是Long,可以通过以下的方法转换成整型
Long num = (Long)query.uniqueResult() ;
System.out.println(num.intValue());
******在*.hbm.xml文件中配置Query查询

Hibernate 学习之Query查询(HQL查询)

取得查询语句:

       Query query = session.getNamedQuery("findById") ;
query.setParameter(0,4);//设置参数可以不考虑数据类型
News vo =(News)query.uniqueResult() ; //查询出单个数据
System.out.println(vo);
HibernateSessionFactory.closeSession();

*********query进行数据的更新操作

    @Test
public void post(){
String hql = "update Dept set dname=? where deptno=?" ;
Query query = session.createQuery(hql) ;
//设置参数
query.setParameter(0,"更新部门操作") ;
query.setParameter(1,1) ;
int x = query.executeUpdate() ; //执行更新语句返回的是影响的行数 ;
System.out.print(x);
}

*********删除数据操作

   String hql = "delete from News where nid=?" ;
Query query = session.createQuery(hql);
query.setParameter(0, 6);
int len = query.executeUpdate() ;//进行数据的更新返回的是影响的行数
HibernateSessionFactory.getSession().beginTransaction().commit();//更新操作必须要进行数据的提交
 System.out.println(len);

*********使用iterator取得查询结果

 @Test
public void post(){
String hql = "from Dept as d" ; //as请别名
//通过session创建quer查询
Query<Dept> query = session.createQuery(hql,Dept.class) ;
//上面的语句也可以这样写:Query query = session.createQuery(hql) ;
Iterator<Dept> iter = query.iterate();
while(iter.hasNext()){
System.out.println(iter.next());
}
}

结果:

Hibernate:
select
dept0_.deptno as col_0_0_
from
Dept dept0_
Hibernate:
select
dept0_.deptno as deptno1_0_0_,
dept0_.dname as dname2_0_0_,
dept0_.loc as loc3_0_0_
from
Dept dept0_
where
dept0_.deptno=?
Dept{deptno=1, dname='更新部门操作', loc='403'}
Hibernate:
select
dept0_.deptno as deptno1_0_0_,
dept0_.dname as dname2_0_0_,
dept0_.loc as loc3_0_0_
from
Dept dept0_
where
dept0_.deptno=?
Dept{deptno=2, dname='运营部门', loc='411'}
Hibernate:
select
dept0_.deptno as deptno1_0_0_,
dept0_.dname as dname2_0_0_,
dept0_.loc as loc3_0_0_
from
Dept dept0_
where
dept0_.deptno=?
Dept{deptno=3, dname='设计部门', loc='401'}

  这种方式我是不会用的