Hibernate--使用setProperties()方法动态生成
来源:互联网 发布:sql与数据库系统的关系 编辑:程序博客网 时间:2024/05/17 02:51
在Hibernate中可以使用setProperties()方法,将命名参数与一个对象的属性值绑定在一起!
查询工资>1000,职位是CLERK,雇佣日期从1980-10-1 到1983-10-1?
--sql语句如下:
select * from emp where sal>1000 and job='CLERK' and hiredate>to_date('1980-12-01','yyyy-MM-dd')
and hiredate<to_date('1983-01-1','yyyy-MM-dd')
1)封装对象(有4个属性)
/** * 封装条件 * @author Administrator * */public class EmpCondition {private String jobs;private Double salary;private Date hiredateStart;private Date hiredateEnd;public String getJobs() {return jobs;}public void setJobs(String jobs) {this.jobs = jobs;}public Double getSalary() {return salary;}public void setSalary(Double salary) {this.salary = salary;}public Date getHiredateStart() {return hiredateStart;}public void setHiredateStart(Date hiredateStart) {this.hiredateStart = hiredateStart;}public Date getHiredateEnd() {return hiredateEnd;}public void setHiredateEnd(Date hiredateEnd) {this.hiredateEnd = hiredateEnd;}}2)使用这个实体类
@Testpublic void list6() {// 1.获得会话对象Session session = HibernateUtil.currentSession();// 条件对象EmpCondition condition = new EmpCondition();condition.setJobs("CLERK");condition.setSalary(1000.0);condition.setHiredateStart(Tool.strOrDate("1980-12-01", "yyyy-MM-dd"));condition.setHiredateEnd(Tool.strOrDate("1983-01-01", "yyyy-MM-dd"));// 2sqlStringBuffer hql = new StringBuffer("from Emp e where 1=1");// 判断当前对象是否为空if (null != condition.getSalary()) {hql.append(" and e.sal>:salary");}if (null != condition.getJobs()) {hql.append(" and e.job=:jobs");}if (null != condition.getHiredateStart()) {hql.append(" and e.hiredate>:hiredateStart");}if (null != condition.getHiredateEnd()) {hql.append(" and e.hiredate<:hiredateEnd");}// 3.获得Query对象Query query = session.createQuery(hql.toString());// 动态生成/////////////////query.setProperties(condition);// //////////////////////// 4.获得list对象List<Emp> list = query.list();// 5.遍历数据for (Emp emp : list) {System.out.println(emp.getEmpno() + "\t" + emp.getEname() + "\t"+ emp.getJob() + "\t" + emp.getSal() + "\t"+ emp.getHiredate());}// 6.将会话关闭session.close();}3)可以根据具体的条件(可以是1个或2个或3个或4个等等)来查询数据。
0 0
- Hibernate--使用setProperties()方法动态生成
- Hibernate的Query的setProperties(Map map)方法的使用
- BeanUtils使用总结 setProperties
- BeanUtils使用总结 setProperties
- BeanUtils使用总结 setProperties
- hibernate 动态生成表
- hibernate 动态生成表
- hibernate 动态生成表
- hibernate 动态生成HQL
- hibernate动态生成SQL
- hibernate主键生成方法
- 生成hibernate model方法
- hibernate之动态生成SQL
- Hibernate注解方法使用主键生成策略@GeneratedValue(三)
- TreeView动态生成方法!
- 使用指针动态生成内表的方法
- 使用gcc生成动态链接库dll的方法
- jQuery使用on()方法给动态生成元素绑定事件
- Android Studio系列教程六--导入jar包
- 编译报错处理:error C4996
- Linux技术——lsof命令详解
- Content hugging和Content compression resistance
- NodeJs【自学笔记】1
- Hibernate--使用setProperties()方法动态生成
- 时间转换
- HTML 样式
- php curl函数请求链接获得结果
- python调用matlab的m自定义函数
- iOS开发性能优化———UITableView的优化
- 关于Hadoop HDFS数据均衡。
- iOS开发中app之间的跳转
- LeetCode 43 - Multiply Strings