一 對象導(dǎo)航查詢
- 根據(jù)id查詢某個(gè)客戶夏志,再查詢這個(gè)客戶里面所有的聯(lián)系人
查詢某個(gè)客戶里面所有聯(lián)系人過程,使用對象導(dǎo)航實(shí)現(xiàn)
實(shí)現(xiàn)代碼
@Test
public void testSelect1(){
Transaction tx = null;
try{
Session session = HibernateUtils.getSessionObject();
tx= session.beginTransaction();
//根據(jù)cid=1客戶脐雪,再查詢這個(gè)客戶里面所有聯(lián)系人
Customer customer= session.get(Customer.class,1);
//在查詢這個(gè)客戶里面所有聯(lián)系人
//直接得到客戶里面聯(lián)系人的set集合
Set<LinkMan> linkman = customer.getSetlinkMan();
System.out.println(linkman.size());
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
二 OID查詢
- 根據(jù)id查詢某一條記錄,返回對象
根據(jù)id查詢記錄(調(diào)用session里面的get方法)
三 hql查詢
- Query對象,寫hql語句實(shí)現(xiàn)
1 hql: hibernate query language 提供一種查詢語言寡具,hql語言和普通sql很相似
區(qū)別:普通sql操作數(shù)據(jù)庫和字段,hql操作實(shí)體類和屬性
2 常用hql語句
(1)查詢所有:from 實(shí)體類名稱
(2)條件查詢:from 實(shí)體類名稱 where 屬性名稱 = ?
3 使用hql操作時(shí)候稚补,使用query對象
(1)創(chuàng)建Query對象童叠,寫hql語句
(2)調(diào)用query對象里面的方法得到結(jié)果
4 使用
(1)查詢所有
需求:查詢所有的客戶記錄
@Test
public void testSelect1(){
Transaction tx =null;
try{
Session session= HibernateUtils.getSessionObject();
tx= session.beginTransaction();
//1 創(chuàng)建Query對象
Query query= session.createQuery("from entity.Customer");
//2 調(diào)用方法得到結(jié)果
List<Customer> list= query.list();
for(Customer customer :list){
System.out.println(customer.getCid()+":"+customer.getCustName());
}
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
(2)條件查詢
hql 條件查詢語句寫法
from 實(shí)體類名稱 where 實(shí)體類屬性名稱=? and 實(shí)體類屬性名稱=?
from 實(shí)體類名稱 where 實(shí)體類屬性名稱 like ?
where 條件查詢
@Test
public void testSelect2(){
Transaction tx =null;
try{
Session session= HibernateUtils.getSessionObject();
tx= session.beginTransaction();
//1創(chuàng)建query對象
Query query= session.createQuery("from entity.Customer customer where customer.cid = ? and customer.custName=?");
//2 設(shè)置條件值 向课幕?里面設(shè)置值
//setParameter
// 第一個(gè)參數(shù):int類型是厦坛?位置,乍惊?位置從0開始
//第二個(gè)參數(shù)是具體值
//設(shè)置第一個(gè)杜秸?值
query.setParameter(0,2);
//設(shè)置第二個(gè)?值
query.setParameter(1,"百度");
//3 調(diào)用方法得到結(jié)果
List<Customer> list = query.list();
for (Customer customer:list){
System.out.println(customer.getCid()+":"+customer.getCustName());
}
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
like模糊查詢
Query query= session.createQuery("from entity.Customer customer where customer.custName like ?");
query.setParameter(0,"%百%");
List<Customer> list = query.list();
for (Customer customer:list){
System.out.println(customer.getCid()+":"+customer.getCustName());
}
(3)排序查詢
hql排序語句寫法
from 實(shí)體類名稱 order by 實(shí)體類屬性名稱 asc/desc
@Test
public void testSelect4(){
Transaction tx =null;
try{
Session session= HibernateUtils.getSessionObject();
tx= session.beginTransaction();
Query query= session.createQuery("from entity.Customer customer order by customer.cid desc");
List <Customer> list= query.list();
for (Customer customer : list){
System.out.println(customer.getCid()+":"+customer.getCustName());
}
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
(4)分頁查詢
- 使用關(guān)鍵字limit實(shí)現(xiàn)(第一個(gè)參數(shù):從哪里開始润绎,第二個(gè)參數(shù):每頁顯示幾條數(shù)據(jù))
- 在hql中實(shí)現(xiàn)分頁(在hql操作中撬碟,在語句里面不能寫limit,hibernate的query對象封裝兩個(gè)方法實(shí)現(xiàn)分頁操作)
@Test
public void testSelect5(){
Transaction tx =null;
try{
Session session= HibernateUtils.getSessionObject();
tx= session.beginTransaction();
//1 創(chuàng)建對象
Query query= session.createQuery("from entity.Customer");
//2 設(shè)置分頁數(shù)據(jù)
//2.1 設(shè)置開始位置
query.setFirstResult(0);
//2.2 設(shè)置每頁記錄數(shù)
query.setMaxResults(1);
//3 調(diào)用方法得到結(jié)果
List<Customer> list= query.list();
for (Customer customer :list){
System.out.println(customer.getCid()+":"+customer.getCustName());
}
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
(5)投影查詢
- 投影查詢: 查詢不是所有字段值莉撇,而是部分字段值
- 投影查詢hql語句寫法
select 實(shí)體類屬性名稱1 ,實(shí)體類屬性名稱2 from 實(shí)體類名稱
select 后面不能寫*呢蛤,不支持
@Test
public void testSelect6(){
Transaction tx =null;
try{
Session session= HibernateUtils.getSessionObject();
tx= session.beginTransaction();
//1 創(chuàng)建對象
Query query= session.createQuery("select custName from entity.Customer ");
List<Object> list= query.list();
for (Object object :list){
System.out.println(object);
}
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
(6)聚集函數(shù)使用
- 常用的聚集函數(shù)
- count
- sum
- avg
- max
- min
@Test
public void testSelect7(){
Transaction tx =null;
try{
Session session= HibernateUtils.getSessionObject();
tx= session.beginTransaction();
Query query=session.createQuery("select count(*) from entity.Customer");
Object object=query.uniqueResult();
System.out.println(object);
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
四 QBC查詢
- 使用hql查詢需要寫hql語句實(shí)現(xiàn),但是使用qbc時(shí)候棍郎,不需要寫語句了其障,使用方法實(shí)現(xiàn)
- 使用qbc時(shí)候,操作實(shí)體類和屬性
- 使用qbc涂佃,使用Criteria對象
1 查詢所有
(1) 創(chuàng)建Criteria對象
(2)調(diào)用方法得到結(jié)果
@Test
public void testSelect1(){
Transaction tx = null;
try{
Session session = HibernateUtils.getSessionObject();
tx = session.beginTransaction();
//1.創(chuàng)建Criteria對象
Criteria criteria= session.createCriteria(Customer.class);
//2.調(diào)用方法得到結(jié)果
List<Customer> customers=criteria.list();
for(Customer customer :customers){
System.out.println(customer.getCid()+":"+customer.getCustName());
}
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
2 條件查詢(沒有語句励翼,通過方法實(shí)現(xiàn))
@Test
public void testSelect2(){
Transaction tx = null;
try{
Session session = HibernateUtils.getSessionObject();
tx = session.beginTransaction();
//1 創(chuàng)建對象
Criteria criteria= session.createCriteria(Customer.class);
//2 使用Criteria對象里面的方法設(shè)置條件值
//首先使用add方法,表示設(shè)置條件值
//在add方法里面使用類的方法實(shí)現(xiàn)條件設(shè)置
//類似于cid=1
criteria.add(Restrictions.eq("cid",1));
criteria.add(Restrictions.eq("custName","歐亞學(xué)院"));
List<Customer> list = criteria.list();
for(Customer customer :list){
System.out.println(customer.getCid()+":"+customer.getCustName());
}
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
模糊查詢
@Test
public void testSelect3(){
Transaction tx = null;
try{
Session session = HibernateUtils.getSessionObject();
tx = session.beginTransaction();
Criteria criteria= session.createCriteria(Customer.class);
criteria.add(Restrictions.like("custName","%百%"));
List<Customer> list = criteria.list();
for(Customer customer :list){
System.out.println(customer.getCid()+":"+customer.getCustName());
}
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
3 排序查詢
@Test
public void testSelect4(){
Transaction tx = null;
try{
Session session = HibernateUtils.getSessionObject();
tx = session.beginTransaction();
Criteria criteria= session.createCriteria(Customer.class);
criteria.addOrder(Order.desc("cid"));
List<Customer> list = criteria.list();
for(Customer customer :list){
System.out.println(customer.getCid()+":"+customer.getCustName());
}
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
4 分頁查詢
@Test
public void testSelect5(){
Transaction tx = null;
try{
Session session = HibernateUtils.getSessionObject();
tx = session.beginTransaction();
Criteria criteria= session.createCriteria(Customer.class);
// 設(shè)置分頁數(shù)據(jù)
//設(shè)置開始位置
criteria.setFirstResult(0);
//每頁顯示記錄數(shù)
criteria.setMaxResults(1);
List<Customer> list = criteria.list();
for(Customer customer :list){
System.out.println(customer.getCid()+":"+customer.getCustName());
}
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
開始位置計(jì)算公示:(當(dāng)前頁-1)* 每頁記錄數(shù)
5 統(tǒng)計(jì)查詢
@Test
public void testSelect6(){
Transaction tx = null;
try{
Session session = HibernateUtils.getSessionObject();
tx = session.beginTransaction();
Criteria criteria= session.createCriteria(Customer.class);
//設(shè)置操作
criteria.setProjection(Projections.rowCount());
//調(diào)用方法得到結(jié)果
Object object= criteria.uniqueResult();
System.out.println(object);
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
6 離線查詢(和session沒有關(guān)聯(lián))
servlet調(diào)用service巡李,servrice調(diào)用dao
(1)帶dao里面對數(shù)據(jù)庫crud操作
(2)在dao里面使用hibernate框架抚笔,使用hibernate框架時(shí)候,調(diào)用session里面的方法
@Test
public void testSelect7(){
Transaction tx = null;
try{
Session session = HibernateUtils.getSessionObject();
tx = session.beginTransaction();
//Criteria criteria= session.createCriteria(Customer.class);
//1 創(chuàng)建對象
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
// 最終在執(zhí)行的時(shí)候才需要session
Criteria criteria= detachedCriteria.getExecutableCriteria(session);
List<Customer> list = criteria.list();
for (Customer customer : list){
System.out.println(customer.getCid()+":"+customer.getCustName());
}
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
五 本地sql查詢
1 mysql里面多表查詢
(1)內(nèi)連接
/*內(nèi)連接查詢*/
SELECT * FROM t_customer c,t_linkman l WHERE c.cid = l.clid;
SELECT * FROM t_customer c INNER JOIN t_linkman l ON c.cid=l.clid;
(2)左外連接
/*左外連接*/
SELECT * FROM t_customer c LEFT OUTER JOIN t_linkman l ON c.cid=l.clid;
(3)右外連接
/*右外連接*/
SELECT * FROM t_customer c RIGHT OUTER JOIN t_linkman l ON c.cid=l.clid;
2 HQL多表查詢
(1)內(nèi)連接
內(nèi)連接查詢hql語句寫法(以客戶和聯(lián)系人為例)
from Customer c inner join c.setLinkMan
@Test
public void testSelect1(){
Transaction tx = null;
try {
Session session= HibernateUtils.getSessionObject();
tx = session.beginTransaction();
Query query = session.createQuery("from entity.Customer c inner join c.setlinkMan");
List list= query.list();
for (Object object:list){
System.out.println(object.toString());
}
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
返回list侨拦,list里面每部分都是數(shù)組形式
(2)左外連接
from Customer c left outer join c.setLinkMan
左外鏈接返回list中每部分是數(shù)組
(3)右外連接
from Customer c right outer join c.setLinkMan
右外鏈接返回list中每部分是數(shù)組
(4)迫切內(nèi)連接
- 迫切內(nèi)連接和內(nèi)連接底層實(shí)現(xiàn)一樣的
- 區(qū)別:使用內(nèi)連接返回list中每部分是數(shù)組殊橙,迫切內(nèi)連接返回list每部分是對象
from entity.Customer c inner join fetch c.setlinkMan
@Test
public void testSelect2(){
Transaction tx = null;
try {
Session session= HibernateUtils.getSessionObject();
tx = session.beginTransaction();
Query query = session.createQuery("from entity.Customer c inner join fetch c.setlinkMan");
List list = query.list();
tx.commit();
}catch (Exception e){
e.printStackTrace();
tx.rollback();
}
}
(5)迫切左外連接
from Customer c left outer join fetch c.setLinkMan
迫切左外鏈接返回每部分是對象
- SQLQuery對象,使用普通sql實(shí)現(xiàn)查詢