Google News
logo
Java Spring Boot - Interview Questions
How to insert data in mysql using spring boot?
First configure mysql in your spring boot application.
 
Then you can map your entities with your db tables using JPA.
 
And with the help of save() method in JPA, you can directly insert your data into your database.
 
@RestController
@RequestMapping("/greatleasrning")
public class Controller {
@Autowired
private final FreeTimeLearningRepository freetimeLearningRepository;
public Controller(GreatLearningRepository freetimeLearningRepository) {
this. freetimelearningRepository = freetimeLearningRepository;
}
 
In the above case, your data which may be in JSON format can be inserted successfully into the database.
 
@RequestMapping(method = RequestMethod.POST)
ResponseEntity<?> insert(@RequestBody Course course) {
freetimeLearningRepository.save(course);
return ResponseEntity.accepted().build();
}
}

 

Advertisement