First, preface
Just use SPRING-DATA-JPA, encountered a lot of difficulties, on-line search a lot of information, found that not much to say JPA, send a I just done the interface of the project bar.
Second, demand
Do you see the picture? Need to implement the search and sorting of various fields and paging, there may be a selection of various conditions drop-down list, is not very abnormal?
Three, began the one, DAO
The DAO layer needs to be processed first, here like called Repository. Do an entity class of DAO layer interface, inherit Jpaspecificationexecutor, and then write a query interface.
Second, service
The main processing here is the query criteria, I here is the search function of the fuzzy query, of course, if there are more queries can also be added here. It is important to note that specification.
Third, sort
A helper entity class needs to be built first, and the attribute name is the same name as the entity class that needs to be sorted, but note that the attributes are of type string. In the following detail, I built the auxiliary class first.
@Data
public class Deptsort {
Private String id;//Encoding
Private String name;//Name
Private String highdeptname;//Parent Department
Private String principal;//in charge
Private String depttype;//Department type
Private String enable;//Enabled
}
fields are fields that need to be sorted, which is for good distinction, and can be called other.
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<sort.order> orders = new arraylist<sort.order> ();
if (Stringutils.isnotblank (Deptsort.getid ())) {
Orders.add (New Sort.order (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 (New Sort.order (Sort.Direction.fromString (Deptsort.getenable ()), "Enable");
}
sort sort = new sort (orders);
pageable pageable = new Pagerequest (pagenumber,pagesize,sort);
page<businessdept> all = Service.findall (search, pageable);
model = Responsemodel.getsuccessresponsemodel (). SetData (all);
}catch (Exception e) {
e.printstacktrace ();
model = Responsemodel.getfailedresponsemodel ();
}
return model;
}
The required parameters are search content searches, and Deptsort helper classes. First establish
list<sort.order> orders = new arraylist<sort.order> (); set, then if the argument is added to the collection.
What needs to be explained is a similar
Orders.add (New Sort.order (Sort.Direction.fromString (Deptsort.getenable ()), "enable") statement, "Enable" is required to query the
The field in the Businessdept, not the auxiliary class, of course, my auxiliary class and the Businessdept class is consistent, but the different students need to pay attention to.
Iv. Front-end
What are the requirements for the parameters passed by the front end?
The parameters of each property of the Deptsort can only be limited to two ASC and DESC, ascending and descending. Functional requirements only need to pass a property in the Deptsort, and here are two parameters to demonstrate.
Query successful data does not show, to show you a background SQL statement
Hibernate:
/* Select
Count (GENERATEDALIAS0)
From
Businessdept as GeneratedAlias0
where
(
Generatedalias0.name like:p aram0
)
and (
Generatedalias0.deleteis=1
) */Select
Count (businessde0_.id) as col_0_0_
From
T_department businessde0_
where
(
Businessde0_.name like?
)
and Businessde0_.delete_is=1
Hibernate:
/* Select
GeneratedAlias0
From
Businessdept as GeneratedAlias0
where
(
Generatedalias0.name like:p aram0
)
and (
Generatedalias0.deleteis=1
)
ORDER BY
Generatedalias0.depttype ASC,
Generatedalias0.enable desc */Select
Businessde0_.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_
From
T_department businessde0_
where
(
Businessde0_.name like?
)
and Businessde0_.delete_is=1
ORDER BY
Businessde0_.dept_type ASC,
Businessde0_.enable desc limit?
You can see conditional queries, both ascending and descending.
Iv. concluding remarks
OK, I am the first post of my hair, please support more, it is best to pay attention to me.
JPA specification complex queries and sequencing