第五章 HQL实用技术(2)

2019-03-28 18:28

2.按参数名称绑定

定义命名参数,命名参数以”:”开头 示例4:

修改以上代码如下:

// 设置命名参数

String hql = \; Query query = session.createQuery(hql); // 为命名参数赋值

query.setString(\, \);

二、绑定各种类型的参数

setBoolean() setByte()

setDouble() setData() setString()

都有如下两种重载形式

setString(int position,String val)//按位置绑定参数 setString(String name,String val)//按名称绑定参数 Hibernate还提供了2个特殊的参数绑定方法 1.setParameter()方法:绑定任意类型的参数 //按位置绑定参数

publicclass test {

publicstaticvoid main(String[] args) {

SessionFactory sessionFactory = null; Session session = null; try {

sessionFactory = new Configuration().configure()

.buildSessionFactory();

session = sessionFactory.openSession();

// 设置占位符

String hql = \; Query query = session.createQuery(hql); // 为占位符赋值

query.setParameter(0, \); Iterator it = query.iterate(); if (it.hasNext()) {

System.out.println(\部门地址为 :\ + it.next().getLocation()); }

}

}

}

e.printStackTrace(); if (session != null)

session.close();

} catch (Exception e) { } finally {

//按命名绑定参数 修改以上代码如下:

// 设置命名参数

String hql = \; Query query = session.createQuery(hql); // 为命名参数赋值

query.setParameter(\, \);

2. setProperties()方法:绑定命名参数与一个对象的属性值

(1)封装条件的类DeptCondition,类似于实体类的编写,无需无参构造方法。

package entity;

importjava.util.Date;

publicclass DeptCondition {

private Byte deptNo; private String deptName; private String location;

public Byte getDeptNo() { }

publicvoid setDeptNo(Byte deptNo) { }

public String getDeptName() { }

publicvoid setDeptName(String deptName) {

returndeptName; this.deptNo = deptNo; returndeptNo;

}

}

this.deptName = deptName;

public String getLocation() { }

publicvoid setLocation(String location) { }

this.location = location; returnlocation;

(2)编写测试类

publicclass test { }

/**

* @param args */

publicstaticvoid main(String[] args) { }

SessionFactory sessionFactory = null; Session session = null; try { }

DeptCondition deptCondition=new DeptCondition(); deptCondition.setDeptName(\);

sessionFactory = new Configuration().configure()

.buildSessionFactory();

session = sessionFactory.openSession(); // 设置命名参数

String hql = \Query query = session.createQuery(hql); // 为命名参数符赋值

query.setProperties(deptCondition); List result = query.list(); for (Dept dept:result){

System.out.println(\部门的地址:\+dept.getLocation()); }

e.printStackTrace(); if (session != null)

session.close();

dept.deptName= :deptName\;

} catch (Exception e) { } finally {

三、实现动态查询

在查询条件很多的情况下,传递过多的参数很不方便,可以把参数封装在对象中,使用Query接口的setProperties()方法为HQL中的命名参数赋值。setProperties()方法把对象的属性匹配到命名参数上,需注意命名参数名称要与Java对象的属性匹配。

P146 示例6

(1)新建一个条件类EmpCondition packageentity;

import java.util.Date;

public class EmpCondition { // 员工职位 private String job; // 员工工资 private Double salary; // 员工入职结束时间 private Date hireDateEnd; // 员工入职开始时间 private Date hireDateStart; 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; }


第五章 HQL实用技术(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:老年汗病(自主神经功能紊乱)中医诊疗方案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: