您好,登錄后才能下訂單哦!
小編給大家分享一下JPA怎么通過Specification實(shí)現(xiàn)復(fù)雜查詢,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
JPA中繼承BaseRepo之后,可以使用最基本的增刪改查,如果想實(shí)現(xiàn)復(fù)雜查詢,則需要借助Specification來完成這個功能:
public void findAll(ConstructPlanPageReqEntity constructPlanPageReqEntity) { Integer pageNum = page.getPageNum(); Integer pageSize = page.getPageSize(); String costType = constructPlanPageReqEntity.getCostType(); String name = constructPlanPageReqEntity.getName(); String planMoneyStart = constructPlanPageReqEntity.getPlanMoneyStart(); String planMoneyEnd = constructPlanPageReqEntity.getPlanMoneyEnd(); String singMoneyEnd = constructPlanPageReqEntity.getSingMoneyEnd(); String signMoneyStart = constructPlanPageReqEntity.getSignMoneyStart(); long projectId = Long.parseLong(constructPlanPageReqEntity.getProjectId()); String status = constructPlanPageReqEntity.getStatus(); //分頁 pageNum=pageNum-1; Pageable pageable = PageRequest.of(pageNum, pageSize); //多條件匹配查詢 Specification specification= new Specification<ContractPlanBean>() { @Override public Predicate toPredicate(Root<ContractPlanBean> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { ArrayList<Predicate> list = new ArrayList<>(); Path<String> costType1 = root.get("costType"); Path<String> name1 = root.get("name"); Path<Long> projectId1 = root.get("projectId"); Path<Object> status1 = root.get("status"); if (projectId>0){ list.add(criteriaBuilder.equal(projectId1,projectId)); } if (StringUtil.isNotEmpty(status)){ list.add(criteriaBuilder.equal(status1,status)); } //條件查詢 if (StringUtil.isNotEmpty(costType)){ list.add(criteriaBuilder.equal(costType1,costType)); } //模糊查詢 if (StringUtil.isNotEmpty(name)){ list.add(criteriaBuilder.like(name1,"%"+name+"%")); } //范圍查詢 if (StringUtil.isNotEmpty(planMoneyStart)&&StringUtil.isNotEmpty(planMoneyEnd)){ try { list.add(criteriaBuilder.between(root.get("planMoney"),NumberUtil.strToDouble(planMoneyStart),NumberUtil.strToDouble(planMoneyEnd))); } catch (Exception e) { throw new ApiException("規(guī)劃金額查詢失敗"); } } //排序 criteriaQuery.orderBy(criteriaBuilder.asc(root.get("name"))); Predicate[] array = new Predicate[list.size()]; return criteriaBuilder.and(list.toArray(array)); } }; }
以上代碼實(shí)現(xiàn)了多條件查詢,其中需要重寫toPredicate方法,具體參數(shù):
用root.get()獲取bean中的數(shù)據(jù)庫對應(yīng)字段
用criteriaBuilder來組建條件查詢語句
上圖是criteriaBuilder各種sql符號的方法名,根據(jù)需求組建不同的sql語句
criteriaBuilder.and(list.toArray(array))這句是最后定義各個sql查詢條件的關(guān)系,這里用的and
至此,復(fù)雜sql語句就拼接完成,本人對Specification的使用未進(jìn)行深入研究,個人覺得相對filter Strem的復(fù)雜查詢來說Specification更繁瑣,因此更傾向于通過Strem的復(fù)雜查詢,這回就不多說了,下次就介紹下如何使用Stream進(jìn)行復(fù)雜查詢
public Page<ServiceItem> findAll(Map<String, String[]> params, ServiceItemConsumeStatus serviceItemConsumeStatus,ServiceItemStatus serviceItemStatus, Pageable pageable) { return dao.findAll(spec(serviceItemConsumeStatus, serviceItemStatus, params), pageable); } private Specification<ServiceItem> spec(final ServiceItemConsumeStatus serviceItemConsumeStatus, final ServiceItemStatus serviceItemStatus, Map<String, String[]> params) { Collection<SearchFilter> filters = SearchFilter.parse(params).values(); final Specification<ServiceItem> fsp = SearchFilter.spec(filters, ServiceItem.class); Specification<ServiceItem> sp = new Specification<ServiceItem>() { public Predicate toPredicate(Root<ServiceItem> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Predicate pred = fsp.toPredicate(root, query, cb); if (ServiceItemConsumeStatus.可消費(fèi).equals(serviceItemConsumeStatus)) { pred = cb.and(pred, cb.gt(root.get("countLeft").as(int.class), 0)); } else if (ServiceItemConsumeStatus.消費(fèi)完畢.equals(serviceItemConsumeStatus)) { pred = cb.and(pred, cb.le(root.get("countLeft").as(int.class), 0)); } if (serviceItemStatus != null) { pred = cb.and(pred, cb.equal(root.get("status"), serviceItemStatus)); } return pred; } }; return sp; }
看完了這篇文章,相信你對“JPA怎么通過Specification實(shí)現(xiàn)復(fù)雜查詢”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。