1. 程式人生 > >Hibernate--動態查詢

Hibernate--動態查詢

//dao層

package com.kgc.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.kgc.common.HibernateSessionFactory;
import com.kgc.po.Emp;

public class DeptDao  {
	
	
	public List findByNamed(Object[] args){
		return HibernateSessionFactory.getSession()
				.createCriteria(Emp.class)//from emp
				//and
				.add(Restrictions.eq("id", 123))   				//等於
				.add(Restrictions.gt("hiredate", "1985-4-1"))	//大於
				.add(Restrictions.ge("hiredate", "1985-4-1"))	//大於等於
				.add(Restrictions.lt("hiredate", "1985-4-1"))	//小於
				.add(Restrictions.le("hiredate", "1985-4-1"))	//小於等於
				
				//or
				.add(Restrictions.or(Restrictions.eq("id", 123), Restrictions.gt("hiredate", "1985-4-1")))
				//可以拼多個or
				.add(Restrictions.disjunction()
					.add(Restrictions.eq("id", 123))
					.add(Restrictions.gt("hiredate", "1985-4-1"))
					.add(Restrictions.le("hiredate", "1985-4-1"))
				)
				//各種條件
				.add(Restrictions.between("birthday", "1985-4-1", "1989-4-1"))	//生日在1985-4-1至1989-4-1之間
				.add(Restrictions.like("name", "張",MatchMode.START))	// 查詢名字姓張的  張%
			//	.add(Restrictions.like("name", "張",MatchMode.EXACT))	// 查詢名字姓張的  張  類似於等值
				.add(Restrictions.ilike("name", "a"))	// 模糊查詢,不區分大小寫
				.add(Restrictions.in("id", args))
				.add(Restrictions.isEmpty("emps")) //例如:Dept.emps  為空
				.add(Restrictions.isNotEmpty("emps"))  //非空
				.add(Restrictions.isNull("sal"))	   //  sal is null
				.add(Restrictions.isNotNull("sal"))	  //sal is not null
				.add(Restrictions.sizeEq("emps", 5))   //編號等於5的員工
				.add(Restrictions.ne("id", 5))			//id不為5的員工
				//.add(Restrictions.eqProperty("", ""))	//兩個屬性相比要相等
				.list();
	}
	/**如何查詢符合以下條件的員工資訊
	 * 職位是工程師	如:job='engineer'
	 * 工資大於2000元,如:salary>2000
	 * 入職時間在1980-12-17至1987-12-17之間
	 */
	@SuppressWarnings("unchecked")
	public List<Emp> findEmps(Map<String, Object> args){
		List<Emp> list = null;
			 Criteria c = HibernateSessionFactory.getSession().createCriteria(Emp.class);
			c.add(Restrictions.eq("job", args.get("job")));
			if (args.get("sal")!=null) 
			c.add(Restrictions.gt("sal", args.get("sal")));
			if (args.get("begin")!=null) 
			c.add(Restrictions.between("hiredate", args.get("begin"), args.get("end")));
			list = c.list();
			System.out.println("姓名\t薪資\t入職時間");
			for (Emp emp : list) {
				System.out.println(emp.getEname()+"\t"+emp.getSal()+"\t"+emp.getHiredate().toLocaleString());
			}
			return list;
	}

}

//biz

package com.kgc.biz;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import com.kgc.common.HibernateSessionFactory;
import com.kgc.dao.DeptDao;

public class DeptBiz {
	private DeptDao dao = new DeptDao();
	public void findEmp(){
		Transaction tx = null;
		try {
			tx=HibernateSessionFactory.getSession().beginTransaction();
			Map<String, Object> m  = new HashMap<String, Object>();
			m.put("job", "CLERK");
			m.put("sal", 950.00);
			try {
				m.put("begin", DateFormat.getDateInstance().parse("1980-12-17"));
				m.put("end", DateFormat.getDateInstance().parse("1987-12-17"));
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			dao.findEmps(m);
			tx.commit();
		} catch (HibernateException e1) {
			e1.printStackTrace();
			if(tx!=null){
				tx.rollback();
			}
		}
		
	}
}

//test

package com.kgc.test;


import com.kgc.biz.DeptBiz;

public class DeptTest {
	public static void main(String[] args) {
		DeptBiz biz = new DeptBiz(); 
		biz.findEmp();
		
		
	}

}

 //執行結果


Hibernate: 
    select
        this_.EMPNO as EMPNO0_0_,
        this_.ENAME as ENAME0_0_,
        this_.JOB as JOB0_0_,
        this_.MGR as MGR0_0_,
        this_.HIREDATE as HIREDATE0_0_,
        this_.SAL as SAL0_0_,
        this_.COMM as COMM0_0_,
        this_.DEPTNO as DEPTNO0_0_ 
    from
        EMP this_ 
    where
        this_.JOB=? 
        and this_.SAL>? 
        and this_.HIREDATE between ? and ?
姓名	薪資	入職時間
ADAMS	1100.0	1987-5-23 0:00:00
MILLER	1300.0	1982-1-23 0:00:00