Combined query--Convert Form object to JSON object

Source: Internet
Author: User

1. Package page and rows into pageable
pageable pageable = new Pagerequest (page, rows);
2. Create a combined conditional query criteria Object
Specification<fixedarea> spec = new specification<fixedarea> () {

@Override
Public predicate topredicate (root<fixedarea> Root, criteriaquery<?> query, Criteriabuilder CB) {
2.1 Creating a list collection that holds the assembled conditions
arraylist<predicate> predicates = new arraylist<predicate> ();

2.2 Assembling a combination of query conditions
String id = model.getid ();
if (Stringutils.isnotblank (ID)) {

Predicates.add (Cb.equal (Root.get ("id"). As (String.class), id));

}
String company = Model.getcompany ();
if (Stringutils.isnotblank (company)) {

Predicates.add (Cb.like (Root.get ("Company"). As (String.class), "%" +company+ "&");

}

2.3 Creating an predicate array that holds the assembly conditions
predicate[] Predicatearr = new predicate[predicates.size ()];

2.4 Return to assembled conditions
Return Query.where (Predicates.toarray (Predicatearr)). Getrestriction ();
}
};

3. Perform paged queries
page<fixedarea> page = Fixedareaservice.pagequery (spec,pageable);

4. Remove the "many" field in the Fixedarea entity class that will cause no session
String[] excludes = {"Subareas", "Couriers"};

Call the method in Baseaction to convert the results of the query into JSON sent to the foreground
This.write2jsonobject (page, excludes);

-------------------------------------------------------------------------------------------------------------

In Baseaction:

public void Write2jsonobject (page<?> Page, string[] excludes) throws IOException {

Building JSON Object-type data
map<string, object> map = new hashmap<string, object> ();
Map.put ("Total", page.gettotalelements ());
Map.put ("Rows", page.getcontent ());

Jsonconfig: Configuring unwanted properties in JSON data for transformations
Jsonconfig jsonconfig = new Jsonconfig ();

Jsonconfig.setexcludes (excludes);

Convert Map to JSON
String JSON = jsonobject.fromobject (map, Jsonconfig). toString ();
Servletactioncontext.getresponse (). setContentType ("Test/json;charset=utf-8");
Servletactioncontext.getresponse (). Getwriter (). print (JSON);

}

Combined query--Convert Form object to JSON object

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.