您的位置:首页 > 其它

Hibernet DetachedCriteria 对象使用,Mark一下~

2016-07-22 17:36 441 查看
public QueryResult<T> findByMap(QueryResult<T> queryResult,Map<String, Object> map,DetachedCriteria detachedCriteria,List<String> colList) throws Exception {
List<T> resultlist = new ArrayList<T>() ;
if(null==queryResult){
queryResult = new QueryResult<T>() ;
}
Criteria criteria = detachedCriteria.getExecutableCriteria(getSessionFactory().getCurrentSession()) ;
String propertyPrefix = null ;
String propertySuffix = null ;
List<Order> orders = new ArrayList<Order>();
String[] orarr = null ;
Disjunction disjunction = null;
for (String key : map.keySet()) {
if(null==map.get(key)||"".equals(map.get(key))){
continue;
}
if(key.contains("||")){
disjunction = Restrictions.disjunction();
orarr = key.split("\\|\\|");
for (String strOr : orarr) {
strOr = strOr.trim();
if(strOr.contains(".")){
propertyPrefix = StringUtils.substringBeforeLast(strOr,".").trim();
propertySuffix = StringUtils.substringAfterLast(strOr,".").trim();
//多类型查询条件处理
if(propertySuffix.equals("like")){
if(((String)map.get(key)).indexOf("%")>-1){
disjunction.add(Restrictions.like(propertyPrefix, map.get(key)));
}else{
disjunction.add(Restrictions.like(propertyPrefix, "%"+map.get(key)+"%"));
}
}else if(propertySuffix.equals("=")){
disjunction.add(Restrictions.eq(propertyPrefix, map.get(key)));
}else if(propertySuffix.equals("!=")){
disjunction.add(Restrictions.ne(propertyPrefix, map.get(key)));
}else if(propertySuffix.equals(">")){
disjunction.add(Restrictions.gt(propertyPrefix, map.get(key)));
}else if(propertySuffix.equals(">=")){
disjunction.add(Restrictions.ge(propertyPrefix, map.get(key)));
}else if(propertySuffix.equals("<")){
disjunction.add(Restrictions.lt(propertyPrefix, map.get(key)));
}else if(propertySuffix.equals("<=")){
disjunction.add(Restrictions.le(propertyPrefix, map.get(key)));
}
}else{
disjunction.add(Restrictions.eq(strOr, map.get(key))) ;
}
}
criteria.add(disjunction);
}else if(key.contains(".")){
propertyPrefix = StringUtils.substringBeforeLast(key,".").trim();
propertySuffix = StringUtils.substringAfterLast(key,".").trim();
//排序处理
if(propertyPrefix.equalsIgnoreCase("orderby")&&propertySuffix.equals("asc")){
orders.add(Order.asc((String)map.get(key))) ;
}else if(propertyPrefix.equalsIgnoreCase("orderby")&&propertySuffix.equals("desc")){
orders.add(Order.desc((String)map.get(key)));
}
//多类型查询条件处理
if((map.get(key) instanceof String)&&((String)map.get(key)).contains("||")){
disjunction = Restrictions.disjunction();
String[] valarr = ((String)map.get(key)).split("\\|\\|");
for (String val : valarr) {
if(propertySuffix.equals("like")){
if(((String)map.get(key)).indexOf("%")>-1){
disjunction.add(Restrictions.like(propertyPrefix, val));
}else{
disjunction.add(Restrictions.like(propertyPrefix, "%"+val+"%"));
}
}else if(propertySuffix.equals("=")){
disjunction.add(Restrictions.eq(propertyPrefix, val));
}else if(propertySuffix.equals("!=")){
disjunction.add(Restrictions.ne(propertyPrefix, val));
}else if(propertySuffix.equals(">")){
disjunction.add(Restrictions.gt(propertyPrefix, val));
}else if(propertySuffix.equals(">=")){
disjunction.add(Restrictions.ge(propertyPrefix, val));
}else if(propertySuffix.equals("<")){
disjunction.add(Restrictions.lt(propertyPrefix, val));
}else if(propertySuffix.equals("<=")){
disjunction.add(Restrictions.le(propertyPrefix, val));
}
}
criteria.add(disjunction);
}else{
if(propertySuffix.equals("like")){
if(((String)map.get(key)).indexOf("%")>-1){
criteria.add(Restrictions.like(propertyPrefix, map.get(key)));
}else{
criteria.add(Restrictions.like(propertyPrefix, "%"+map.get(key)+"%"));
}
}else if(propertySuffix.equals("=")){
criteria.add(Restrictions.eq(propertyPrefix, map.get(key)));
}else if(propertySuffix.equals("!=")){
criteria.add(Restrictions.ne(propertyPrefix, map.get(key)));
}else if(propertySuffix.equals(">")){
criteria.add(Restrictions.gt(propertyPrefix, map.get(key)));
}else if(propertySuffix.equals(">=")){
criteria.add(Restrictions.ge(propertyPrefix, map.get(key)));
}else if(propertySuffix.equals("<")){
criteria.add(Restrictions.lt(propertyPrefix, map.get(key)));
}else if(propertySuffix.equals("<=")){
criteria.add(Restrictions.le(propertyPrefix, map.get(key)));
}
}
}else{
if(key.equalsIgnoreCase("orderby")){
orders.add(Order.asc((String)map.get(key))) ;
}else{
criteria.add(Restrictions.eq(key, map.get(key)));
}
}
}
Integer totalCount = ((Long)criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
queryResult.setRecordtotal(totalCount) ;
if(queryResult.getIndex()>queryResult.getPageCount()){
return queryResult;
}
if(totalCount>0){
for (Order o : orders) {
criteria.addOrder(o) ;
}
ProjectionList  proList = null;//设置投影集合
if(null!=colList&&colList.size()>0){
proList = Projections.projectionList();//设置投影集合
for (String colName : colList) {
proList.add(Projections.alias(Projections.property(colName), colName));
}
}
criteria.setProjection(proList);
criteria.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
criteria.setFirstResult((queryResult.getIndex()-1)*queryResult.getPageSize()) ;
criteria.setMaxResults(queryResult.getPageSize()) ;
resultlist = (List<T>)criteria.list();
}
queryResult.setResultlist(resultlist) ;
return queryResult;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hibernate Criteria