jQuery条件分页 代替离线查询(附代码)
程序员文章站
2022-11-26 09:15:17
import javax.persistence.criteria.criteriabuilder;
import javax.persistence.crite...
import javax.persistence.criteria.criteriabuilder; import javax.persistence.criteria.criteriaquery; import javax.persistence.criteria.join; import javax.persistence.criteria.predicate; import javax.persistence.criteria.root; import net.sf.json.jsonobject; import net.sf.json.jsonconfig; @action("courieraction_pagequery") public string pagequery() throws exception { pageable pageable = new pagerequest(page-1, rows); final string couriernum = model.getcouriernum(); final string company = model.getcompany(); final string type = model.gettype(); final standard standard = model.getstandard(); //相当于detchedcriteria对象.通过specification封装过滤条件 specification<courier> specification = new specification<courier>() { @override public predicate topredicate(root<courier> root, criteriaquery<?> query, criteriabuilder cb) { //root:根实体 query:排序,封装条件 criteriabuilder:predicate断言工厂,产生predicate对象 //添加过滤条件:添加快递员编号条件 //p1:实体中属性 p2:条件 list<predicate> list = new arraylist<>(); if(stringutils.isnotblank(couriernum)){ predicate p1 = cb.equal(root.get("couriernum").as(string.class), couriernum); list.add(p1); } if(stringutils.isnotblank(company)){ predicate p2 = cb.equal(root.get("company").as(string.class), company); list.add(p2); } if(stringutils.isnotblank(type)){ predicate p3 = cb.equal(root.get("type").as(string.class), type); list.add(p3); } //sql : select * from t_courier t inner join t_standard s on t.c_standard_id = s.c_id // where s.c_name = '标准一(100公斤)'; //jpql: from courier c inner join c.standard s where s.name = ""; if(standard!=null && stringutils.isnotblank(standard.getname())){ //返回关联对象 join<object, object> join = root.join("standard"); predicate p4 = cb.equal(join.get("name").as(string.class), standard.getname()); list.add(p4); } if(list.size()==0){ return null; } //list集合转为数组 predicate[] restrictions = new predicate[list.size()]; restrictions = list.toarray(restrictions); return cb.and(restrictions); } }; page<courier> page = courierservice.findall(specification, pageable); map<string, object> map = new hashmap<>(); map.put("total", page.gettotalelements()); map.put("rows", page.getcontent()); //将fixedares集合属性排除掉,不转json jsonconfig jsonconfig = new jsonconfig(); jsonconfig.setexcludes(new string[]{"fixedareas"}); string json = jsonobject.fromobject(map, jsonconfig).tostring(); servletactioncontext.getresponse().setcontenttype("text/json;charset=utf-8"); servletactioncontext.getresponse().getwriter().write(json); return none; } dao: public interface courierdao extends jparepository<courier, integer>, jpaspecificationexecutor<courier> { }
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助~如果有疑问大家可以留言交流,谢谢大家对的支持!
上一篇: ai怎么画宠物小精灵中的铁壳蛹?
下一篇: 看来宝宝很满意这个奶嘴