日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Hibernate之HQL数据库操作

發(fā)布時(shí)間:2025/5/22 66 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hibernate之HQL数据库操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文件結(jié)構(gòu)

1. 實(shí)體類 實(shí)例 Employee

package com.bdqn.entity;import java.util.Date;public class Employee {private Integer empNo; //員工編號(hào)private String empName; //員工姓名private String job; // 職位private Double salary; // 薪資private Integer deptNo; // 部門編號(hào)private Date hireDate; // 入職時(shí)間// 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類 將字符串轉(zhuǎn)換成 Date 日期格式

文件名:DateUtil.class

package com.bdqn.utils;import java.text.SimpleDateFormat; import java.util.Date; import java.text.ParseException;public class DateUtil {/*** 將字符創(chuàng)轉(zhuǎn)換成 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. 拓展實(shí)體 EmployeeVo

com.xxx.vo.EmployeeVo.class
自動(dòng)會(huì)生成沒有的 字段

package com.bdqn.vo;import com.bdqn.entity.Employee;import java.util.Date; import java.util.zip.DataFormatException;/*** 自定義查詢類* 該類制作查詢條件的參數(shù)(方法參數(shù)),不做方法的返回值類型* */ public class EmployeeVo extends Employee {// 開始時(shí)間private Date startDate;// 結(jié)束時(shí)間private Date endDate;public Date getStartDate() {return startDate;}public Date getEndDate() {return endDate;}public void setStartDate(Date startDate) {this.startDate = startDate;}public void setEndDate(Date endDate) {this.endDate = endDate;} }

4. 增

5. 刪

6. 改

6.1

/** * 按位置進(jìn)行參數(shù)綁定 * 注意:高版本hibernate下標(biāo)從1開始 * */ @Test public void testParamter1(){// 獲取SessionSession session = HibernateUtil.getSession();// 定義HQL語句String hql = "from Employee where empName like ?1 and salary >= ?2";// 創(chuàng)建Query對(duì)象Query query = session.createQuery(hql);// 賦值 // query.setString(1,"%國%"); // query.setDouble(1,5000);// 推薦使用 setParameter()query.setParameter(1,"%國%");query.setParameter(2,5000.0);// 執(zhí)行查詢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";// 創(chuàng)建Query對(duì)象Query query = session.createQuery(hql);// 賦值 // query.setString(1,"%國%"); // query.setDouble(1,5000);// 推薦使用 setParameter()query.setParameter("empName","%國%");query.setParameter("salary",5000.0);// 執(zhí)行查詢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語句 查詢的是實(shí)體類類名String hql = "from Employee";// 創(chuàng)建Query對(duì)象Query query = session.createQuery(hql);// 執(zhí)行查詢List<Employee> list = query.list();// 循環(huán)遍歷for (Employee employee : list) { // 快捷鍵: iterSystem.out.println(employee);}} catch (Exception e){e.printStackTrace();} finally {// 關(guān)閉資源HibernateUtil.closeSession(session);} }

7.2 iterate()

/** * 查詢所有員工列表 * */ @Test public void testIternate(){// 獲取Session(使用openSession()方式獲取)Session session = HibernateUtil.getSession();try{// 定義HQl語句 查詢的是實(shí)體類類名String hql = "from Employee";// 創(chuàng)建Query對(duì)象Query query = session.createQuery(hql);// 執(zhí)行查詢Iterator<Employee> iterate = query.iterate(); // 會(huì)發(fā)出1+N田sql語句//循環(huán)遍歷while(iterate.hasNext()) {// 得到每一個(gè)員工對(duì)象Employee employee = iterate.next();System.out.println(employee);}} catch (Exception e){// 關(guān)閉資源HibernateUtil.closeSession(session);} }

7.3 動(dòng)態(tài)條件查詢

/** * 動(dòng)態(tài)條件查詢 * */ @Test public void testEmployee(){// 創(chuàng)建查詢條件類對(duì)象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"); // 命名參數(shù)必須與實(shí)體類的屬性名相同}// 職位if(employeeVo.getJob() != null && employeeVo.getJob().equals("")){hql.append(" and job = :job"); // 命名參數(shù)必須與實(shí)體類的屬性名相同}// 開始時(shí)間if(employeeVo.getStartDate() != null){hql.append(" and hireDate >= startDate");}// 結(jié)束時(shí)間if(employeeVo.getEndDate() != null){hql.append(" and hireDate <= :endDate");}}// 創(chuàng)建 query 對(duì)象Query query = session.createQuery(hql.toString());// 賦值query.setProperties(employeeVo);//要求:必須保證命名擦?xí)Q與實(shí)體類的屬性名相同// 執(zhí)行查詢List<Employee> list = query.list();for (Employee employee : list) {System.out.println(employee);}}

總結(jié)

以上是生活随笔為你收集整理的Hibernate之HQL数据库操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。