From 4fb2f5c9e7072d2e08bbf7f4d2eaf49238b5e967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C4=B1van=C3=A7=20Sahici?= Date: Tue, 18 Dec 2018 23:22:36 +0300 Subject: [PATCH 1/6] use JpaRepository for paging and sorting --- spring-boot-datatable/pom.xml | 10 +- .../opencodez/controllers/BaseController.java | 325 +++++++++--------- .../repo/GenericPagingRepository.java | 8 + .../src/main/resources/application.properties | 14 +- .../src/main/resources/data.sql | 20 ++ 5 files changed, 216 insertions(+), 161 deletions(-) create mode 100644 spring-boot-datatable/src/main/java/com/opencodez/repo/GenericPagingRepository.java create mode 100644 spring-boot-datatable/src/main/resources/data.sql diff --git a/spring-boot-datatable/pom.xml b/spring-boot-datatable/pom.xml index 938912f..765944e 100644 --- a/spring-boot-datatable/pom.xml +++ b/spring-boot-datatable/pom.xml @@ -46,20 +46,26 @@ com.google.code.gson gson 2.6.2 - + mysql mysql-connector-java 5.1.36 - + + + com.h2database + h2 + runtime + diff --git a/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java b/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java index ed9f49b..2b5df3b 100644 --- a/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java +++ b/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java @@ -1,154 +1,171 @@ -/** - * - */ -package com.opencodez.controllers; - -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.Query; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.servlet.ModelAndView; - -import com.google.gson.Gson; -import com.opencodez.domain.User; -import com.opencodez.domain.UserModel; -import com.opencodez.domain.pagination.DataTableRequest; -import com.opencodez.domain.pagination.DataTableResults; -import com.opencodez.domain.pagination.PaginationCriteria; -import com.opencodez.repo.GenericRepo; -import com.opencodez.repo.UserRepository; -import com.opencodez.util.AppUtil; - -/** - * @author pavan.solapure - * - */ -@Controller -public class BaseController { - - @Autowired - private UserRepository userRepo; - - @Autowired - private GenericRepo genericRepo; - - /** The entity manager. */ - @PersistenceContext - private EntityManager entityManager; - - - @RequestMapping(value="/", method = RequestMethod.GET) - public ModelAndView home(@RequestParam(value = "name", defaultValue = "World") String name) { - ModelAndView mv = new ModelAndView("index"); - mv.addObject("userModel", new UserModel()); - List userList = genericRepo.getUserModel(); - mv.addObject("userlist", userList); - return mv; - } - - @RequestMapping(value="/users/mysql", method=RequestMethod.GET) - public String listUsers(Model model) { - return "users_mysql"; - } - - @RequestMapping(value="/users/paginated/mysql", method=RequestMethod.GET) - @ResponseBody - public String listUsersPaginated(HttpServletRequest request, HttpServletResponse response, Model model) { - - DataTableRequest dataTableInRQ = new DataTableRequest(request); - PaginationCriteria pagination = dataTableInRQ.getPaginationRequest(); - - String baseQuery = "SELECT USER_ID as id, USER_NAME as name, SALARY as salary, (SELECT COUNT(1) FROM MYUSERS) AS total_records FROM MYUSERS"; - String paginatedQuery = AppUtil.buildPaginatedQuery(baseQuery, pagination); - - System.out.println(paginatedQuery); - - Query query = entityManager.createNativeQuery(paginatedQuery, UserModel.class); - - @SuppressWarnings("unchecked") - List userList = query.getResultList(); - - DataTableResults dataTableResult = new DataTableResults(); - dataTableResult.setDraw(dataTableInRQ.getDraw()); - dataTableResult.setListOfDataObjects(userList); - if (!AppUtil.isObjectEmpty(userList)) { - dataTableResult.setRecordsTotal(userList.get(0).getTotalRecords() - .toString()); - if (dataTableInRQ.getPaginationRequest().isFilterByEmpty()) { - dataTableResult.setRecordsFiltered(userList.get(0).getTotalRecords() - .toString()); - } else { - dataTableResult.setRecordsFiltered(Integer.toString(userList.size())); - } - } - return new Gson().toJson(dataTableResult); - } - - @RequestMapping(value="/users/oracle", method=RequestMethod.GET) - public String listUsersOracle(Model model) { - return "users"; - } - - @RequestMapping(value="/users/paginated/orcl", method=RequestMethod.GET) - @ResponseBody - public String listUsersPaginatedForOracle(HttpServletRequest request, HttpServletResponse response, Model model) { - - DataTableRequest dataTableInRQ = new DataTableRequest(request); - PaginationCriteria pagination = dataTableInRQ.getPaginationRequest(); - String baseQuery = "SELECT USER_ID as id, USER_NAME as name, SALARY as salary FROM MYUSERS"; - String paginatedQuery = AppUtil.buildPaginatedQueryForOracle(baseQuery, pagination); - - System.out.println(paginatedQuery); - - Query query = entityManager.createNativeQuery(paginatedQuery, UserModel.class); - - @SuppressWarnings("unchecked") - List userList = query.getResultList(); - - DataTableResults dataTableResult = new DataTableResults(); - dataTableResult.setDraw(dataTableInRQ.getDraw()); - dataTableResult.setListOfDataObjects(userList); - if (!AppUtil.isObjectEmpty(userList)) { - dataTableResult.setRecordsTotal(userList.get(0).getTotalRecords() - .toString()); - if (dataTableInRQ.getPaginationRequest().isFilterByEmpty()) { - dataTableResult.setRecordsFiltered(userList.get(0).getTotalRecords() - .toString()); - } else { - dataTableResult.setRecordsFiltered(Integer.toString(userList.size())); - } - } - return new Gson().toJson(dataTableResult); - } - - @RequestMapping(value="/adduser", method=RequestMethod.POST) - public String addUser(@ModelAttribute UserModel userModel, Model model) { - if(null != userModel) { - - if(!AppUtil.isObjectEmpty(userModel.getId()) && - !AppUtil.isObjectEmpty(userModel.getName()) && - !AppUtil.isObjectEmpty(userModel.getSalary())) { - - User u = new User(); - u.setId(userModel.getId()); - u.setName(userModel.getName()); - u.setSalary(userModel.getSalary()); - userRepo.save(u); - } - } - return "redirect:/"; - } - -} +/** + * + */ +package com.opencodez.controllers; + +import java.util.ArrayList; +import java.util.List; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Direction; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; +import com.google.gson.Gson; +import com.opencodez.domain.User; +import com.opencodez.domain.UserModel; +import com.opencodez.domain.pagination.DataTableRequest; +import com.opencodez.domain.pagination.DataTableResults; +import com.opencodez.domain.pagination.PaginationCriteria; +import com.opencodez.repo.GenericRepo; +import com.opencodez.repo.GenericPagingRepository; +import com.opencodez.repo.UserRepository; +import com.opencodez.util.AppUtil; + +/** + * @author pavan.solapure + * + */ +@Controller +public class BaseController { + + @Autowired + private UserRepository userRepo; + + @Autowired + private GenericRepo genericRepo; + + @Autowired + private GenericPagingRepository pagingRepo; + + /** The entity manager. */ + @PersistenceContext + private EntityManager entityManager; + + + @RequestMapping(value="/", method = RequestMethod.GET) + public ModelAndView home(@RequestParam(value = "name", defaultValue = "World") String name) { + ModelAndView mv = new ModelAndView("index"); + mv.addObject("userModel", new UserModel()); + List userList = genericRepo.getUserModel(); + mv.addObject("userlist", userList); + return mv; + } + + @RequestMapping(value="/users/mysql", method=RequestMethod.GET) + public String listUsers(Model model) { + return "users_mysql"; + } + + @RequestMapping(value="/users/paginated/mysql", method=RequestMethod.GET) + @ResponseBody + public String listUsersPaginated(HttpServletRequest request, HttpServletResponse response, Model model) { + + DataTableRequest dataTableInRQ = new DataTableRequest(request); + PaginationCriteria pagination = dataTableInRQ.getPaginationRequest(); + + String baseQuery = "SELECT USER_ID as id, USER_NAME as name, SALARY as salary, (SELECT COUNT(1) FROM MYUSERS) AS total_records FROM MYUSERS"; + String paginatedQuery = AppUtil.buildPaginatedQuery(baseQuery, pagination); + + System.out.println(paginatedQuery); + + Query query = entityManager.createNativeQuery(paginatedQuery, UserModel.class); + + @SuppressWarnings("unchecked") + List userList = query.getResultList(); + + DataTableResults dataTableResult = new DataTableResults(); + dataTableResult.setDraw(dataTableInRQ.getDraw()); + dataTableResult.setListOfDataObjects(userList); + if (!AppUtil.isObjectEmpty(userList)) { + dataTableResult.setRecordsTotal(userList.get(0).getTotalRecords() + .toString()); + if (dataTableInRQ.getPaginationRequest().isFilterByEmpty()) { + dataTableResult.setRecordsFiltered(userList.get(0).getTotalRecords() + .toString()); + } else { + dataTableResult.setRecordsFiltered(Integer.toString(userList.size())); + } + } + return new Gson().toJson(dataTableResult); + } + + @RequestMapping(value="/users/oracle", method=RequestMethod.GET) + public String listUsersOracle(Model model) { + return "users"; + } + + @RequestMapping(value="/users/paginated/orcl", method=RequestMethod.GET) + @ResponseBody + public String listUsersPaginatedForOracle(HttpServletRequest request, HttpServletResponse response, Model model) { + DataTableRequest dataTableInRQ = new DataTableRequest(request); + String sortBy = dataTableInRQ.getOrder().getData(); + Direction dir = Direction.fromString(dataTableInRQ.getOrder().getSortDir()); + Sort sort = new Sort(new Sort.Order(dir, sortBy)); + + Integer pageNo =(Integer.parseInt(request.getParameter(PaginationCriteria.PAGE_NO))); + Integer pageSize = (Integer.parseInt(request.getParameter(PaginationCriteria.PAGE_SIZE))); + Pageable pageable = new PageRequest(pageNo / pageSize, pageSize, sort); + + Page page = pagingRepo.findAll(pageable); + List userListOld = page.getContent(); + + List userList = new ArrayList(); + for(User user : userListOld) { + UserModel temp = new UserModel(); + temp.setId(user.getId()); + temp.setName(user.getName()); + temp.setSalary(user.getSalary()); + temp.setTotalRecords(20); + userList.add(temp); + } + + DataTableResults dataTableResult = new DataTableResults(); + + dataTableResult.setDraw(dataTableInRQ.getDraw()); + dataTableResult.setListOfDataObjects(userList); + if (!AppUtil.isObjectEmpty(userList)) { + dataTableResult.setRecordsTotal(userList.get(0).getTotalRecords() + .toString()); + if (dataTableInRQ.getPaginationRequest().isFilterByEmpty()) { + dataTableResult.setRecordsFiltered(userList.get(0).getTotalRecords() + .toString()); + } else { + dataTableResult.setRecordsFiltered(Integer.toString(userList.size())); + } + } + return new Gson().toJson(dataTableResult); + } + + @RequestMapping(value="/adduser", method=RequestMethod.POST) + public String addUser(@ModelAttribute UserModel userModel, Model model) { + if(null != userModel) { + + if(!AppUtil.isObjectEmpty(userModel.getId()) && + !AppUtil.isObjectEmpty(userModel.getName()) && + !AppUtil.isObjectEmpty(userModel.getSalary())) { + + User u = new User(); + u.setId(userModel.getId()); + u.setName(userModel.getName()); + u.setSalary(userModel.getSalary()); + userRepo.save(u); + } + } + return "redirect:/"; + } + +} diff --git a/spring-boot-datatable/src/main/java/com/opencodez/repo/GenericPagingRepository.java b/spring-boot-datatable/src/main/java/com/opencodez/repo/GenericPagingRepository.java new file mode 100644 index 0000000..d644868 --- /dev/null +++ b/spring-boot-datatable/src/main/java/com/opencodez/repo/GenericPagingRepository.java @@ -0,0 +1,8 @@ +package com.opencodez.repo; + +import org.springframework.data.jpa.repository.JpaRepository; +import com.opencodez.domain.User; + +public interface GenericPagingRepository extends JpaRepository { + +} diff --git a/spring-boot-datatable/src/main/resources/application.properties b/spring-boot-datatable/src/main/resources/application.properties index c8247ce..e206ad5 100644 --- a/spring-boot-datatable/src/main/resources/application.properties +++ b/spring-boot-datatable/src/main/resources/application.properties @@ -1,10 +1,14 @@ +server.port = 8090 spring.thymeleaf.cache=false -spring.datasource.url=jdbc:mysql://localhost:3306/localdb -spring.datasource.username=lessroot -spring.datasource.password=lessroot -spring.datasource.driver-class-name=com.mysql.jdbc.Driver -spring.jpa.properties.javax.persistence.validation.mode=none +# Enabling H2 Console +spring.h2.console.enabled=false + +#spring.datasource.url=jdbc:mysql://localhost:3306/localdb +#spring.datasource.username=lessroot +#spring.datasource.password=lessroot +#spring.datasource.driver-class-name=com.mysql.jdbc.Driver +#spring.jpa.properties.javax.persistence.validation.mode=none #spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe #spring.datasource.username=pavans diff --git a/spring-boot-datatable/src/main/resources/data.sql b/spring-boot-datatable/src/main/resources/data.sql new file mode 100644 index 0000000..9d86b1f --- /dev/null +++ b/spring-boot-datatable/src/main/resources/data.sql @@ -0,0 +1,20 @@ +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('1', 'user1', '100'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('2', 'user2', '200'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('3', 'user3', '300'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('4', 'user4', '400'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('5', 'user5', '500'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('6', 'user6', '600'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('7', 'user7', '700'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('8', 'user8', '800'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('9', 'user9', '900'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('10', 'user10', '1000'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('11', 'user11', '1100'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('12', 'user12', '1200'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('13', 'user13', '1300'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('14', 'user14', '1400'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('15', 'user15', '1500'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('16', 'user16', '1600'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('17', 'user17', '1700'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('18', 'user18', '1800'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('19', 'user19', '1900'); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('20', 'user20', '2000'); \ No newline at end of file From 95909c0d1ab8fb86c5edf79c8774c48a7687bb26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C4=B1van=C3=A7=20Sahici?= Date: Tue, 18 Dec 2018 23:26:09 +0300 Subject: [PATCH 2/6] use numeric data type for salary for better sorting --- .../main/java/com/opencodez/domain/User.java | 6 +-- .../java/com/opencodez/domain/UserModel.java | 6 +-- .../src/main/resources/data.sql | 40 +++++++++---------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java b/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java index a37d6d4..e222326 100644 --- a/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java +++ b/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java @@ -24,7 +24,7 @@ public class User { private String name; @Column(name = "SALARY") - private String salary; + private Integer salary; /** @@ -54,13 +54,13 @@ public void setName(String name) { /** * @return the salary */ - public String getSalary() { + public Integer getSalary() { return salary; } /** * @param salary the salary to set */ - public void setSalary(String salary) { + public void setSalary(Integer salary) { this.salary = salary; } // /** diff --git a/spring-boot-datatable/src/main/java/com/opencodez/domain/UserModel.java b/spring-boot-datatable/src/main/java/com/opencodez/domain/UserModel.java index 8bdfa76..197517c 100644 --- a/spring-boot-datatable/src/main/java/com/opencodez/domain/UserModel.java +++ b/spring-boot-datatable/src/main/java/com/opencodez/domain/UserModel.java @@ -17,7 +17,7 @@ public class UserModel { @Id private Long id; private String name; - private String salary; + private Integer salary; private Integer totalRecords; @@ -57,7 +57,7 @@ public void setName(String name) { /** * @return the salary */ - public String getSalary() { + public Integer getSalary() { return salary; } @@ -65,7 +65,7 @@ public String getSalary() { * @param salary * the salary to set */ - public void setSalary(String salary) { + public void setSalary(Integer salary) { this.salary = salary; } diff --git a/spring-boot-datatable/src/main/resources/data.sql b/spring-boot-datatable/src/main/resources/data.sql index 9d86b1f..3c79090 100644 --- a/spring-boot-datatable/src/main/resources/data.sql +++ b/spring-boot-datatable/src/main/resources/data.sql @@ -1,20 +1,20 @@ -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('1', 'user1', '100'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('2', 'user2', '200'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('3', 'user3', '300'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('4', 'user4', '400'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('5', 'user5', '500'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('6', 'user6', '600'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('7', 'user7', '700'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('8', 'user8', '800'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('9', 'user9', '900'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('10', 'user10', '1000'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('11', 'user11', '1100'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('12', 'user12', '1200'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('13', 'user13', '1300'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('14', 'user14', '1400'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('15', 'user15', '1500'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('16', 'user16', '1600'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('17', 'user17', '1700'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('18', 'user18', '1800'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('19', 'user19', '1900'); -INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('20', 'user20', '2000'); \ No newline at end of file +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('1', 'user1', 100); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('2', 'user2', 200); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('3', 'user3', 300); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('4', 'user4', 400); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('5', 'user5', 500); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('6', 'user6', 600); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('7', 'user7', 700); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('8', 'user8', 800); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('9', 'user9', 900); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('10', 'user10', 1000); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('11', 'user11', 1100); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('12', 'user12', 1200); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('13', 'user13', 1300); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('14', 'user14', 1400); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('15', 'user15', 1500); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('16', 'user16', 1600); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('17', 'user17', 1700); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('18', 'user18', 1800); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('19', 'user19', 1900); +INSERT INTO MYUSERS (USER_ID, USER_NAME, SALARY) VALUES ('20', 'user20', 2000); \ No newline at end of file From e6bb5fb5e4707cc784386fdd82a504c6ad580234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C4=B1van=C3=A7=20Sahici?= Date: Sun, 23 Dec 2018 14:06:01 +0300 Subject: [PATCH 3/6] use a generic pagination template for all db vendors --- .../opencodez/controllers/BaseController.java | 43 +----------- .../src/main/resources/templates/index.html | 4 +- .../main/resources/templates/users_mysql.html | 65 ------------------- 3 files changed, 3 insertions(+), 109 deletions(-) delete mode 100644 spring-boot-datatable/src/main/resources/templates/users_mysql.html diff --git a/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java b/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java index 2b5df3b..ff04573 100644 --- a/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java +++ b/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java @@ -7,7 +7,6 @@ import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; -import javax.persistence.Query; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; @@ -64,46 +63,8 @@ public ModelAndView home(@RequestParam(value = "name", defaultValue = "World") S mv.addObject("userlist", userList); return mv; } - - @RequestMapping(value="/users/mysql", method=RequestMethod.GET) - public String listUsers(Model model) { - return "users_mysql"; - } - - @RequestMapping(value="/users/paginated/mysql", method=RequestMethod.GET) - @ResponseBody - public String listUsersPaginated(HttpServletRequest request, HttpServletResponse response, Model model) { - - DataTableRequest dataTableInRQ = new DataTableRequest(request); - PaginationCriteria pagination = dataTableInRQ.getPaginationRequest(); - - String baseQuery = "SELECT USER_ID as id, USER_NAME as name, SALARY as salary, (SELECT COUNT(1) FROM MYUSERS) AS total_records FROM MYUSERS"; - String paginatedQuery = AppUtil.buildPaginatedQuery(baseQuery, pagination); - - System.out.println(paginatedQuery); - - Query query = entityManager.createNativeQuery(paginatedQuery, UserModel.class); - - @SuppressWarnings("unchecked") - List userList = query.getResultList(); - - DataTableResults dataTableResult = new DataTableResults(); - dataTableResult.setDraw(dataTableInRQ.getDraw()); - dataTableResult.setListOfDataObjects(userList); - if (!AppUtil.isObjectEmpty(userList)) { - dataTableResult.setRecordsTotal(userList.get(0).getTotalRecords() - .toString()); - if (dataTableInRQ.getPaginationRequest().isFilterByEmpty()) { - dataTableResult.setRecordsFiltered(userList.get(0).getTotalRecords() - .toString()); - } else { - dataTableResult.setRecordsFiltered(Integer.toString(userList.size())); - } - } - return new Gson().toJson(dataTableResult); - } - - @RequestMapping(value="/users/oracle", method=RequestMethod.GET) + + @RequestMapping(value="/users", method=RequestMethod.GET) public String listUsersOracle(Model model) { return "users"; } diff --git a/spring-boot-datatable/src/main/resources/templates/index.html b/spring-boot-datatable/src/main/resources/templates/index.html index dc346cc..312c80b 100644 --- a/spring-boot-datatable/src/main/resources/templates/index.html +++ b/spring-boot-datatable/src/main/resources/templates/index.html @@ -54,9 +54,7 @@

