Hibernate 学习之Query查询(HQL查询)
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查询
取得查询语句:
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()); } }
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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' } |
这种方式我是不会用的
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?