Hibernate持久層框架使用【六】HQL語句
除了前面直接呼叫的get等方法查詢資料外,還可以通過編寫HQL語句來更加靈活的查詢資料
例如在資料庫中有一張student表,其中有兩條資料,如下圖
下面使用HQL語句來查詢表中的資料
示例程式碼:
student表:
@Entity(name="student") public class Student { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int sid; private String name; private int age; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
測試程式碼:
public class Hql { public static void main(String[] args) { // TODO Auto-generated method stub MySessionFactory mySessionFactory = new MySessionFactory(); Session session = mySessionFactory.getSession(); Query query = session.createQuery("select f from student as f"); List<Student> students = query.list(); for(Student student : students) { System.out.println(student.getName()); } mySessionFactory.commit(); mySessionFactory.close(); } }
這裡為了方便,使用了前面寫的SessionFactory工具類(MySessionFactory,具體參考前面的第一篇或第二篇部落格)
程式碼中使用了Query query = session.createQuery("select f from student as f");這句程式碼
在createQuery方法中編寫Hql語句select f from student as f,這裡的f表示所有屬性,與Sql語句中的*星號類似,f可以自定義
執行結果顯示查詢出兩條資料的名字
如果需要帶查詢條件進行查詢,可以在後面加入條件,如:
Query query = session.createQuery("select f.name from student as f where f.age=?"); query.setParameter(0, 20); String objs = (String) query.uniqueResult(); System.out.println(objs);
uniqueResult方法表示唯一結果,若結果為多個,可以用list集合接收資料
setParameter(0, 20)其中0代表?的位置,20代表注入的值
如果需要查詢某一個屬性,可以這樣寫
Query query = session.createQuery("select f.name from student as f");
List<String> names = query.list();
for(String name : names)
{
System.out.println(names);
}
因為查詢出來的結果是String型別的List集合,所以需要用List<String>來接資料
當需要某幾個屬性時,就要用Object[]型別來接收了,如:
Query query = session.createQuery("select f.name,f.age from student as f");
List<Object[]> objs = query.list();
for(Object[] obj : objs)
{
System.out.println(obj[0]);
System.out.println(obj[1]);
}
除了直接查詢需要的某幾個屬性外,還可以將查詢的資料轉為特定的格式,如map或list
轉為map格式:
Query query = session.createQuery("select new map(f.name as name,f.age as age) from student as f");
List<Map<Object, Object>> objs = query.list();
for(Map<Object, Object> obj : objs)
{
System.out.println(obj);
}
Hql語句中的f.name as name表示,以name作為key,f.name作為value,類似於給key作別名
轉為list格式:
Query query = session.createQuery("select new list(f.name,f.age) from student as f");
List<Object> objs = query.list();
for(Object obj : objs)
{
System.out.println(obj);
}
分頁查詢:
分頁查詢是專案中最常用到的需求,如何使用HQL語句實現分頁查詢,示例程式碼:
int index = 2;
int size = 1;
Query query = session.createQuery("select f.name from student as f");
query.setFirstResult((index-1)*size);//起始位置
query.setMaxResults(size);//最大結果數
List<Object> objs = query.list();
for(Object obj : objs)
{
System.out.println(obj);
}
index表示當前查詢的頁碼,size表示每一頁的資料數量,在分頁查詢時,只需要修改這兩項即可完成分頁查詢
模糊查詢:
//like
List<Student> students = session.createQuery("select f from student as f where f.name like ?")
.setParameter(0, "%黃%")
.list();
for(Student student : students)
{
System.out.println(student.getName());
}
其他常用函式:
按年齡進行降序排序
Query query = session.createQuery("select f.name from student as f order by f.age desc");
List<Object> objs = query.list();
for(Object obj : objs)
{
System.out.println(obj);
}
求和函式
Query query = session.createQuery("select sum(f.age) from student as f");
Long sum = (Long) query.uniqueResult();
System.out.println(sum);
統計數量函式
Query query = session.createQuery("select count(f.name) from student as f");
Long count = (Long) query.uniqueResult();
System.out.println(count);
求最大值函式
Query query = session.createQuery("select max(f.age) from student as f");
int max = (int) query.uniqueResult();
System.out.println(max);
求最小值函式
Query query = session.createQuery("select min(f.age) from student as f");
int min = (int) query.uniqueResult();
System.out.println(min);
求平均值函式
Query query = session.createQuery("select avg(f.age) from student as f");
Double avg = (Double) query.uniqueResult();
System.out.println(avg);
EJB-QL函式:
求長度函式
//查詢name長度為2的資料
Query query = session.createQuery("select f from student as f where length(f.name) = ?");
query.setParameter(0, 9);
List<Student> students = query.list();
for(Student student : students)
{
System.out.println(student.getName());
}
開方函式
//開方函式
//這裡求開方後等於2的資料
List<Student> students = session.createQuery("select f from student as f where sqrt(f.sid) = ?")
.setParameter(0, 2.0)
.list();
for(Student student : students)
{
System.out.println(student.getName());
}
位長函式
//位長函式
List<Long> longs = session.createQuery("select bit_length(f.name) from student as f")
.list();
for(Long lg : longs)
{
System.out.println(lg);
}
ps:一個位元組為8位,這裡一個字元為3個位元組
取模(取餘)函式
//這裡求age對2取模(取餘)後結果為1的資料
List<Student> students = session.createQuery("select f from student as f where mod(f.age,?) = ?")
.setParameter(0, 2)
.setParameter(1, 1)
.list();
for(Student student : students)
{
System.out.println(student.getAge());
}