Hibernate查询1

//Hibernate查询

@Repository("crb_bgkDao")
public class Crb_bgkDaoImpl extends BaseDaoImpl<Crb_bgk> implements ICrb_bgkDao {

	@Override
	public PageInfo<Crb_bgk> queryByPersonAndHospital(PageInfo<Crb_bgk> pageInfo, 
			Hospital hospital, Person person, String bqfl) {
		DetachedCriteria detached = DetachedCriteria.forClass(entityClass);
		//取别名
		DetachedCriteria detachedPage = DetachedCriteria.forClass(entityClass);
		detached.createAlias("person", "p");
		detached.createAlias("hospital", "h");
		detachedPage.createAlias("person", "p");
		detachedPage.createAlias("hospital", "h");
		try {
			//根据医院查询
			if (hospital != null && hospital.getHospitalId() != null
					&& !"".equals(hospital.getHospitalId())) {
				detached.add(Restrictions.eq("h.hospitalId", hospital.getHospitalId()));
				detachedPage.add(Restrictions.eq("h.hospitalId", hospital.getHospitalId()));
				//根据个人信息查询
				if (person != null) {
					//姓名
					if (person.getName() != null && !"".equals(person.getName())) {
						detached.add(Restrictions.ilike("p.name", person.getName(), MatchMode.ANYWHERE));
						detachedPage.add(Restrictions.ilike("p.name", person.getName(), MatchMode.ANYWHERE));
					}
					//身份证
					if (person.getCardNumber() != null && !"".equals(person.getCardNumber())) {
						detached.add(Restrictions.eq("p.cardNumber", person.getCardNumber()));
						detachedPage.add(Restrictions.eq("p.cardNumber", person.getCardNumber()));
					}
				}
				//病情分类:8--艾滋病;9--寄生虫;7--其他传染病
				if (bqfl != null && !"".equals(bqfl)) {
					if ("8".equals(bqfl.trim())) {
						detached.add(Restrictions.like("crb_yl", "8", MatchMode.ANYWHERE));
						detachedPage.add(Restrictions.like("crb_yl", "8", MatchMode.ANYWHERE));
					}
					else if ("9".equals(bqfl.trim())) {
						detached.add(
								//“或”查询
								Restrictions.or(
								    Restrictions.like("crb_yl", "9", MatchMode.ANYWHERE), 
								    Restrictions.like("crb_bl", "9", MatchMode.ANYWHERE)
								)
							);
						detachedPage.add(
								Restrictions.or(
										Restrictions.like("crb_yl", "9", MatchMode.ANYWHERE), 
										Restrictions.like("crb_bl", "9", MatchMode.ANYWHERE)
								)
						);
					}
					else if ("7".equals(bqfl.trim())) {
						//根据判断字段是否为空查询
						detached.add(Restrictions.isNotNull("qtcrb"));
						detachedPage.add(Restrictions.isNotNull("qtcrb"));
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		pageInfo.setData(this.getHibernateTemplate().findByCriteria(detached, 
				pageInfo.getCurrentPage() * pageInfo.getPageSize(), pageInfo.getPageSize()));
		pageInfo.setPageRow(queryByRowNumber(detachedPage));
		return pageInfo;
	}

你可能感兴趣的:(hibernate查询)