Work notes -hibernate of QueryCriteria

Transfer: https: //www.cnblogs.com/jack4738/p/9178394.html

I am using a virtual environment sg-uap

Copy the code
// query methods
// parameter RequestCondition with the controller's front desk @QueryRequestParam annotation can receive incoming entire object
// parameter UserSession currently logged on user information 
// obj entity class
    public QueryResultObject query(RequestCondition queryCondition,UserSession userSession){
        
        QueryCriteria qc = new QueryCriteria;
        // set of queries
        List<obj> result = null;
        // query the total number of records
        int count = 0;
        //Query conditions
        qc.addWhere ( "", "database field name", "=", "value");
        // lookup table
        qc.addFrom(obj.class)
        // other judgment conditions
        if(queryCondition != null){
            // add another query
            qc = wrapQuery(queryCondition,qc);
            // Get the total number of records
            count = getRecordCount(qc);
            // add paging information
            qc = wrapPage(queryCondition,qc);
            result = hibernateDao.findAllByCriteria(qc);
        }else{
            result = hibernateDao.findAllByCriteria(qc);
            count = getRecordCount(qc);
        }
        return envelope object returned
    }
    public QueryCriteria wrapQuery(QueryCriteria qc,RequestCondition queryCondition ){

        List<QueryFilter> wheres = queryCondition.getQueryFilter(obj.class);
        if(wheres != null && wheres.size() > 0){
            // fuzzy query
            for(int i = 0;i<wheres.size();i++){
                QueryFilter q = wheres.get(i);
                if ( "Object Properties" .equals (q.getFieldName)) {
                    // set this property fuzzy query
                    q.setOperator("^");
                }
                // Sort
                qc.addOrder ( "sort field", "collation");
                // construct a query where clause
                CrudUtils.addQCWhere(qc,Wheres,Obj.class.getName());
            }
            String orders = queryCondition.getSorter();
            Alternatively // Construction sorting statement delimiter
            if(orders != null){
                qc.addOrder(Order.replaceAll("&",","))
            }
                return qc;
    }

    public QueryCriteria wrapPage(QueryCriteria qc,RequestCondition queryCondition ){
        int pageIndex = 1,pageSize = 1;
        if(queryCondition.getPageIndex() != null&&queryCondition.getPageSize() != null){
            pageIndex=queryCondition.getPageIndex();
            pageSize=queryCondition.getPageSize();
            qc.addPage(pageIndex,pageSize);
        }
        return qc;
    }
Copy the code

A simple fuzzy + paging query

Guess you like

Origin www.cnblogs.com/sharpest/p/11078614.html