public class Classes {
/*班級(jí)ID*/
private int id;
/*班級(jí)名稱*/
private String name;
/*班級(jí)和學(xué)生的關(guān)系*/
private Set students;
//省略setter和getter方法
}
Student.java:
public class Student {
/*學(xué)生ID*/
private int id;
/*學(xué)生姓名*/
private String name;
/*學(xué)生和班級(jí)的關(guān)系*/
private Classes classes;
//省略setter和getter方法
}
Classes.hbm.xml:
<?xml version="1.0"?>
Student.hbm.xml:
<?xml version="1.0"?>
1.查詢單一屬性:
/*返回結(jié)果集屬性列表,元素類型和實(shí)體類中的屬性類型一致*/
List students = session.createQuery("select name from Student").list();
/*遍歷*/
for (Iterator iter=students.iterator(); iter.hasNext();) {
String name = (String)iter.next();
System.out.println(name);
}
/*可以使用別名*/
List students = session.createQuery("select s.id, s.name from Student s").list();
/*遍歷*/
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + ", " + obj[1]);
}
5.查詢實(shí)體對(duì)象:
/*返回的是實(shí)體對(duì)象類型的集合*/
List students = session.createQuery("from Student").list();
/*遍歷*/
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
注:查詢實(shí)體可以直接使用from 類名的形式。
/*使用select就必須使用別名*/
List students = session.createQuery("select s from Student s").list();
/*遍歷*/
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
/*根據(jù)條件進(jìn)行查詢(這里通常都使用別名,比較方便 )*/
List students = session.createQuery("select s.id, s.name from Student s where s.name like '%0%'").list();
/*遍歷*/
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + ", " + obj[1]);
}
/*鏈?zhǔn)骄幊?/
List students = session.createQuery("select s.id, s.name from Student s where s.name like ?")
.setParameter(0, "%0%")
.list();
/*遍歷*/
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + ", " + obj[1]);
}
注:可以通過占位符的形式進(jìn)行傳參,這種方式可以防止SQL注入。
9.自定義參數(shù)的形式:
/*鏈?zhǔn)骄幊?/
List students = session.createQuery("select s.id, s.name from Student s where s.name like :myname")
.setParameter("myname", "%0%")
.list();
/*對(duì)象數(shù)組*/
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + ", " + obj[1]);
}
注:like :myname冒號(hào)后面是沒有空格的,否則會(huì)出錯(cuò)。
10.查詢條件為in的形式:
[java] view plain copy
/*采用in的方式,形參只要一個(gè)即可*/
List students = session.createQuery("select s.id, s.name from Student s where s.id in(:ids)")
.setParameterList("ids", new Object[]{1, 2, 3, 4, 5})
.list();
/*遍歷*/
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + ", " + obj[1]);
}
/*分頁查詢,setFirstResult(1)表示從第一條數(shù)據(jù)開始查詢;setMaxResult(2)表示每頁顯示2條數(shù)據(jù)*/
List students = session.createQuery("from Student")
.setFirstResult(1)
.setMaxResults(2)
.list();
/*遍歷*/
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
14.導(dǎo)航查詢
/*導(dǎo)航查詢,s.classes.name從學(xué)生導(dǎo)航到班級(jí)在導(dǎo)航到班級(jí)名稱(這是從多的一端導(dǎo)航到少的一端,反過來也可以)*/
List students = session.createQuery("from Student s where s.classes.name like '%2%'")
.list();
/*遍歷*/
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
/*查詢語句*/
String hql = "select c.name, count(s) from Classes c join c.students s group by c.name order by c.name";
List students = session.createQuery(hql).list();
/*遍歷*/
for (int i=0; i