Hibernate查詢方式:
- 1.導航對象圖檢索方式:根據(jù)已有對象導航到其他對象
- 2.OID檢索:對象OID檢索對象
- 3.HQL檢索:使用面向?qū)ο驢QL查詢語言
- 4.QBC檢索:使用QBC API來檢索對象
- 5.本地SQL檢索:使用SQL語言
HQL檢索有如下功能:
- 1設(shè)定查詢條件
- 2檢索部分屬性
- 3支持分頁查詢
- 5支持鏈接查詢
- 6支持分組查詢拥娄,可以使用HAVING和GROUP BY
- 7提供內(nèi)置函數(shù)
- 8支持子查詢
- 9支持動態(tài)綁定參數(shù)
- 10能夠使用用戶SQL函數(shù)
Hibernate基本邏輯代碼
public static void test() {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();// 配置文件configure()
SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
//這里寫查詢方法
test(session);
transaction.commit();
session.close();
sessionFactory.close();
}
查詢的持久化對象和表
//員工
public class Employee {
private Integer id;
private String name;
private float salary;
private String email;
private Department dept;
public Employee() {
super();
}
public Employee(String name, String email) {
super();
this.name = name;
this.email = email;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + ", email=" + email + ", dept=" + dept
+ "]";
}
}
//部門
public class Department {
private Integer id;
private String name;
private Set<Employee> employees=new HashSet<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
@Override
public String toString() {
return "Department [id=" + id + ", name=" + name + ", employees=" + employees + "]";
}
}
此處省略映射文件(一對多關(guān)系)
HQL檢索步驟
1.通過Session 的createQuery()方法獲取Query對象 它包涵一個HQL語句和參數(shù)
2.動態(tài)綁定參數(shù)
3.調(diào)用Query執(zhí)行方法
/**
* 第一次查詢
*/
public static void queryA(Session session) {
// 1.創(chuàng)建Query對象
String hql = "FROM Employee e WHERE e.salary > ? AND e.email LIKE ? ";
Query query = session.createQuery(hql);
// 2.綁定參數(shù)
query.setParameter(0, 5255.4f).setParameter(1, "%%");
// 3.執(zhí)行查詢
List<Employee> eList = query.list();
System.out.println(eList.size());
}
HQL可以直接使用持久化類的類名 和屬性名
命名參數(shù):取代"?"為查詢條件命名
/**
* 命名參數(shù)
* */
public static void queryB(Session session) {
// 1.創(chuàng)建Query對象
String hql = "FROM Employee e WHERE e.salary > :salary AND e.email LIKE :email ";
Query query = session.createQuery(hql);
// 2.綁定參數(shù)
query.setParameter("salary", 5255.4f).setParameter("email", "%%");
// 3.執(zhí)行查詢
List<Employee> eList = query.list();
System.out.println(eList.size());
}
實體參數(shù) :HQL是面向?qū)ο蟮?/h5>
/**
* 實體參數(shù)
* */
public static void queryC(Session session) {
// 1.創(chuàng)建Query對象
String hql = "FROM Employee e WHERE e.salary > ? AND e.dept = ? ";
Query query = session.createQuery(hql);
// 2.綁定參數(shù)
Department e=new Department();
e.setId(1);
query.setParameter(0, 5255.4f).setParameter(1, e);
// 3.執(zhí)行查詢
List<Employee> eList = query.list();
System.out.println(eList.size());
}
ORDER BY 排序
/**
* ORDER BY 排序
* */
public static void queryD(Session session) {
// 1.創(chuàng)建Query對象
String hql = "FROM Employee e WHERE e.dept = ? ORDER BY e.salary DESC";
Query query = session.createQuery(hql);
// 2.綁定參數(shù)
Department e=new Department();
e.setId(1);
query.setParameter(0, e);
// 3.執(zhí)行查詢
List<Employee> eList = query.list();
for (Employee employee : eList) {
System.out.println(employee.getName());
}
}
分頁查詢
/**
* 分頁查詢
* */
public static void queryF(Session session) {
String hql = "FROM Employee e WHERE e.id > ?";
Query query = session.createQuery(hql);
query.setParameter(0, 3);
int pageNo=0;//分頁起始位置包涵
int pageSize=2;//每頁長度
List<Employee> eList = query.setFirstResult(pageNo).setMaxResults(pageSize).list();
for (Employee employee : eList) {
System.out.println(employee.getName());
}
}
命名查詢 hql放在映射文件中
/**
* 實體參數(shù)
* */
public static void queryC(Session session) {
// 1.創(chuàng)建Query對象
String hql = "FROM Employee e WHERE e.salary > ? AND e.dept = ? ";
Query query = session.createQuery(hql);
// 2.綁定參數(shù)
Department e=new Department();
e.setId(1);
query.setParameter(0, 5255.4f).setParameter(1, e);
// 3.執(zhí)行查詢
List<Employee> eList = query.list();
System.out.println(eList.size());
}
/**
* ORDER BY 排序
* */
public static void queryD(Session session) {
// 1.創(chuàng)建Query對象
String hql = "FROM Employee e WHERE e.dept = ? ORDER BY e.salary DESC";
Query query = session.createQuery(hql);
// 2.綁定參數(shù)
Department e=new Department();
e.setId(1);
query.setParameter(0, e);
// 3.執(zhí)行查詢
List<Employee> eList = query.list();
for (Employee employee : eList) {
System.out.println(employee.getName());
}
}
/**
* 分頁查詢
* */
public static void queryF(Session session) {
String hql = "FROM Employee e WHERE e.id > ?";
Query query = session.createQuery(hql);
query.setParameter(0, 3);
int pageNo=0;//分頁起始位置包涵
int pageSize=2;//每頁長度
List<Employee> eList = query.setFirstResult(pageNo).setMaxResults(pageSize).list();
for (Employee employee : eList) {
System.out.println(employee.getName());
}
}
在映射文件中hibernate-mapping元素下
<query name="salaryEmps"><![CDATA[FROM Employee e WHERE e.salary > :minSal AND e.salary < :maxSal]]></query>
/**
* 命名查詢 hql放在映射文件中
* */
public static void queryG(Session session) {
String hql = "FROM Employee e WHERE e.id > ?";
Query query = session.getNamedQuery("salaryEmps");
query.setParameter("minSal", 5000f);
query.setParameter("maxSal", 20000f);
List<Employee> eList = query.list();
for (Employee employee : eList) {
System.out.println(employee.getName());
}
}
投影查詢 查詢部分屬性 默認返回數(shù)組
返回數(shù)組
/**
* 投影查詢 查詢部分屬性 默認返回數(shù)組
* */
public static void queryH(Session session) {
String hql = "SELECT e.email, e.name FROM Employee e WHERE e.dept = :dept";
Query query = session.createQuery(hql);
Department e=new Department();
e.setId(1);
query.setParameter("dept", e);
List<Object[]> eList = query.list();
for (Object[] os : eList) {
System.out.println(Arrays.asList(os));
}
}
返回對象
/**
* 投影查詢 查詢部分屬性 默認返回數(shù)組
* */
public static void queryH(Session session) {
String hql = "SELECT e.email, e.name FROM Employee e WHERE e.dept = :dept";
Query query = session.createQuery(hql);
Department e=new Department();
e.setId(1);
query.setParameter("dept", e);
List<Object[]> eList = query.list();
for (Object[] os : eList) {
System.out.println(Arrays.asList(os));
}
}
報表查詢 使用函數(shù)
/**
* 報表查詢
* */
public static void queryJ(Session session) {
//持久化類提供這樣的構(gòu)造器
String hql = "SELECT min(e.salary), max(e.salary) FROM Employee e GROUP BY e.dept HAVING min(salary)> :minsal";
Query query = session.createQuery(hql);
query.setParameter("minsal", 1000f);
List<Object[]> eList = query.list();
for (Object[] os : eList) {
System.out.println(Arrays.asList(os));
}
}
本地sql:可以執(zhí)行sql所有操作
/**
* 本地sql
* 可以執(zhí)行sql所有操作
* */
public static void queryL(Session session) {
String sql = "INSERT INTO department (name) VALUES(:name)";
Query query = session.createNativeQuery(sql);//使用createNativeQuery方法
query.setParameter("name", "本地sql").executeUpdate();
}