Creating a Java Rest API using MySQL -CRUD OPERATIONS-HTTP METHODS-GET POST DELETE PUT

 Spring Boot JPA(web service) with MySQL

—---------------------------------------------------


Hi this a document with steps to complete a webservice development using MySQL


Follow the steps:


1.Create a new Spring Boot Application

  1. STS- new Project

  2. start.spring.io


2.Add 3 modules

a)SpringWeb

b)MySQL

c)Actuator(optional but good)


3.Run the application.

Application will show error as we have to configure MySQL connection


4. Open the application.properties file in resources folder

5. Write these entries

Create  a database in mysql and give the credentials like this(jpa1)


spring.application.name=demo-5

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/jpa1

spring.datasource.username=root

spring.datasource.password=root123

spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto=update

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect



6.Save and run.Now application will run,but no output will come.

7.Create 4 packages in the main package


controller

model

service

repository


8.model


package com.example.demo.model;


import jakarta.persistence.Entity;

import jakarta.persistence.Id;


@Entity

public class Trainee {

@Id

private int tid;

private String tname;

private String tgender;

private String ttechnology;

public Trainee(int tid, String tname, String tgender, String ttechnology) {

super();

this.tid = tid;

this.tname = tname;

this.tgender = tgender;

this.ttechnology = ttechnology;

}

public Trainee() {

super();

// TODO Auto-generated constructor stub

}

public int getTid() {

return tid;

}

public void setTid(int tid) {

this.tid = tid;

}

public String getTname() {

return tname;

}

public void setTname(String tname) {

this.tname = tname;

}

public String getTgender() {

return tgender;

}

public void setTgender(String tgender) {

this.tgender = tgender;

}

public String getTtechnology() {

return ttechnology;

}

public void setTtechnology(String ttechnology) {

this.ttechnology = ttechnology;

}


}



9.respository

package com.example.demo.repository;


import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.stereotype.Repository;


import com.example.demo.model.Trainee;


@Repository

public interface TraineeRepository extends JpaRepository<Trainee,Integer>{


}



10.service


package com.example.demo.service;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;


import com.example.demo.model.Trainee;

import com.example.demo.repository.TraineeRepository;


@Service

public class TraineeService {

    @Autowired TraineeRepository traineerepository;

public List<Trainee> getAlltrainees() {

// TODO Auto-generated method stub

return traineerepository.findAll();

}

public void addStudent(Trainee trainee) {

// TODO Auto-generated method stub

traineerepository.save(trainee);

}

public Trainee getTraineeByTid(int tid) {

// TODO Auto-generated method stub

return traineerepository.findById(tid).orElse(new Trainee());

}

public void updateStudent(Trainee trainee) {

// TODO Auto-generated method stub

traineerepository.save(trainee);

}

public void deleteTrainee(int tno) {

// TODO Auto-generated method stub

traineerepository.deleteById(tno);

}


}



11.controller

package com.example.demo.controller;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.PutMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;


import com.example.demo.model.Trainee;

import com.example.demo.service.TraineeService;


@RestController

public class TraineeController {

@Autowired TraineeService traineeservice;

@GetMapping("/trainees")

public List<Trainee> getAllTrainees(){

return traineeservice.getAlltrainees();

}

@PostMapping("/addTrainee")

public String addStudent(@RequestBody Trainee trainee) {

traineeservice.addStudent(trainee);

return "trainee data added";

}

//for selecting one row

@GetMapping("/Trainee/{tid}")

public Trainee getTrainee(@PathVariable("tid") int tid) {

return traineeservice.getTraineeByTid(tid);

}

//update trainee data

@PutMapping("/updateTrainee")

public String updateTrainee(@RequestBody Trainee trainee) {

traineeservice.updateStudent(trainee);

return "trainee data updated";

}

@DeleteMapping("/deleteTrainee/{tno}")

public String deleteTrainee(@PathVariable int tno) {

traineeservice.deleteTrainee(tno);

return "trainee data deleted";

}

}


12.run the application


13.use browser to test get methods


14.use postman to test all http methods



No comments:

Post a Comment

python training with real time projects

java script-text to speech-assignment-Fatima Solkar Final Year-BE I.T FAMT-Ratnagiri-Maharastra

Page Title Mandvi Beach Ratnagiri At a distance of 2 km from Ratnagiri Bus Stand, Mandvi Beach i...

Technical Skills for B.Com and B.B.A