Skip to content

Latest commit

 

History

History
183 lines (129 loc) · 4.93 KB

File metadata and controls

183 lines (129 loc) · 4.93 KB

Spring Boot RestAPI

RestApi with MySQL database with CRUD operations

This RestAPI uses MySQL database and have table student and have attributes as follows.

Student
Id(primary key)
name
city
gender
pincode
contact number
birth date

Prerequisites

  • Installed MySQL server and workbench on system.
  • java 11 installed on system
  • Any JAVA IDE is required with spring boot plugin installed and configured.
  • Postman for sending requests and receiving response.

Installation

  • Make sure your IDE is configured and MySQL server is up and running.
  • Open Project in IDE and configure application.properties according to your MySQL configuration on your system.
  • Open application.properties and see spring.datasource.url=jdbc:mysql://localhost:3306/paurav line change paurav with database name you want to create.
  • open MySQL workbench and fire this SQL statement to create database of your name which you changed inplace of paurav in that url
create database [your database name which you replaced with paurav in above url in `application.properties` file].
use [your database name].
  • Run DemoApplication.java as a java application, if everything on your system is configured you should see the console with no errors.
  • Open postman and start using RestAPI.
  • Now you can see the Database in MySQL workbench also you just need to create datbase that's it all things will operated by spring boot itself.

Usage

  • Make sure your MySQL server and application is running in IDE with no errors as stated in Installation section.
  • If you changed application.properties file then see the port number which you assigned on that port id your application is running right now if not the go with the default configuration shown below.

 

Create

 

To add student in database.

url pattenrn: http://localhost:9912/addstudent

{
    "bdate":"29/12/1999",
    "city":"Ahmedabad",
    "contact":"+91-9313711987",
    "gender":"Male",
    "name":"Paurav",
    "pincode":382480
}
  • For example:

Adding student image postman Sending POST request to add one student.

  Adding student image database Student added into the database.

 

To add multiple students in database.

url pattern : http://localhost:9912/addstudents

[
    {
        "bdate":"12/1/2001",
        "city":"Gandhinagar",
        "contact":"+91-1234567763",
        "gender":"Male",
        "name":"pqr",
        "pincode":322480
    },
    {
        "bdate":"12/1/2001",
        "city":"Gandhinagar",
        "contact":"+91-1234567763",
        "gender":"Male",
        "name":"pqr",
        "pincode":322480
    }
]
  • For Example:

Adding multiple students image postman Sending request to add multiple students in database.

Adding multiple students image Database Multiple students added into the database.  

Read

 

To get list of all students from database

url pattern: http://localhost:9912/students

  • For example:

Adding student image postman Getting all students from database.

To get list of student by ID from database

url pattern: http://localhost:9912/studentbyid/1

  • For Example:

Adding student image postman Getting student by id from database.  

Update

 

To update existing student entry in database

url pattern: http://localhost:9912/update

  • Passing JSON to update student entry by id.
{
        "id": 2,
        "name": "PSP",
        "city": "A-BAD",
        "gender": "Male",
        "pincode": 381480,
        "bdate": "29/1/1999",
        "contact": "+91-9313711767"
}

 

  • For Example:

Updating student image postman Updating student details in database.

Updating student image database Updated student details in database.

 

Delete

 

To delete student entry in database.

url pattern: http://localhost:9912/delete/5

  • Passing id to delete student entry by that id.

  • For Example:

Deleting student image postman Deleting student details in database.

Deleted student image postman Deleted student details in database.

Thank you!