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 Oracle sequence的使用技巧
- jsp Hibernate批量更新和批量删除处理代码
- jsp hibernate的分页代码第1/3页
- Struts2+Hibernate实现数据分页的方法
- Hibernate环境搭建与配置方法(Hello world配置文件版)
- JAVA+Hibernate 无限级分类
- SSH整合中 hibernate托管给Spring得到SessionFactory
- jsp hibernate 数据保存操作的原理
- hibernate中的增删改查实现代码
- 解决hibernate+mysql写入数据库乱码
- java优化hibernate性能的几点建议
- java Hibernate延迟加载
- hibernate 常用方法介绍
- 详解Java的Hibernate框架中的注解与缓存
- 浅析Java的Hibernate框架中的继承关系设计
- Java的Hibernate框架中的继承映射学习教程
- Hibernate实现批量添加数据的方法
- Hibernate4在MySQL5.1以上版本创建表出错 type=InnDB
- JQuery+Ajax+Struts2+Hibernate框架整合实现完整的登录注册
- SSH框架网上商城项目第16战之Hibernate二级缓存处理首页热门显示