Add User Data

-Paginated List using Oracle -
-Paginated List using MySQL +Paginated List
diff --git a/spring-boot-datatable/src/main/resources/templates/users_mysql.html b/spring-boot-datatable/src/main/resources/templates/users_mysql.html deleted file mode 100644 index 3f509ed..0000000 --- a/spring-boot-datatable/src/main/resources/templates/users_mysql.html +++ /dev/null @@ -1,65 +0,0 @@ - - - -Home - - - - - - - - - - - - - - - - - - - -
- -
- - -
- - - - - - - - - -
IdNameSalary
-
- - -
- -
- - - \ No newline at end of file From b4dca88f8aacfa9578bd67bb0545bfd1f41b8595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C4=B1van=C3=A7=20Sahici?= Date: Sun, 23 Dec 2018 14:26:38 +0300 Subject: [PATCH 4/6] use a single jpa repo for all purposes in the demo --- .../opencodez/controllers/BaseController.java | 39 ++++++----------- .../main/java/com/opencodez/domain/User.java | 31 ++++++++------ .../repo/GenericPagingRepository.java | 8 ---- .../java/com/opencodez/repo/GenericRepo.java | 19 --------- .../com/opencodez/repo/GenericRepoImpl.java | 42 ------------------- .../com/opencodez/repo/UserRepository.java | 17 +------- .../src/main/resources/static/js/admin.js | 2 +- .../main/resources/static/js/admin_mysql.js | 19 --------- 8 files changed, 34 insertions(+), 143 deletions(-) delete mode 100644 spring-boot-datatable/src/main/java/com/opencodez/repo/GenericPagingRepository.java delete mode 100644 spring-boot-datatable/src/main/java/com/opencodez/repo/GenericRepo.java delete mode 100644 spring-boot-datatable/src/main/java/com/opencodez/repo/GenericRepoImpl.java delete mode 100644 spring-boot-datatable/src/main/resources/static/js/admin_mysql.js diff --git a/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java b/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java index ff04573..d1c68c6 100644 --- a/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java +++ b/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java @@ -3,7 +3,6 @@ */ package com.opencodez.controllers; -import java.util.ArrayList; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @@ -29,8 +28,6 @@ import com.opencodez.domain.pagination.DataTableRequest; import com.opencodez.domain.pagination.DataTableResults; import com.opencodez.domain.pagination.PaginationCriteria; -import com.opencodez.repo.GenericRepo; -import com.opencodez.repo.GenericPagingRepository; import com.opencodez.repo.UserRepository; import com.opencodez.util.AppUtil; @@ -40,26 +37,18 @@ */ @Controller public class BaseController { - @Autowired private UserRepository userRepo; - @Autowired - private GenericRepo genericRepo; - - @Autowired - private GenericPagingRepository pagingRepo; - /** The entity manager. */ @PersistenceContext private EntityManager entityManager; - @RequestMapping(value="/", method = RequestMethod.GET) public ModelAndView home(@RequestParam(value = "name", defaultValue = "World") String name) { ModelAndView mv = new ModelAndView("index"); - mv.addObject("userModel", new UserModel()); - List userList = genericRepo.getUserModel(); + mv.addObject("userModel", new User()); + List userList = userRepo.findAll(); mv.addObject("userlist", userList); return mv; } @@ -69,7 +58,7 @@ public String listUsersOracle(Model model) { return "users"; } - @RequestMapping(value="/users/paginated/orcl", method=RequestMethod.GET) + @RequestMapping(value="/users/paginated", method=RequestMethod.GET) @ResponseBody public String listUsersPaginatedForOracle(HttpServletRequest request, HttpServletResponse response, Model model) { DataTableRequest dataTableInRQ = new DataTableRequest(request); @@ -81,20 +70,16 @@ public String listUsersPaginatedForOracle(HttpServletRequest request, HttpServle Integer pageSize = (Integer.parseInt(request.getParameter(PaginationCriteria.PAGE_SIZE))); Pageable pageable = new PageRequest(pageNo / pageSize, pageSize, sort); - Page page = pagingRepo.findAll(pageable); - List userListOld = page.getContent(); - - List userList = new ArrayList(); - for(User user : userListOld) { - UserModel temp = new UserModel(); - temp.setId(user.getId()); - temp.setName(user.getName()); - temp.setSalary(user.getSalary()); - temp.setTotalRecords(20); - userList.add(temp); - } + Page page = userRepo.findAll(pageable); + + Integer totalRecords = (int)page.getTotalElements(); + + List userList = page.getContent(); + for(User user : userList) { + user.setTotalRecords(totalRecords); + } - DataTableResults dataTableResult = new DataTableResults(); + DataTableResults dataTableResult = new DataTableResults(); dataTableResult.setDraw(dataTableInRQ.getDraw()); dataTableResult.setListOfDataObjects(userList); diff --git a/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java b/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java index e222326..a8e0c4d 100644 --- a/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java +++ b/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java @@ -7,6 +7,7 @@ import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; +import javax.persistence.Transient; /** * @author pavan.solapure @@ -25,6 +26,11 @@ public class User { @Column(name = "SALARY") private Integer salary; + + private Integer totalRecords; + + @Transient + private Integer rn; /** @@ -63,16 +69,17 @@ public Integer getSalary() { public void setSalary(Integer salary) { this.salary = salary; } -// /** -// * @return the totalRecords -// */ -// public Integer getTotalRecords() { -// return totalRecords; -// } -// /** -// * @param totalRecords the totalRecords to set -// */ -// public void setTotalRecords(Integer totalRecords) { -// this.totalRecords = totalRecords; -// } + + public Integer getTotalRecords() { + return totalRecords; + } + public void setTotalRecords(Integer totalRecords) { + this.totalRecords = totalRecords; + } + public Integer getRn() { + return rn; + } + public void setRn(Integer rn) { + this.rn = rn; + } } diff --git a/spring-boot-datatable/src/main/java/com/opencodez/repo/GenericPagingRepository.java b/spring-boot-datatable/src/main/java/com/opencodez/repo/GenericPagingRepository.java deleted file mode 100644 index d644868..0000000 --- a/spring-boot-datatable/src/main/java/com/opencodez/repo/GenericPagingRepository.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.opencodez.repo; - -import org.springframework.data.jpa.repository.JpaRepository; -import com.opencodez.domain.User; - -public interface GenericPagingRepository extends JpaRepository { - -} diff --git a/spring-boot-datatable/src/main/java/com/opencodez/repo/GenericRepo.java b/spring-boot-datatable/src/main/java/com/opencodez/repo/GenericRepo.java deleted file mode 100644 index 195912b..0000000 --- a/spring-boot-datatable/src/main/java/com/opencodez/repo/GenericRepo.java +++ /dev/null @@ -1,19 +0,0 @@ -/** - * - */ -package com.opencodez.repo; - -import java.util.List; - -import com.opencodez.domain.UserModel; - -/** - * @author pavan.solapure - * - */ -public interface GenericRepo { - - List getUserModel(); - - -} diff --git a/spring-boot-datatable/src/main/java/com/opencodez/repo/GenericRepoImpl.java b/spring-boot-datatable/src/main/java/com/opencodez/repo/GenericRepoImpl.java deleted file mode 100644 index 6debe1d..0000000 --- a/spring-boot-datatable/src/main/java/com/opencodez/repo/GenericRepoImpl.java +++ /dev/null @@ -1,42 +0,0 @@ -/** - * - */ -package com.opencodez.repo; - -import java.util.List; - -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.Query; - -import org.springframework.stereotype.Repository; - -import com.opencodez.domain.UserModel; - -/** - * @author pavan.solapure - * - */ -@Repository -public class GenericRepoImpl implements GenericRepo { - - @PersistenceContext - private EntityManager entityManager; - - /* (non-Javadoc) - * @see com.opencodez.repo.GenericRepo#getUserModel() - */ - @Override - public List getUserModel() { - - String qry = "SELECT USER_ID as id, USER_NAME as name, salary as salary, 1 as total_records FROM MYUSERS"; - Query query = entityManager.createNativeQuery(qry, - UserModel.class); - - @SuppressWarnings("unchecked") - List daoDtolist = query.getResultList(); - - return daoDtolist; - } - -} diff --git a/spring-boot-datatable/src/main/java/com/opencodez/repo/UserRepository.java b/spring-boot-datatable/src/main/java/com/opencodez/repo/UserRepository.java index 04b8a58..23600ca 100644 --- a/spring-boot-datatable/src/main/java/com/opencodez/repo/UserRepository.java +++ b/spring-boot-datatable/src/main/java/com/opencodez/repo/UserRepository.java @@ -1,21 +1,8 @@ -/** - * - */ package com.opencodez.repo; -import java.util.List; - import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; - import com.opencodez.domain.User; -/** - * @author pavan.solapure - * - */ -public interface UserRepository extends JpaRepository { - - @Query(value = "SELECT * FROM MYUSERS", nativeQuery = true) - List findAllByUsernames(List listOfUsernames); +public interface UserRepository extends JpaRepository { + } diff --git a/spring-boot-datatable/src/main/resources/static/js/admin.js b/spring-boot-datatable/src/main/resources/static/js/admin.js index 74c86d6..bafcb14 100644 --- a/spring-boot-datatable/src/main/resources/static/js/admin.js +++ b/spring-boot-datatable/src/main/resources/static/js/admin.js @@ -4,7 +4,7 @@ $( document ).ready(function() { "serverSide": true, "pageLength": 5, "ajax": { - "url": "/users/paginated/orcl", + "url": "/users/paginated", "data": function ( data ) { //process data before sent to server. }}, diff --git a/spring-boot-datatable/src/main/resources/static/js/admin_mysql.js b/spring-boot-datatable/src/main/resources/static/js/admin_mysql.js deleted file mode 100644 index 109e9dc..0000000 --- a/spring-boot-datatable/src/main/resources/static/js/admin_mysql.js +++ /dev/null @@ -1,19 +0,0 @@ -$( document ).ready(function() { - $('#paginatedTable').DataTable( { - "processing": true, - "serverSide": true, - "pageLength": 5, - "ajax": { - "url": "/users/paginated/mysql", - "data": function ( data ) { - //process data before sent to server. - }}, - "columns": [ - { "data": "id", "name" : "ID", "title" : "ID" }, - { "data": "name", "name" : "Name" , "title" : "Name"}, - { "data": "salary", "name" : "Salary" , "title" : "Salary"} - ] - }); - - $('#paginatedTable').dataTable().fnSetFilteringEnterPress(); -}); \ No newline at end of file From c2627f479db0cebf98711d631ce9ef2cf20250db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C4=B1van=C3=A7=20Sahici?= Date: Sun, 23 Dec 2018 14:38:16 +0300 Subject: [PATCH 5/6] remove obsolete code form spring-boot-datatable project --- .../opencodez/controllers/BaseController.java | 3 +- .../main/java/com/opencodez/domain/User.java | 13 +-- .../java/com/opencodez/domain/UserModel.java | 95 ------------------- 3 files changed, 2 insertions(+), 109 deletions(-) delete mode 100644 spring-boot-datatable/src/main/java/com/opencodez/domain/UserModel.java diff --git a/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java b/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java index d1c68c6..6d29ba2 100644 --- a/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java +++ b/spring-boot-datatable/src/main/java/com/opencodez/controllers/BaseController.java @@ -24,7 +24,6 @@ import org.springframework.web.servlet.ModelAndView; import com.google.gson.Gson; import com.opencodez.domain.User; -import com.opencodez.domain.UserModel; import com.opencodez.domain.pagination.DataTableRequest; import com.opencodez.domain.pagination.DataTableResults; import com.opencodez.domain.pagination.PaginationCriteria; @@ -97,7 +96,7 @@ public String listUsersPaginatedForOracle(HttpServletRequest request, HttpServle } @RequestMapping(value="/adduser", method=RequestMethod.POST) - public String addUser(@ModelAttribute UserModel userModel, Model model) { + public String addUser(@ModelAttribute User userModel, Model model) { if(null != userModel) { if(!AppUtil.isObjectEmpty(userModel.getId()) && diff --git a/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java b/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java index a8e0c4d..2212b53 100644 --- a/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java +++ b/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java @@ -7,7 +7,6 @@ import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; -import javax.persistence.Transient; /** * @author pavan.solapure @@ -28,10 +27,6 @@ public class User { private Integer salary; private Integer totalRecords; - - @Transient - private Integer rn; - /** * @return the id @@ -75,11 +70,5 @@ public Integer getTotalRecords() { } public void setTotalRecords(Integer totalRecords) { this.totalRecords = totalRecords; - } - public Integer getRn() { - return rn; - } - public void setRn(Integer rn) { - this.rn = rn; - } + } } diff --git a/spring-boot-datatable/src/main/java/com/opencodez/domain/UserModel.java b/spring-boot-datatable/src/main/java/com/opencodez/domain/UserModel.java deleted file mode 100644 index 197517c..0000000 --- a/spring-boot-datatable/src/main/java/com/opencodez/domain/UserModel.java +++ /dev/null @@ -1,95 +0,0 @@ -/** - * - */ -package com.opencodez.domain; - -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Transient; - -/** - * @author pavan.solapure - * - */ -@Entity -public class UserModel { - - @Id - private Long id; - private String name; - private Integer salary; - - private Integer totalRecords; - - @Transient - private Integer rn; - - /** - * @return the id - */ - public Long getId() { - return id; - } - - /** - * @param id - * the id to set - */ - public void setId(Long id) { - this.id = id; - } - - /** - * @return the name - */ - public String getName() { - return name; - } - - /** - * @param name - * the name to set - */ - public void setName(String name) { - this.name = name; - } - - /** - * @return the salary - */ - public Integer getSalary() { - return salary; - } - - /** - * @param salary - * the salary to set - */ - public void setSalary(Integer salary) { - this.salary = salary; - } - - /** - * @return the totalRecords - */ - public Integer getTotalRecords() { - return totalRecords; - } - - /** - * @param totalRecords - * the totalRecords to set - */ - public void setTotalRecords(Integer totalRecords) { - this.totalRecords = totalRecords; - } - - public Integer getRn() { - return rn; - } - - public void setRn(Integer rn) { - this.rn = rn; - } - -} From d8c7878bec3bc22d20325d62d4354e1c74a8268e Mon Sep 17 00:00:00 2001 From: "Sahici, Kivanc" Date: Mon, 24 Dec 2018 13:41:32 +0300 Subject: [PATCH 6/6] mark totalRecords as Transient --- .../src/main/java/com/opencodez/domain/User.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java b/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java index 2212b53..ec7883b 100644 --- a/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java +++ b/spring-boot-datatable/src/main/java/com/opencodez/domain/User.java @@ -7,6 +7,7 @@ import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; +import javax.persistence.Transient; /** * @author pavan.solapure @@ -26,6 +27,7 @@ public class User { @Column(name = "SALARY") private Integer salary; + @Transient private Integer totalRecords; /**