In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the common queries in JPA Specification". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the common queries in JPA Specification.
JPA Specification common query + sort 1. Step 1: inherit the parent class public interface TblCarton2RCardLogRepository extends JpaRepository,JpaSpecificationExecutor {2. Step 2: tblCarton2RCardLogRepository.findAll (new Specification () {@ Override public Predicate toPredicate (Root root, CriteriaQuery query,CriteriaBuilder cb) {List list = new ArrayList (); list.add (cb.equal (root.get ("cartonNo") .as (String.class), cartonNo)); / / some common field list.add (cb.equal (root.get ("id"). Get ("rCard") .as (String.class), rCard) / / A field in the primary key list.add (cb.like (root.get ("mocode") .as (String.class), "%" + mocode + "%")); / / like list.add (cb.between (root.get ("frozenDate") .as (Long.class), frozenDateStart, frozenDateEnd)); / / between and list.add (cb.greaterThanOrEqualTo (root.get ("id"). Get ("rcard") .as (String.class), rCardStart) / greater than or equal to list.add (root.get ("id"). Get ("lotNo") .as (String.class) .in (lotNos); / / in / / ORDER BY packdate DESC,packtime DESC Predicate [] p = new Predicate [list.size ()]; query.where (cb.and (list.toArray (p) Query.orderBy (cb.desc (root.get ("packDate")), cb.desc (root.get ("packTime")); return query.getRestriction ();}}); JPA Specification complex query + sorting
Just using spring-data-jpa, I encountered a lot of problems. I checked a lot of information on the Internet and found that there are not many people talking about jpa. I just sent a process of the interface I have just done.
Demand
Do you see the picture? Need to achieve the search and the sorting of various fields but also pagination, there may be a variety of criteria to select the drop-down list, is not very abnormal?
Here we go. Dao
You need to deal with the dao layer first, which is called repository here. Do an entity class dao layer interface, inherit JpaSpecificationExecutor, and write a query interface.
II. Service
Here we mainly deal with the query conditions, I am here is the search function of the fuzzy query, of course, if there are more queries can also be added here. What needs to be noticed here is specification.
Third, sort
You need to create an auxiliary entity class first. I have the same name as the entity class that needs to be sorted, but note that the attributes are of type String. I'll talk about it in more detail. I'll start with the auxiliary class I built.
@ Datapublic class DeptSort {private String id;// Encoding private String name;// name private String highDeptName;// superior department private String principal;// head private String deptType;// department type private String enable;// enabled}
Fields are all fields that need to be sorted. This is for the sake of distinction. Other fields can be called.
The following is the controller layer, the specific implementation of the sorting function.
Public ResponseModel table (@ RequestParam ("search") String search,@RequestParam ("pageNumber") Integer pageNumber,@RequestParam ("pageSize") Integer pageSize,@RequestBody DeptSort deptSort) {ResponseModel model = null;try {List orders = new ArrayList (); if (StringUtils.isNotBlank (deptSort.getId () {orders.add (Sort.Direction.fromString (deptSort.getId ()), "id")) } if (StringUtils.isNotBlank (deptSort.getName () {orders.add (new Sort.Order (Sort.Direction.fromString (deptSort.getName ()), "name"));} if (StringUtils.isNotBlank (deptSort.getHighDeptName () {orders.add (new Sort.Order (Sort.Direction.fromString (deptSort.getHighDeptName ()), "highDeptName");} if (StringUtils.isNotBlank (deptSort.getPrincipal () {orders.add (new Sort.Order (Sort.Direction.fromString (deptSort.getPrincipal ()), "principal") } if (StringUtils.isNotBlank (deptSort.getDeptType () {orders.add (new Sort.Order (Sort.Direction.fromString (deptSort.getDeptType ()), "deptType"));} if (StringUtils.isNotBlank (deptSort.getEnable () {orders.add (Sort.Direction.fromString (deptSort.getEnable ()), "enable")) } / / orders cannot be empty, so if empty setting, sort by id [/ code] [code] if (orders.size () = = 0) {orders.add (new Sort.Order (Sort.Direction.ASC, "id")) {orders.add (new Sort.Order (Sort.Direction.ASC, "id"));} Sort sort = new Sort (orders); Pageable pageable = new PageRequest (pageNumber,pageSize,sort); Page all = service.findAll (search, pageable); model = ResponseModel.getSuccessResponseModel (). SetData (all);} catch (Exception e) {e.printStackTrace (); model = ResponseModel.getFailedResponseModel () } return model;}
The required parameters are the search content search and the DeptSort helper class. First set up
List orders = new ArrayList ()
Collection, and then if judges to add parameters to the collection.
What needs to be noted is similar.
Orders.add (new Sort.Order (Sort.Direction.fromString (deptSort.getEnable ()), "enable")
Statement, "enable" is the field in the Businessdept that needs to be queried, not the auxiliary class, of course, my auxiliary class here is the same as the Businessdept class, but different students need to pay attention.
Front end
What are the requirements for the parameters passed by the front end?
The parameters of each property of deptSort can only qualify two types of asc and desc, ascending and descending. The functional requirements in the above figure only need to be passed one attribute in deptSort. Here are two parameters to demonstrate.
The data that has been queried successfully will not be displayed. I will show you a background SQL statement.
Hibernate: / * selectcount (generatedAlias0) fromBusinessdept as generatedAlias0 where (generatedAlias0.name like: param0) and (generatedAlias0.deleteIs=1) * / selectcount (businessde0_.id) as col_0_0_ fromt_department businessde0_ where (businessde0_.name like?) And businessde0_.delete_is=1Hibernate: / * selectgeneratedAlias0 fromBusinessdept as generatedAlias0 where (generatedAlias0.name like: param0) and (generatedAlias0.deleteIs=1) order bygeneratedAlias0.deptType asc,generatedAlias0.enable desc * / selectbusinessde0_.id as id1_3_,businessde0_.delete_is as delete_i2_3_,businessde0_.dept_type as dept_typ3_3_,businessde0_.enable as enable4_3_,businessde0_.high_dept_id as high_dep5_3_,businessde0_.high_dept_name as high_dep6_3_,businessde0_.name as name7_3_ Businessde0_.principal as principa8_3_ fromt_department businessde0_ where (businessde0_.name like?) And businessde0_.delete_is=1 order bybusinessde0_.dept_type asc,businessde0_.enable desc limit?
You can see conditional queries, both ascending and descending.
Thank you for your reading, these are the contents of "what are the common queries in JPA Specification?" after the study of this article, I believe you have a deeper understanding of what common queries in JPA Specification have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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: 0
*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.