Visit http://start.spring.io and initialize project with below dependencies:
Spring Web
Spring Data Jpa: manage sql queries
Mysql Driver: connect to db
Projectlombok : auto configuring getter setter and constructor in Entity
Devtools: auto restart the application on changes
Thymeleaf: engine for creating frontend (avoid if frontend is not required and app can be tested by postman)
Create below packages:
- Model
- Repository
- Service
- Controller
=======(application.properties)=======
spring.datasource.url=jdbc:mysql://localhost:3306/restcruddb
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=practice
spring.datasource.password=toor
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
===================================
Create an Entity “Book” with 3 fields bookId, bookName and bookAuthor
Also generate getter and setter along with toString.
Now create a Repository Package and inside create an interface which extends JpaRepository<ModelName, ModelIdType(primary key data type)>
Create Service package which will contain the autowired repository and methods with some business logic
Create a Controller package with BookController class.
Controller will have field “BookRepository” autowired
Project Structure
Postman Test
project git repo: https://github.com/AdarshKvT/restcrud.git