1. 程式人生 > >Hibernate=====HQL實用技術

Hibernate=====HQL實用技術

cnblogs 三種 blog 原生sql pan 類名 select stl onf

Hibernate支持三種查詢語言:HQL查詢、Criteria查詢和原生SQL查詢

HQL(hibernate Query Language,hibernate查詢語言)是一種面向對象查詢語言,其中沒有表和字段的概念,只有類、對象和屬性的概念

語法:

  form子句:form +全類名 / form + 類名(查詢所有)

  select子句:select dept.deptname from Dept

  where子句:from dept where deptName=‘sales‘;

HQL執行語句

   例子:form子句、select子句、where子句以及參數查詢

    建立實體類

  

public class Dept {
    private Integer deptno;
    private String dname;

    public Dept() {
    }

    public Dept(Integer deptno, String dname) {
        this.deptno = deptno;
        this.dname = dname;
    }

    public Integer getDeptno() {
        return deptno;
    }

    
public void setDeptno(Integer deptno) { this.deptno = deptno; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } }

測試方法

package cn;

import cn.happy.entity.Dog;
import cn.hib.entity.Dept;
import org.hibernate.Query;
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Before; import org.junit.Test; import java.util.List; /** * Created by a on 2017/8/20. */ public class Test924 { Configuration cfg; SessionFactory factory; Session session; Transaction tx; @Before public void beforeTest(){ //創建配置對象 cfg=new Configuration().configure(); //根據配置對象創建SessonFactory factory=cfg.buildSessionFactory(); //根據SessionFactory創建Session session=factory.getCurrentSession(); //session=factory.openSession(); //開啟事務 tx=session.beginTransaction(); } @Test public void testlist(){ String hql="from Dept"; Query query=session.createQuery(hql); List<Dept> list=query.list(); for (Dept dept : list){ System.out.println(dept.getDname()); } } @Test public void selectSomeColumnFromTable(){ String hql="select dept.deptno,dept.dname from Dept"; Query query=session.createQuery(hql); List<Object[]>list=query.list(); for(Object[] obj : list){ for (Object child : obj){ System.out.println(child); } } } @Test public void selectSomesColumnFromTable(){ String hql="select new Dept(dept.deptno,dept.dname) from Dept dept"; Query query=session.createQuery(hql); List<Dept> list=query.list(); for (Dept dept : list){ System.out.println(dept.getDname()); } } @Test public void selectSomeRecordsFromTable(){ String hql="from dept.deptno,dept.dname from Dept dept where dept.dname=‘RESEARCH‘"; Query query=session.createQuery(hql); List<Dept> list=query.list(); for (Dept dept : list){ System.out.println(dept.getDname()); } } //案例:參數查詢: 方案一:匿名占位符“?” @Test public void selectByConditionNiming(){ String hql="from Dept dept where dname=?"; Query query=session.createQuery(hql); query.setParameter(0,"RESEARCH"); List<Dept> list=query.list(); for(Dept dept : list){ System.out.println(dept.getDname()); } } //案例:參數查詢:方案二: @Test public void selectByConditionParametername(){ String hql="from Dept dept where dname=:dname"; Query query=session.createQuery(hql); query.setParameter("dname","RESEARCH"); List<Dept> list=query.list(); for(Dept dept : list){ System.out.println(dept.getDname()); } } //案例:參數查詢:方案三:name 參數名稱綁定++++對象屬性 @Test public void selectByConditionParameternameAndObjectAttribute(){ String hql="from Dept dept where dname=:dname"; Dept dd=new Dept(); dd.setDname("RESEARCH"); Query query=session.createQuery(hql); query.setProperties(dd); List<Dept> list=query.list(); for(Dept dept : list){ System.out.println(dept.getDname()); } } }

Hibernate=====HQL實用技術