DetachedCriteria离线查询离线查询---实现模糊查询 Hibernate

DetachedCriteria dc = DetachedCriteria.forClass(Emp.class);
		addJob(dc, job);
		addSal(dc, salLow, salHeight);
		addDeptno(dc, deptno);
		List emps = empDAO.findByCriteria(dc);
		request.setAttribute("empList", emps);
-----------------------------------------------------------------------
	private void addJob(DetachedCriteria dc, String job) {
		if(job != null && !job.equals("")) {
			dc.add(Restrictions.eq("job", job));
		}
	}
	
	private void addSal(DetachedCriteria dc, String sallow, String salheight) {
		if(sallow != null && !sallow.equals("") && salheight != null && !salheight.equals("")){
			dc.add(Restrictions.ge("sal", Double.parseDouble(sallow)));
			dc.add(Restrictions.le("sal", Double.parseDouble(salheight)));
		}
	}
	
	private void addDeptno(DetachedCriteria dc, String deptno) {
		if(deptno != null && !deptno.equals("")) {
			dc = dc.createCriteria("dept");
			dc.add(Restrictions.eq("deptno", Long.parseLong(deptno)));
		}
	}





---------------------------------------
public List findByCriteria(DetachedCriteria dc){
		Session session = HibernateUtil.openSession();
		Criteria c = dc.getExecutableCriteria(session);	
		return c.list();
	}	
--------------------
Hibernate二级缓存技术
1,首先在hibernate.cfg.xml中配置

	30
	50
	
	true
	org.hibernate.cache.HashtableCacheProvider
	
	
	
	
	
2,在到每个查询语句中设置
public List findAllDname(){
		Session session = HibernateUtil.openSession();
		Query query = session.createQuery("select distinct dname from Dept");
		query.setCacheable(true);//缓存设置
		List result = query.list();
		
		return result;
	}	


 

你可能感兴趣的:(重要代码)