HQL实用技术 您所在的位置:网站首页 hql语句查询的语法 HQL实用技术

HQL实用技术

2023-03-10 09:03| 来源: 网络整理| 查看: 265

Hibernate查询

Hibernate支持3种查询方式,分别是HQL查询,Criteria查询,原生SQL查询

一.HQL查询 (一)什么是HQL

1.HQL:hibernate query language (Hibernate查询语言) 2. 语法和SQL很像,但是它是面向对象的查询语言,不是面向数据库 3. 除了Java类和属性的名称外,hql语句对大小写并不敏感 4. hql里面查询的是实体对象,所以写的是实体类的类名和属性名,严格区分大小写 5. HQL语句查询的是实体类的名称,不是数据库的表 6. HQL语句中的类名和属性名严格区分大小写

执行HQL语句的步骤 获取Session对象编写HQL语句创建Query对象执行查询,得到查询结果 list()方法查询 //查询部门所有列表 public class App { public static void main(String[] args) { // 1.读取hibernate.cfg.xml配置文件 Configuration configuration = new Configuration().configure(); // 2.创建一个会话工厂 SessionFactory sessionFactory = configuration.buildSessionFactory(); // 3.打开一个会话 Session session = sessionFactory.openSession(); // 4.查询数据 //定义HQL语句 String hql = "from Department"; //查询的是类名称,不是表名 //创建Query对象 Query query = session.createQuery(hql); //执行查询,得到结果集 List list = query.list(); //循环遍历 for (Department department : list) { System.out.println(department); } // 5.关闭session session.close(); } } iterate()方法查询 //查询部门所有列表 public class TestIterator { public static void main(String[] args) { Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); //开启事务 Transaction transaction = session.beginTransaction(); //查询数据 String hql = "from Department"; Query query = session.createQuery(hql); //执行查询,得到结果集(会发出1+N条SQL语句) Iterator itor = query.iterate(); while (itor.hasNext()) { //获取部门对象 Department department = itor.next(); System.out.println(department); } transaction.commit(); session.close(); } }

list()方法和iterate()方法的区别:

list()方法不会发出1+n条SQL语句 (list用的比较多)iterate()方法会发出1+n条SQL语句,在有缓存的基础上性能较好 HQL语句参数绑定

添加员工类Employee和员工映射文件 在这里插入图片描述 在resources下创建mapper包并在Employee.hbm.xml内配置

在hibernate.cfg.xml内加载映射文件在这里插入图片描述 在HQL语句中,给参数绑定数据可以使用以下2种方法,分别是:

按参数位置绑定按参数名称绑定 高版本HQL参数绑定 观察HQL语句中的占位符?1,?2,其中?1是HQL语句中的第1个占位符参数下标从1开始 /** * 查询某个部门下的员工信息 */ public class QueryEmployee { public static void main(String[] args) { Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); //查询数据,根据参数位置赋值,进行查询 //定义HQL语句 HQL语句中的占位符?1,?2,其中?1是HQL语句中的第1个占位符 String hql = "from Employee where deptNo=?1 and empName like ?2"; Query query = session.createQuery(hql); query.setParameter(1, 1); query.setParameter(2, "%a%"); List list = query.list(); for (Employee employee : list) { System.out.println(employee); } session.close(); } } 按参数名称绑定

HQL语句中可以使用 :参数名称 的方式进行传参,其中参数名称自定义

public class QueryEmployee2 { public static void main(String[] args) { Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); //查询,根据参数名称进行复制 HQL语句中使用 :参数名称 的方式进行传参,其中参数名称自定义 String hql = "from Employee where empName like :empName and salary >=:salary"; Query query = session.createQuery(hql); query.setParameter("empName", "%a%"); query.setParameter("salary", 6000.0); List list = query.list(); for (Employee employee : list) { System.out.println(employee); } session.close(); } } Hibernate中的动态条件查询

setXXX(): 针对具体数据类型

setXXX(int position, XXX value)setXXX(String name, XXX value)

setParamenter(): 任意类型参数

setParameter( int position, Object value)setParameter( String name, Object value)

setProperties(): 专为命名参数定制

根据条件动态查询员工数据 1.根据职位查询,如:job=“Java工程师” 2.根据薪资查询,如:salary>=4000 3.入职时间 2018年1月1日 到 2020年1月1日 注意:条件个数不确定 说先自定义一个查询条件类(EmployeeVo)继承Employee 生成get和set方法 在这里插入图片描述

在hbrj下创建一个util包DateUtil类 用于转换时间日期

public static Date parseDate(String str) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { return format.parse(str); } catch (ParseException e) { e.printStackTrace(); } return null; } }

开始写测试类

/** * 根据条件动态查询员工数据 * 1.根据职位查询,如:job="Java工程师" * 2.根据薪资查询,如:salary>=4000 * 3.入职时间 2018年1月1日 到 2020年1月1日 */ public class QueryEmployee3 { public static void main(String[] args) { Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); StringBuilder hql = new StringBuilder("from Employee where 1=1 "); //动态条件查询 //定义一个查询条件的类对象 EmployeeVo employeeVo = new EmployeeVo(); employeeVo.setJob("%员%"); employeeVo.setSalary(5500.0); employeeVo.setStartDate(DateUtil.parseDate("2023-3-2 00:00:00")); employeeVo.setEndDate(DateUtil.parseDate("2023-3-9 00:00:00")); //EmployeeVo实体类中的成员变量名必须跟参数名称一致。 if (employeeVo.getJob() != null) { //判断职位 hql.append("and job like :job "); //参数名称必须与实体类的属性名相同 } if (employeeVo.getSalary() != null) { //判断薪资 hql.append("and salary >=:salary "); } if (employeeVo.getEmpName() != null) { //判断名字 hql.append("and empName like :empName "); } if (employeeVo.getStartDate() != null) { //判断开始日期 hql.append("and hireDate >=:startDate "); } if (employeeVo.getEndDate() != null) { //判断结束日期 hql.append("and hireDate


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有