HQL的参数绑定

按参数位置绑定 setXXX()方法第一个参数从0下标开始

 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>?

按参数名称绑定 命名参数以":"开头而且setXXX()方法第一个参数和:后面的字符串对应

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()方法用来绑定任意类型的参数,当不便指定参数的具体类型时,可以使用setParameter()为参数赋值

 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()方法绑定命名参数与一个对象的属性值(命名参数要和属性值对应)

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>=?

动态设置查询参数 根据用户实际输入的参数确定查询条件

  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=?
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ORACLE自学教程 --create tabletestone ( id number, --序号usernam...
    落叶寂聊阅读 4,839评论 0 0
  • 引出 •请思考如下问题? –查询所有员工的每个月工资总和,平均工资? –查询工资最高和最低的工资是多少? –查询公...
    C_cole阅读 12,042评论 0 3
  • mysql数据库中 :database : 文件夹table : 数据表(数据文件) 进入mysqlmysql -...
    赋闲阅读 3,690评论 0 0
  • 5.多表查询 多表查询 目的:从多张表获取数据 前提:进行连接的多张表中有共同的列 等连接 通过两个表具有相同意义...
    乔震阅读 5,293评论 0 0
  • Oracle SQL基本操作 Oracle数据库基本操作 1.概述 Oracle数据库客户端一般需要安装在服务器上...
    横竖撇捺啊阅读 3,570评论 0 1

友情链接更多精彩内容