Skip to content

YeowKinRen/spring-data-jpa-mysql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

accessing-data-mysql

Basic Spring Data JPA with MySQL Database

mysql> create database db_example; -- Creates the new database
mysql> create user 'springuser'@'%' identified by 'ThePassword'; -- Creates the user
mysql> grant all on db_example.* to 'springuser'@'%'; -- Gives all privileges to the new user on the newly created database

You can perform RESTful API requests such as GET, POST, PUT, PATCH, and DELETE requests via Postman or cURL commands. The following are a sample of requests and responses.

GET

curl http://localhost:8080/demo/all

{"id":1,"name":"[email protected]","email":"Julio Fletcher"},{"id":2,"name":"[email protected]","email":"Alison Grant"},{"id":3,"name":"[email protected]","email":"Magdalena Campos"},{"id":4,"name":"[email protected]","email":"Alfons Stemmler"},{"id":5,"name":"[email protected]","email":"Milana Martin"}

You can implement the requests in conjunction with Projection and/or Pagination and Sorting.

POST

curl --data "name=Yeow&[email protected]" http://localhost:8080/demo/add"

DELETE

curl -X DELETE http://localhost:8080/demo/delete/1

USER_ID: 1 DELETED

PUT

curl -X PUT -d "name=newName" http://localhost:8080/demo/put/1

USER_ID: 1 UPDATED

Notes

Spring Boot Annotations

  • @SpringBootApplication: combinations of 3 annotations namely @EnableAutoConfiguration, @ComponentScan, and @Configuration

    • @Configuration: Tags the class as a source of bean definitions for the application context.

    • @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. For example, if spring-webmvc is on the classpath, this annotation flags the application as a web application and activates key behaviors, such as setting up a DispatcherServlet.

    • @ComponentScan: Tells Spring to look for other components, configurations, and services in the com/example package, letting it find the controllers.

  • @Component: annotation that allows Spring to automatically detect our custom beans

  • @Controller, Service, Repository: specialized form of @Component and annotations for controller as front controllers and the management of the REST interface, service as business logic implementation and repository as the access of database and the storage of the entity beans such as CRUD operations

  • @RestController: combination of the @Controller and @ResponseBody annotation

Resource

About

Basic Spring Data JPA with MySQL Database

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages