Concrete implementation of the example
implementation of the warehouse
Using spring data, add custom interface Searchablerepository, as follows:
Public interface Personrepository extends Crudrepository<person, Long>
}
public class Personrepositoryimpl extends abstractsearchablejparepository<person>{
}
The realization of service
Public interface Personservice {
...
Page<person> searchpeople (Searchcriteria searchcriteria, pageable pageable);
@Service public
class Defaultpersonservice implements personservice{
@Inject personrepository repository;
. ....
@Override
@Transactional public
page<person> searchpeople (Searchcriteria Searchcriteria, pageable pageable) {return
this.repository.search (Searchcriteria, pageable);
}
}
the realization of controller
Public String Find (map<string, object> model, Searchform form,pageable pageable) throws parseexception{Searchcrit
Eria criteria = SearchCriteria.Builder.create (); if (Form.isincludefirstname ()) Criteria.add (New Criterion ("FirstName", Criterion.Operator.EQ, Form.getfirstname ())
; if (Form.isincludemiddleinitial ()) Criteria.add (New Criterion ("MiddleInitial", Criterion.Operator.EQ,
Form.getmiddleinitial ()));
if (Form.isincludelastname ()) Criteria.add (New Criterion ("LastName", Criterion.Operator.EQ, Form.getlastname ());
if (Form.isincludestate ()) Criteria.add (New Criterion ("state", Criterion.Operator.EQ, Form.getstate ());
if (Form.isincludecountry ()) Criteria.add (New Criterion ("Country", Criterion.Operator.EQ, Form.getcountry ()); if (Form.isincludebirthdate ()) Criteria.add (New Criterion ("Birthdate", Criterion.Operator.EQ, New Date (New simpledate
Format (Dateformatter). Parse (Form.getbirthdate ()). GetTime ())); if (form.isincludegendER ()) Criteria.add (New Criterion ("Gender", Criterion.Operator.EQ, Form.getgender ());
if (Form.isincluderace ()) Criteria.add (New Criterion ("Race", Criterion.Operator.EQ, Form.getrace ()); if (form.isincludeethnicity ()) Criteria.add (New Criterion ("ethnicity", Criterion.Operator.EQ, Form.getethnicity ())
;
Model.put ("Searchform", form);
Model.put ("Results", this.personService.searchPeople (criteria, pageable));
return "People/find"; }
for or
We can see that the universal criteria JPA query is powerful, but also dangerous, and that users can make arbitrary queries that affect performance.
In the example, we only give a way of and, in fact, the or way is also the type. Let's take a look at how JPA handles the conditions that exist with and and OR.
The "Example 1" name has n, whether it is a surname or a name (or), and a birthday is a B (and) relationship.
Criteria.select (Root). where (Builder.or builder.equal (Root.get ("LastName"), N),
Builder.equal (Root.get ("FirstName"), N)), Builder.equal (Root.get ("birthdate"), b));
Example 2 is the same as Example 1, as long as it embodies a relational criteria.select (root) embedded with and and OR. where (Builder.and builder.or (
Builder.equal (Root.get ("LastName"), N), Builder.equal (Root.get ("FirstName"), N)
), Builder.equal (Root.get ("birthdate"), b));
"Example 3" is based on an embedding relationship, providing a complex example of Pagecriteria.select (Pageroot). WHERE (Builder.or Builder.and (
Builder.equal (expr), builder.equal (expr), Pageroot.get ("property"). In (expr) ), Builder.or (Builder.lessthan (expr), Builder.greaterthanore
Qualto (expr)), Builder.and (builder.equal (expr), Builder.greaterthan (expr));
RELATED links: My professional Java for WEB applications related articles