Hibernate查询

普通无条件查询:

DAO:

/*public List findAll(){
		String hql="from Emp";//定义hql语句
		Query query=currentSession().createQuery(hql);//构建Query对象
		return query.list();//执行查询
	}*/
	
	public Iterator findAll(){//返回迭代器
		String hql="from Emp";//定义hql语句
		Query query=currentSession().createQuery(hql);//构建Query对象
		return query.iterate();//执行查询
	}

Service:

/*public List findAllEmps(){
		Transaction tx=null;
		List empList=null;
		try {
			tx=empDao.currentSession().beginTransaction();
			empList=empDao.findAll();
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if(tx!=null){
				tx.rollback();
			}
		}
		return empList;
	}*/
	
	public Iterator findAllEmps(){
		Transaction tx=null;
		Iterator empList=null;
		try {
			tx=empDao.currentSession().beginTransaction();
			empList=empDao.findAll();
			Emp emp=null;
			while(empList.hasNext()){
				emp=empList.next();
				System.out.println(emp.geteName());
			}
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if(tx!=null){
				tx.rollback();
			}
		}
		return empList;
	}

Test:

	/*	List empList=new EmpService().findAllEmps();
		for (Emp emp : empList) {
			System.out.println(emp.geteName());
		}*/
		
		new EmpService().findAllEmps();

原生多条件查询:

DAO:

	/*public List findByConditions(Object[] conditions){
		//查询依赖多个条件,且类型各异
		String hql="from Emp where job=? and sal>?";
		Query query=currentSession().createQuery(hql);
		if(conditions!=null&&conditions.length>0){
			for (int i = 0; i < conditions.length; i++) {
				query.setParameter(i, conditions[i]);
			}
		}
		return query.list();
	}*/
	
	public List findByConditions(Emp conditions){
		//查询依赖多个条件,且类型各异
		String hql="from Emp where job=:job and sal>:sal";
		Query query=currentSession().createQuery(hql);
		//根据命名参数的名称,从conditions中获取相应的属性进行赋值
		query.setProperties(conditions);
		return query.list();
	}
Service:

/*	public List findEmpsByConditions(Object[] conditions){
		Transaction tx=null;
		List empList=null;
		try {
			tx=empDao.currentSession().beginTransaction();
			empList=empDao.findByConditions(conditions);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if(tx!=null){
				tx.rollback();
			}
		}
		return empList;
	}*/
	public List findEmpsByConditions(Emp conditions){
		Transaction tx=null;
		List empList=null;
		try {
			tx=empDao.currentSession().beginTransaction();
			empList=empDao.findByConditions(conditions);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if(tx!=null){
				tx.rollback();
			}
		}
		return empList;
	}

Test:

		/*Object[] conditions={"CLERK",700};
		List empList=new EmpService().findEmpsByConditions(conditions);
		for (Emp emp : empList) {
			System.out.println(emp.geteName());
		}*/
		
		Emp conditions=new Emp();
		conditions.setJob("CLERK");
		conditions.setSal(700);
		List empList=new EmpService().findEmpsByConditions(conditions);
		for (Emp emp : empList) {
			System.out.println(emp.geteName());
		}

封装后多条件动态查询:

查询条件封装:

/**
 * 封装查询条件,实现动态查询
 * @author 30869
 *
 */
public class EmpCondition {
	private Integer empNo;//编号
	private String eName;//姓名
	private String job;//工种
	private Date hireDate;//入职日期
	public Integer getEmpNo() {
		return empNo;
	}
	public void setEmpNo(Integer empNo) {
		this.empNo = empNo;
	}
	public String geteName() {
		return eName;
	}
	public void seteName(String eName) {

日期格式化工具类:

**
 * 工具类
 * @author 30869
 *
 */
public class Tool {
	
	/**
	 * 日期转换方法java.lang.String-->java.util.Date
	 * @param dateStr
	 * @param pattern
	 * @return
	 * @throws Exception
	 */
	public static Date strToDate(String dateStr,String pattern) throws Exception{
		SimpleDateFormat format=new SimpleDateFormat(pattern);//设置日期格式
		return format.parse(dateStr);
	} 
}

DAO:

public List findByConditions(String hql,EmpCondition conditions){
		return currentSession().createQuery(hql).setProperties(conditions).list();//获取唯一结果用uniqueResult(),Object类型,若结果不唯一,会报异常
	}
Service:

public List findEmpsByConditions(EmpCondition conditions){
		Transaction tx=null;
		List empList=null;
		try {
			tx=empDao.currentSession().beginTransaction();
			//hql根据条件动态生成
			StringBuilder hql=new StringBuilder("from Emp as emp where 1=1");
			if(conditions.getEmpNo()!=null&&conditions.getEmpNo()!=0){
				hql.append(" and emp.empNo=:empNo");
			}
			if(conditions.geteName()!=null&&conditions.geteName().length()>0){
				hql.append(" and emp.eName=:eName");
			}
			if(conditions.getJob()!=null&&conditions.getJob().length()>0){
				hql.append(" and emp.job=:job");
			}
			if(conditions.getHireDate()!=null){
				hql.append(" and emp.hireDate>:hireDate");
			}
			empList=empDao.findByConditions(hql.toString(), conditions);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			if(tx!=null){
				tx.rollback();
			}
		}
		return empList;
	}

Test:

	EmpCondition conditions=new EmpCondition();
		conditions.seteName("KING");
		try {
			conditions.setHireDate(Tool.strToDate("1982-10-10", "yyyy-MM-dd"));
			List empList=new EmpService().findEmpsByConditions(conditions);
			for (Emp emp : empList) {
				System.out.println(emp.geteName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}




你可能感兴趣的:(Hibernate)