Spring JPA Web Support

Spring JPA Web support ships by a module for convenient programming with repository model.
– A DomainClassConverter helps to resolve an repository instance from request parameters or path variables.
– HandlerMethodArgumentResolver is helper for resolving Pageable and Sort instances from request parameters.

I. Technology for Spring JPA Web Support

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.0.RELEASE

II. Overview
1. Structure of Project

springjpa-webmvc-structure-project

2. Step to do

– Create Spring Boot project
– Configure PostgreSql Datasource
– Create Data Model
– Create Repository
– Create Spring MVC Web Controller
– Create Postgresql table
– Run & Enjoy Result

III. Practices
1. Create Spring Boot project

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project info:

springjpa-webmvc-create-project

Press Next, add dependencies:
– Add Spring MVC dependencies: select Web, choose Web
– Add JPA & PostgreSQL: select SQL, choose JPA and PostSQL

springjpa-webmvc-add-dependencies

Open pom.xml file, check dependencies:

2. Configure PostgreSql Datasource

Open application.properties, add configuration:

3. Create Data Model

Create a simple Customer model, that maps with customer table in postgresql

4. Create Repository

PagingAndSortingRepository: Extension of CrudRepository to provide additional methods to retrieve entities using the pagination and sorting abstraction.

Extends PagingAndSortingRepositoryThe is registration that enables Pageable and Sort.

5. Create Spring MVC Web Controller

Create a SimpleWebController with 3 @RequestMapping:
– @RequestMapping(value=”/save”, method=RequestMethod.GET): save 100 Customer instances to PostgreSQL database.
– @RequestMapping(value=”/load”, method=RequestMethod.GET): load a Customer by “id” value of request parameter
Function: public Customer loadCustomer(@RequestParam(“id”) Customer customer, Model model)
– @RequestMapping(value=”/custs”,method=RequestMethod.GET): get a page base on value of: Pageable request parameter: page (Page to retrieve), page.size(Size of the page to retrieve)
Function: Page list( Pageable pageable)

6. Create Postgresql table

Use SQL Editor of pgAdminIII, create customer table:

Images:
spring-jpa-support-web-sql

7. Run & Enjoy Result

Build Project with Maven, goals:
clean install

Run project with mode: Spring Boot App

– Request:
http://localhost:8080/save

Result:
save-postgresql-spring-jpa-support-web

– Request:
http://localhost:8080/load?id=8866279510187862638
Note: 8866279510187862638 is a customer id

Result:

load-spring-jpa-support-web

– Request pagination:
http://localhost:8080/custs?page=0&size=2

Result:

pagination-spring-jpa-support-web

IV. Source code

springjpa-mvc

By JavaSampleApproach | September 11, 2016.

Last updated on June 4, 2017.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*