主要內(nèi)容:
- 分頁(yè)查詢【重點(diǎn)】
- 對(duì)象導(dǎo)航查詢【重點(diǎn)】
- 連接查詢【重點(diǎn)】
- 統(tǒng)計(jì)查詢【重點(diǎn)】
- DML風(fēng)格的操作
一嫩码、分頁(yè)查詢
PageQueryTest.java
package cn.itcast.hibernate;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import junit.framework.TestCase;
/**
* 分頁(yè)查詢
*/
public class PageQueryTest extends TestCase {
public void testQuery1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
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());
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
說(shuō)明:方法setFirstResult用來(lái)設(shè)置查詢從哪里開(kāi)始,這里的記錄是從0開(kāi)始辛掠,而不是從1開(kāi)始谢谦。方法setMaxResults用來(lái)設(shè)置查詢出多少條記錄。
二萝衩、對(duì)象導(dǎo)航查詢
ObjectNavQueryTest.java
package cn.itcast.hibernate;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import junit.framework.TestCase;
/**
* 對(duì)象導(dǎo)航查詢
*/
public class ObjectNavQueryTest extends TestCase {
public void testQuery1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
List students = session.createQuery("select s.name from Student s where s.classes.name like '%1%'").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
String name = (String)iter.next();
System.out.println(name);
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
說(shuō)明:可以看到我們找到班級(jí)名稱是使用s.classes.name
這樣的方式回挽,即使用點(diǎn)號(hào)進(jìn)行導(dǎo)航。
三猩谊、連接查詢
JoinQueryTest.java
package cn.itcast.hibernate;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import junit.framework.TestCase;
/**
* 連接查詢
*/
public class JoinQueryTest extends TestCase {
public void testQuery1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
//List students = session.createQuery("select c.name, s.name from Student s join s.classes c").list();
List students = session.createQuery("select c.name, s.name from Student s inner join s.classes c").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
public void testQuery2() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
List students = session.createQuery("select c.name, s.name from Classes c left join c.students s").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
public void testQuery3() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
List students = session.createQuery("select c.name, s.name from Classes c right join c.students s").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
說(shuō)明:
- 1.方法一是使用內(nèi)連接千劈,其中hql語(yǔ)句就是表示查詢出學(xué)生的名字,同時(shí)還是查詢出對(duì)應(yīng)的班級(jí)名字牌捷。注意其語(yǔ)法墙牌。同時(shí)只會(huì)將同時(shí)具備學(xué)生和班級(jí)的數(shù)據(jù)查詢出來(lái),對(duì)于有些沒(méi)有學(xué)生的班級(jí)或者有些沒(méi)有班級(jí)的學(xué)生是不會(huì)查詢出來(lái)的暗甥。
- 2.方法二是使用的外連接中的左連接喜滨。這里表示的意思就是將所有班級(jí)都查詢出來(lái),對(duì)于有學(xué)生的班級(jí)還會(huì)將其中的學(xué)生查詢出來(lái)撤防。
- 3.方法三使用的外連接中的右連接虽风。這里表示將所有學(xué)生都查詢出來(lái),對(duì)于有班級(jí)的學(xué)生還是將其班級(jí)查詢出來(lái)寄月,當(dāng)然沒(méi)有班級(jí)的學(xué)生的班級(jí)就是為null辜膝。注意區(qū)分左和右。
四漾肮、統(tǒng)計(jì)查詢
StatQueryTest.java
package cn.itcast.hibernate;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import junit.framework.TestCase;
/**
* 統(tǒng)計(jì)查詢
*/
public class StatQueryTest extends TestCase {
public void testQuery1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
/*List students =session.createQuery("select count(*) from Student").list();
Long count = (Long)students.get(0);
System.out.println(count);*/
Long count = (Long)session.createQuery("select count(*) from Student").uniqueResult();
System.out.println(count);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
public void testQuery2() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
List students =session.createQuery("select c.name, count(s) from Student s join s.classes c " +
"group by c.name order by c.name").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + ", " + obj[1]);
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
說(shuō)明:
- 1.對(duì)于方法一厂抖,我們注意不要再使用以前那種方式了,直接使用方法uniqueResult即可克懊。
- 2.對(duì)于方法二是結(jié)合了內(nèi)連接查詢忱辅。會(huì)將所有有班級(jí)的學(xué)生數(shù)量都查詢出來(lái),同時(shí)使用班級(jí)名字分組和排序保檐。
五耕蝉、DML風(fēng)格的操作
DMLQueryTest.java
package cn.itcast.hibernate;
import org.hibernate.Session;
import junit.framework.TestCase;
/**
* DML風(fēng)格的操作
*/
public class DMLQueryTest extends TestCase {
public void testQuery1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
session.createQuery("update Student s set s.name=? where s.id < ?")
.setParameter(0, "李四")
.setParameter(1, 5)
.executeUpdate();
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}
說(shuō)明:就是批量操作,盡量少用夜只,因?yàn)槠浜途彺娌煌嚼菰冢簿褪钦f(shuō)當(dāng)我們使用此種操作時(shí),數(shù)據(jù)庫(kù)中的數(shù)據(jù)會(huì)全部變化,但是緩存中的數(shù)據(jù)卻不會(huì)變场躯,這對(duì)于經(jīng)常操作的數(shù)據(jù)來(lái)說(shuō)就會(huì)變成臟數(shù)據(jù)谈为,所以一般對(duì)于相對(duì)于比較靜態(tài)的數(shù)據(jù)使用。