Java for Web Learning Notes (121): Search (3) JPA dynamic condition Search (next) __java

Source: Internet
Author: User
Tags string find
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.