Hibernate之HQL数据库操作
生活随笔
收集整理的這篇文章主要介紹了
Hibernate之HQL数据库操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文件結構
1. 實體類 實例 Employee
package com.bdqn.entity;import java.util.Date;public class Employee {private Integer empNo; //員工編號private String empName; //員工姓名private String job; // 職位private Double salary; // 薪資private Integer deptNo; // 部門編號private Date hireDate; // 入職時間// public Employee(String empName,String job, Double salary,Integer deptNo,Date hireDate){ // this.empName = empName; // this.job = job; // this.salary = salary; // this.deptNo = deptNo; // this.hireDate = hireDate; // }public Integer getEmpNo() {return empNo;}public String getEmpName() {return empName;}public String getJob() {return job;}public Double getSalary() {return salary;}public Integer getDeptNo() {return deptNo;}public Date getHireDate() {return hireDate;}public void setEmpNo(Integer empNo) {this.empNo = empNo;}public void setEmpName(String empName) {this.empName = empName;}public void setJob(String job) {this.job = job;}public void setSalary(Double salary) {this.salary = salary;}public void setDeptNo(Integer deptNo) {this.deptNo = deptNo;}public void setHireDate(Date hireDate) {this.hireDate = hireDate;}@Overridepublic String toString() {return "Employee{" +"empNo=" + empNo +", empName='" + empName + '\'' +", job='" + job + '\'' +", salary=" + salary +", deptNo=" + deptNo +", hireDate=" + hireDate +'}';} }2. utils類 將字符串轉換成 Date 日期格式
文件名:DateUtil.class
package com.bdqn.utils;import java.text.SimpleDateFormat; import java.util.Date; import java.text.ParseException;public class DateUtil {/*** 將字符創轉換成 Date 日期格式* @param str* @return* */public static Date formatDate(String str){try{SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");return format.parse(str);} catch (ParseException e){e.fillInStackTrace();}return null;}}3. 拓展實體 EmployeeVo
com.xxx.vo.EmployeeVo.class
自動會生成沒有的 字段
4. 增
5. 刪
6. 改
6.1
/** * 按位置進行參數綁定 * 注意:高版本hibernate下標從1開始 * */ @Test public void testParamter1(){// 獲取SessionSession session = HibernateUtil.getSession();// 定義HQL語句String hql = "from Employee where empName like ?1 and salary >= ?2";// 創建Query對象Query query = session.createQuery(hql);// 賦值 // query.setString(1,"%國%"); // query.setDouble(1,5000);// 推薦使用 setParameter()query.setParameter(1,"%國%");query.setParameter(2,5000.0);// 執行查詢List<Employee> list = query.list();for (Employee employee : list) {System.out.println(employee);} }6.2
/**** */@Testpublic void testParamter2(){// 獲取SessionSession session = HibernateUtil.getSession();// 定義HQL語句String hql = "from Employee where empName like :empName and salary >= :salary";// 創建Query對象Query query = session.createQuery(hql);// 賦值 // query.setString(1,"%國%"); // query.setDouble(1,5000);// 推薦使用 setParameter()query.setParameter("empName","%國%");query.setParameter("salary",5000.0);// 執行查詢List<Employee> list = query.list();for (Employee employee : list) {System.out.println(employee);}}7. 查
7.1 list()
/** * 查詢所有員工列表 * */ @Test public void testList(){// 獲取Session(使用openSession()方式獲取)Session session = HibernateUtil.getSession();try{// 定義HQl語句 查詢的是實體類類名String hql = "from Employee";// 創建Query對象Query query = session.createQuery(hql);// 執行查詢List<Employee> list = query.list();// 循環遍歷for (Employee employee : list) { // 快捷鍵: iterSystem.out.println(employee);}} catch (Exception e){e.printStackTrace();} finally {// 關閉資源HibernateUtil.closeSession(session);} }7.2 iterate()
/** * 查詢所有員工列表 * */ @Test public void testIternate(){// 獲取Session(使用openSession()方式獲取)Session session = HibernateUtil.getSession();try{// 定義HQl語句 查詢的是實體類類名String hql = "from Employee";// 創建Query對象Query query = session.createQuery(hql);// 執行查詢Iterator<Employee> iterate = query.iterate(); // 會發出1+N田sql語句//循環遍歷while(iterate.hasNext()) {// 得到每一個員工對象Employee employee = iterate.next();System.out.println(employee);}} catch (Exception e){// 關閉資源HibernateUtil.closeSession(session);} }7.3 動態條件查詢
/** * 動態條件查詢 * */ @Test public void testEmployee(){// 創建查詢條件類對象EmployeeVo employeeVo = new EmployeeVo();employeeVo.setJob("python程序員");employeeVo.setStartDate(DateUtil.formatDate("2018-01-01"));employeeVo.setEndDate(DateUtil.formatDate("2021-01-01"));// 獲取 SessionSession session = HibernateUtil.getSession();// 定義 sql 語句StringBuilder hql = new StringBuilder("from Employee where 1=1 ");// 判斷條件是否為空if(employeeVo != null){// 薪資if(employeeVo.getSalary() != null){hql.append(" and salary >= :salary"); // 命名參數必須與實體類的屬性名相同}// 職位if(employeeVo.getJob() != null && employeeVo.getJob().equals("")){hql.append(" and job = :job"); // 命名參數必須與實體類的屬性名相同}// 開始時間if(employeeVo.getStartDate() != null){hql.append(" and hireDate >= startDate");}// 結束時間if(employeeVo.getEndDate() != null){hql.append(" and hireDate <= :endDate");}}// 創建 query 對象Query query = session.createQuery(hql.toString());// 賦值query.setProperties(employeeVo);//要求:必須保證命名擦書名稱與實體類的屬性名相同// 執行查詢List<Employee> list = query.list();for (Employee employee : list) {System.out.println(employee);}}總結
以上是生活随笔為你收集整理的Hibernate之HQL数据库操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hibernate 基本配置文件+基本增
- 下一篇: SQLAlchemy 常用基本表