In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how JPA achieves complex queries through Specification. I hope you will get something after reading this article. Let's discuss it together.
JPA implements complex query through Specification
After inheriting BaseRepo in JPA, you can use the most basic addition, deletion, modification and query. If you want to implement a complex query, you need to use Specification to complete this function:
Here is a brief introduction to the use of 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 (); / / paging pageNum=pageNum-1; Pageable pageable = PageRequest.of (pageNum, pageSize); / / multiple conditional matching query Specification specification= new Specification () {@ Override public Predicate toPredicate (Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {ArrayList list = new ArrayList () Path costType1 = root.get ("costType"); Path name1 = root.get ("name"); Path projectId1 = root.get ("projectId"); Path status1 = root.get ("status"); if (projectId > 0) {list.add (criteriaBuilder.equal (projectId1,projectId)) } if (StringUtil.isNotEmpty (status)) {list.add (criteriaBuilder.equal (status1,status));} / / conditional query if (StringUtil.isNotEmpty (costType)) {list.add (criteriaBuilder.equal (costType1,costType)) } / / Fuzzy query if (StringUtil.isNotEmpty (name)) {list.add (criteriaBuilder.like (name1, "%" + name+ "%")) } / / range query 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 ("Planning amount query failed");}} / / sort criteriaQuery.orderBy (criteriaBuilder.asc (root.get ("name")); Predicate [] array = new Predicate [list.size ()] Return criteriaBuilder.and (list.toArray (array));};}
The above code implements a multi-conditional query, in which the toPredicate method needs to be overridden with specific parameters:
Use root.get () to get the corresponding fields of the database in bean
Using criteriaBuilder to build conditional query statement
The above figure shows the method names of various sql symbols in criteriaBuilder, and different sql statements are constructed according to the requirements.
The sentence criteriaBuilder.and (list.toArray (array)) is the last to define the relationship of each sql query condition. And is used here.
So far, the complex sql statements have been spliced, and I have not conducted an in-depth study on the use of Specification. I personally feel that Specification is more cumbersome than the complex query of filter Strem, so I prefer the complex query through Strem. I won't say much this time. I'll introduce how to use Stream for complex query next time.
Spring-data-jpa Specification stitching complex query public Page findAll (Map params, ServiceItemConsumeStatus serviceItemConsumeStatus,ServiceItemStatus serviceItemStatus, Pageable pageable) {return dao.findAll (spec (serviceItemConsumeStatus, serviceItemStatus, params), pageable);} private Specification spec (final ServiceItemConsumeStatus serviceItemConsumeStatus, final ServiceItemStatus serviceItemStatus, Map params) {Collection filters = SearchFilter.parse (params). Values (); final Specification fsp = SearchFilter.spec (filters, ServiceItem.class) Specification sp = new Specification () {public Predicate toPredicate (Root root, CriteriaQuery query, CriteriaBuilder cb) {Predicate pred = fsp.toPredicate (root, query, cb); if (ServiceItemConsumeStatus. Consumable .equals (serviceItemConsumeStatus) {pred = cb.and (pred, cb.gt (root.get ("countLeft"). As (int.class), 0);} else if (ServiceItemConsumeStatus. Consumption completed .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;} after reading this article, I believe you have some understanding of "how JPA implements complex queries through Specification". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 262
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.