HQL的參數(shù)綁定

按參數(shù)位置綁定 setXXX()方法第一個參數(shù)從0下標(biāo)開始

 public List<Emp> selectEmp(String job,double salary)
    {
        String hql="select emp from Emp as emp where emp.job=? and emp.salary>?";
        return this.getCurrentSession().createQuery(hql)
                .setString(0,job)
                .setDouble(1,salary)
                .list();
    }

Hibernate:
select
emp0_.empNo as empNo1_,
emp0_.empName as empName1_,
emp0_.job as job1_,
emp0_.salary as salary1_
from
project.Emp emp0_
where
emp0_.job=?
and emp0_.salary>?

按參數(shù)名稱綁定 命名參數(shù)以":"開頭而且setXXX()方法第一個參數(shù)和:后面的字符串對應(yīng)

String hql="select emp from Emp as emp where emp.job=:job and emp.salary>:salary";
        return this.getCurrentSession().createQuery(hql)
                .setString("job",job)
                .setDouble("salary",salary)
                .list();

Hibernate:
select
emp0_.empNo as empNo1_,
emp0_.empName as empName1_,
emp0_.job as job1_,
emp0_.salary as salary1_
from
project.Emp emp0_
where
emp0_.job=?
and emp0_.salary>?

Hibernate提供setParameter()方法用來綁定任意類型的參數(shù),當(dāng)不便指定參數(shù)的具體類型時,可以使用setParameter()為參數(shù)賦值

 public List<Emp> selectEmp(Object[] condition)
    {
        String hql="select emp from Emp as emp where emp.job=? and emp.salary>?";
        Query query=this.getCurrentSession().createQuery(hql);
        if (condition!=null&&condition.length>0)
        {
            for (int i=0;i<condition.length;i++)
            {
                query.setParameter(i,condition[i]);
            }
        }
        return query.list();
    }

Hibernate:
select
emp0_.empNo as empNo1_,
emp0_.empName as empName1_,
emp0_.job as job1_,
emp0_.salary as salary1_
from
project.Emp emp0_
where
emp0_.job=?
and emp0_.salary>?

public List<Emp> selectEmp(Map<String, Object> conditions) {
        String hql = "select emp from Emp as emp where emp.job=:job and emp.salary>:salary";
        Query query = this.getCurrentSession().createQuery(hql);
        if (conditions != null && conditions.size() > 0) {
           /* Iterator<Map.Entry<String, Object>> iter = conditions.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry<String, Object> entry = iter.next();
                String key = entry.getKey();
                Object value = entry.getValue();
                query.setParameter(key, value);
            }*/
           /* for (String key : conditions.keySet()) {
                query.setParameter(key, conditions.get(key));
            }*/
            for (Map.Entry<String, Object> entry : conditions.entrySet()) {
                query.setParameter(entry.getKey(), entry.getValue());
            }
           /* Iterator<String> iterator = conditions.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                Object value = conditions.get(key);
                query.setParameter(key, value);
            }*/
        }
        return query.list();
    }

Hibernate:
select
emp0_.empNo as empNo1_,
emp0_.empName as empName1_,
emp0_.job as job1_,
emp0_.salary as salary1_
from
project.Emp emp0_
where
emp0_.job=?
and emp0_.salary>?

setProperties()方法綁定命名參數(shù)與一個對象的屬性值(命名參數(shù)要和屬性值對應(yīng))

package com.dto;

public class EmpForm {
    private String job;
    private double salary;

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public EmpForm() {

    }

    public EmpForm(String job, double salary) {
        this.job = job;
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "EmpForm{" +
                "job='" + job + '\'' +
                ", salary=" + salary +
                '}';
    }
}

public List<Emp> selectEmp(EmpForm empForm)
      {
          String hql="select emp from Emp emp where emp.job=:job and emp.salary>=:salary";
          Query query=this.getCurrentSession().createQuery(hql);
          query.setProperties(empForm);
          return query.list();
      }

Hibernate:
select
emp0_.empNo as empNo1_,
emp0_.empName as empName1_,
emp0_.job as job1_,
emp0_.salary as salary1_
from
project.Emp emp0_
where
emp0_.job=?
and emp0_.salary>=?

動態(tài)設(shè)置查詢參數(shù) 根據(jù)用戶實際輸入的參數(shù)確定查詢條件

  public List<Emp> selectEmp(EmpForm empForm) {
        StringBuilder hql = new StringBuilder("select emp from Emp emp where 1=1");
        if (empForm.getJob() != null && empForm.getJob().length() > 0) {

            hql.append(" and emp.job=:job");
        }
        if (empForm.getSalary() > 0) {
            hql.append(" and emp.salary>=salary");
        }
        Query query = this.getCurrentSession().createQuery(hql.toString());
        query.setProperties(empForm);
        return query.list();
    }

Hibernate:
select
emp0_.empNo as empNo1_,
emp0_.empName as empName1_,
emp0_.job as job1_,
emp0_.salary as salary1_

from
    project.Emp emp0_ 
where
    1=1 
    and emp0_.job=?
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ORACLE自學(xué)教程 --create tabletestone ( id number, --序號usernam...
    落葉寂聊閱讀 1,252評論 0 0
  • 引出 ?請思考如下問題? –查詢所有員工的每個月工資總和,平均工資? –查詢工資最高和最低的工資是多少? –查詢公...
    C_cole閱讀 7,392評論 0 3
  • mysql數(shù)據(jù)庫中 :database : 文件夾table : 數(shù)據(jù)表(數(shù)據(jù)文件) 進(jìn)入mysqlmysql -...
    賦閑閱讀 641評論 0 0
  • 5.多表查詢 多表查詢 目的:從多張表獲取數(shù)據(jù) 前提:進(jìn)行連接的多張表中有共同的列 等連接 通過兩個表具有相同意義...
    喬震閱讀 1,549評論 0 0
  • Oracle SQL基本操作 Oracle數(shù)據(jù)庫基本操作 1.概述 Oracle數(shù)據(jù)庫客戶端一般需要安裝在服務(wù)器上...
    橫豎撇捺啊閱讀 603評論 0 1

友情鏈接更多精彩內(nèi)容