Spring Data JPA Implementation paging (Spring Mvc+easyui)

Source: Internet
Author: User

Spring data JPA Nicely encapsulates the DAO layer, which is primarily written to implement Easyui Datagird data paging, because the various UI parameters are not the same, so if you are using a different UI, you have to modify it slightly. What needs to be explained is that my code is part of the code in the Spring MVC framework, and part of the structure of the code is given, giving only the key code.

First, when the easy UI sends a request to the server, it automatically returns 4 parameters (Page,rows,order,sort), first writing a PageContent class:

Package Module.system.entity;import Javax.persistence.entity;import Javax.persistence.id;public class PageContent {    private String page;    Private String rows;    Private String sort;    private string Order;public string GetPage () {return page;} public void Setpage (String page) {this.page = page;} Public String GetRows () {return rows;} public void Setrows (String rows) {this.rows = rows;} Public String Getsort () {return sort;} public void Setsort (String sort) {this.sort = sort;} Public String GetOrder () {return order;} public void Setorder (String order) {this.order = order;}    }

Here is the DAO layer, data is an entity class, do not care, replace with your can:

Import Module.app.entity.data;import Module.system.entity.department;import org.springframework.data.domain.Page; Import Org.springframework.data.domain.pageable;import org.springframework.data.jpa.repository.JpaRepository; Public interface Datadao extends jparepository<data,integer> {public page<data> findAll (pageable pageable) ;}


The following is part of the controller's code, and the four parameters sent by the client are automatically assigned to the Page object:

@RequestMapping (value = "/getall.do", method = Requestmethod.post) @ResponseBody  //This note indicates that the return value skips the View Processing section and writes directly to the HTTP Response body in public Map getAll (httpservletrequest request,pagecontent page) {Sort sort = null;if (Page.getorder (). Equals ("ASC")) {sort = new sort (DIRECTION.ASC, page.getsort ());} else if (Page.getorder (). Equals ("desc")) {sort = new sort (Direction.desc, page.getsort ());} int pagenum = Integer.parseint (Page.getpage ()) -1;int rows = Integer.parseint (Page.getrows ()); pageable pageable = new Pagerequest (pagenum,rows,sort); page<data> list = Dataservice.findall (pageable); Map map = new HashMap (); Map.put ("Total", list.gettotalelements ());//Data Totals Map.put ("Rows", list.getcontent ());// Paging should show the data//in the console print Net.sf.json.JSONArray Jsonarray = net.sf.json.JSONArray.fromObject (map); System.out.println (Jsonarray.tostring ()); return map;}
Since my spring MVC framework integrates JSON, returning the map directly to the client automatically dumps the JSON data






Spring Data JPA Implementation paging (Spring Mvc+easyui)

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.