Skip to content

Instantly share code, notes, and snippets.

@enesacikoglu
Created February 2, 2018 11:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enesacikoglu/d49bfdd578b871e2b25f3203bc4196c1 to your computer and use it in GitHub Desktop.
Save enesacikoglu/d49bfdd578b871e2b25f3203bc4196c1 to your computer and use it in GitHub Desktop.
SpringDataRest Pageable Example
/**
Main start
*/
package com.cengenes;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@SpringBootApplication
public class SpringBootHelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloWorldApplication.class, args);
}
}
/**
Main end
*/
/**
Controller start
*/
package com.cengenes.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.cengenes.data.EmployeeRepository;
import com.cengenes.model.Employee;
@RestController
public class EmployeeController {
private static final int DEFAULT_PAGE_NUMBER = 0;
private static final int DEFAULT_PAGE_SIZE = 25;
@Autowired
private EmployeeRepository employeeRepo;
@RequestMapping(value = "/listPageable", method = RequestMethod.GET)
Page<Employee> employeesPageable(
@PageableDefault(page = DEFAULT_PAGE_NUMBER,
size = DEFAULT_PAGE_SIZE) @SortDefault.SortDefaults({
@SortDefault(sort = "dept", direction = Sort.Direction.DESC),
@SortDefault(sort = "name", direction = Sort.Direction.DESC),
@SortDefault(sort = "surname", direction = Sort.Direction.ASC) }) Pageable pageable) {
return employeeRepo.findAll(pageable);
}
}
/**
Controller end
*/
/**
Repo start
*/
package com.cengenes.data;
import org.springframework.data.jpa.repository.JpaRepository;
import com.cengenes.model.Employee;
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
/**
Repo end
*/
/**
Model start
*/
package com.cengenes.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Employee {
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private long id;
private String name;
private String surname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
private String dept;
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", dept=" + dept + "]";
}
}
/**
Model end
*/
/**
application.properties start
*/
spring.datasource.data=classpath:/db-init/employee/employee-h2.sql
/**
application.properties end
*/
/**
DB SCRIPTS start
*/
insert into employee(ID,DEPT,NAME,SURNAME,) values(9999,'AppName','String','TrendYol');
insert into employee(ID,DEPT,NAME,SURNAME,) values(10000,'TimeOut','Integer','61');
insert into employee(ID,DEPT,NAME,SURNAME,) values(10001,'Count','Double','100000');
insert into employee(ID,DEPT,NAME,SURNAME,) values(10002,'IsChanged','Boolean','false');
insert into employee(ID,DEPT,NAME,SURNAME,) values(10003,'AppName','String','a');
insert into employee(ID,DEPT,NAME,SURNAME,) values(10004,'TimeOut','Integer','b');
insert into employee(ID,DEPT,NAME,SURNAME,) values(10005,'Count','Double','c');
insert into employee(ID,DEPT,NAME,SURNAME,) values(10006,'IsChanged','Boolean','d');
insert into employee(ID,DEPT,NAME,SURNAME,) values(99999,'AppName','String','e');
insert into employee(ID,DEPT,NAME,SURNAME,) values(100006,'TimeOut','Integer','f');
insert into employee(ID,DEPT,NAME,SURNAME,) values(100011,'Count','Double','g');
insert into employee(ID,DEPT,NAME,SURNAME,) values(100022,'IsChanged','Boolean','h');
insert into employee(ID,DEPT,NAME,SURNAME,) values(9999121,'AppName','String','l');
insert into employee(ID,DEPT,NAME,SURNAME,) values(1000044,'TimeOut','Integer','61');
insert into employee(ID,DEPT,NAME,SURNAME,) values(1000121,'Count','Double','m');
insert into employee(ID,DEPT,NAME,SURNAME,) values(1000211,'IsChanged','Boolean','n');
insert into employee(ID,DEPT,NAME,SURNAME,) values(9999411,'AppName','String','k');
insert into employee(ID,DEPT,NAME,SURNAME,) values(100001231,'TimeOut','Integer','f');
insert into employee(ID,DEPT,NAME,SURNAME,) values(10001123,'Count','Double','s');
insert into employee(ID,DEPT,NAME,SURNAME,) values(1000112,'IsChanged','Boolean','c');
insert into employee(ID,DEPT,NAME,SURNAME,) values(999419,'AppName','String','s');
insert into employee(ID,DEPT,NAME,SURNAME,) values(231231,'TimeOut','Integer','h');
insert into employee(ID,DEPT,NAME,SURNAME,) values(414111,'Count','Double','j');
insert into employee(ID,DEPT,NAME,SURNAME,) values(231123,'IsChanged','Boolean','i');
insert into employee(ID,DEPT,NAME,SURNAME,) values(5156114,'AppName','String','o');
insert into employee(ID,DEPT,NAME,SURNAME,) values(2341314,'TimeOut','Integer','y');
insert into employee(ID,DEPT,NAME,SURNAME,) values(313131211,'Count','Double','u');
insert into employee(ID,DEPT,NAME,SURNAME,) values(141412124,'IsChanged','Boolean','n');
insert into employee(ID,DEPT,NAME,SURNAME,) values(231231111,'AppName','String','v');
insert into employee(ID,DEPT,NAME,SURNAME,) values(4444444,'TimeOut','Integer','vb');
insert into employee(ID,DEPT,NAME,SURNAME,) values(5512156767,'Count','Double','sa');
insert into employee(ID,DEPT,NAME,SURNAME,) values(8585656,'IsChanged','Boolean','gg');
/**
DB SCRIPTS end
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment