Hibernate查询2


//between  and

@Override
public PageInfo<T> queryInfoAndLikes(PageInfo<T> pageInfo,
			Map<String, String> map, String propertyName, Object minObject,
			Object maxObject) throws DataAccessException {
		if (pageInfo == null) {
			pageInfo = new PageInfo<T>();
		}
		DetachedCriteria detached = DetachedCriteria.forClass(entityClass);
		DetachedCriteria detachedPage = DetachedCriteria.forClass(entityClass);
		for (Map.Entry<String, String> en:map.entrySet()) {
			detached.add(Restrictions.like(en.getKey(),en.getValue(), MatchMode.ANYWHERE));
			detachedPage.add(Restrictions.like(en.getKey(),en.getValue(), MatchMode.ANYWHERE));
		}
		detached.add(Restrictions.between(propertyName, minObject, maxObject));
		detachedPage.add(Restrictions.between(propertyName, minObject, maxObject));
		pageInfo.setPageRow(queryByRowNumber(detachedPage));
		pageInfo.setData(this.getHibernateTemplate().findByCriteria(detached, 
				pageInfo.getCurrentPage() * pageInfo.getPageSize(), pageInfo.getPageSize()));
		return pageInfo;
	}



//外连接,别名
@Repository("organizationDao")
public class OrganizationDaoImpl extends BaseDaoImpl<Organization> implements IOrganizationDao {

	@Override
	public PageInfo<Organization> querySelfUser(PageInfo<Organization> pageInfo,
			Map<String, String> map, Map<String, Object> andEqual,
			Map<String, String> alias, String orKey1, Object value1,
			String orKey2, Object value2) {
		if (pageInfo == null) {
			pageInfo = new PageInfo<Organization>();
		}
		DetachedCriteria detached = DetachedCriteria.forClass(entityClass);
		DetachedCriteria detachedPage = DetachedCriteria.forClass(entityClass);
		if (alias != null) {
			for (Map.Entry<String,String> en:alias.entrySet()) {
				detached.createAlias(en.getKey(), en.getValue());
				detachedPage.createAlias(en.getKey(), en.getValue() ,Criteria.LEFT_JOIN);
			}
		}
		if (orKey1 != null) {
			detached.add(Restrictions.disjunction().add(Restrictions.or(
					Restrictions.eq(orKey1, value1), 
					Restrictions.eq(orKey2, value2)
					)));
			detachedPage.add(Restrictions.disjunction().add(Restrictions.or(
					Restrictions.eq(orKey1, value1), 
					Restrictions.eq(orKey2, value2)
					)));
		}
		for (Map.Entry<String, String> en:map.entrySet()) {
			detached.add(Restrictions.like(en.getKey(),en.getValue(), MatchMode.ANYWHERE));
			detachedPage.add(Restrictions.like(en.getKey(),en.getValue(), MatchMode.ANYWHERE));
		}
		for (Map.Entry<String, Object> en:andEqual.entrySet()) {
		  detached.add(Restrictions.eq(en.getKey(),en.getValue()));
		  detachedPage.add(Restrictions.eq(en.getKey(),en.getValue()));
		}
		//detached.addOrder(Order.asc("organizationId"));
		
		pageInfo.setPageRow(queryByRowNumber(detachedPage));
		pageInfo.setData(this.getHibernateTemplate().findByCriteria(detached, 
		pageInfo.getCurrentPage() * pageInfo.getPageSize(), pageInfo.getPageSize()));
		return pageInfo;

	}




你可能感兴趣的:(Hibernate)