Spring Data JPA (Stop hardcoding!)
- Create a new Spring Project called mvcdemo-data. Choose web, jpa and Apache Derby dependencies
- Examine the pom.xml
- Copy the models and resources package to the new project
- Test the new application
- Add the @Entity annotation to the Student class
- Annotate the Id with @Id annotation
- Create a new class called StudentRepository in the resources package
- Make the repository an interface that extends the CrudRepository (this would enable you to use the methods in CrudRepository interface)
- Add the Student and String as types to the repository
- Autowire the StudentRepository into the StudentService
- Modify the getAllStudents of the StudentService to call the findAll() method of the StudentRepository.(create new ArrayList, loop through using .foreach and used method reference students::add)
- Modify the addStudent to use the save method of the StudentRepository
- Add this line to the application.propeties file jpa.hibernate.ddl-auto=update
- Test the service
- Modify the getStudentById to use StudentRepository. Change the return type to Student<Optional>. Take Student parameter.
- Modify the update to use the StudentRepository save method. Takes Student as parameter.
- Modify the delete method to use Repository deleteById method. Takes the id
Spring Boot with MySQL Database
- Create a Spring Project. Add Jpa, MySql and web dependencies
- Create a model called Students in the models package
- Annotate the class with @Entity annotation and @Table(name=”Students)
- Annotate the Id field with the @Id annotation and @GeneratedValue
- Create a StudentController file in the resources package. Annotate with @RestController
- Annotate the class with a root url of /springmysql
- Create the StudentRepository in the resources package. This repository is an interface that extends CrudRepository.
- Autowire StudentRepository into StudentController
- Write a method getStudents in the StudentConroller to return List<Student>. Use findAll()
- Annotate the getStudents method with @GetMapping of /students. Cast to List<Student>
- Write a method addStudents to insert a Student and return a string saying how many records were saved. Use CrudRepository save() method.
- Annotate the Student parameter with @RequestBody
- Annotate the addStudents() method with @PostMapping of /students
- Write the database configuration in the application.properties file (driver class, url, username, password)
- Create a database in MySQL workbench create database student;
- use student;
The mysql configuration is shown below. This code would be placed in the application.properties file
datasource.driver-class-name=com.mysql.cj.jdbc.Driver datasource.password=root datasource.username=root datasource.url=jdbc:mysql://localhost:3307/student jpa.hibernate.ddl-auto=update
Test the application. Remember the Id is GeneratedValue, you need to only pass the name and the department parameters
Updates StudentService Class Using CrudRepository
@Service public class StudentService { @Autowired private StudentRepository studentRepository; List<Student> students = new ArrayList<>(Arrays.asList( new Student("S2", "Kindson", "Software"), new Student("S2", "Saffron", "Accounting"), new Student("S3", "Emeka", "Management") )); public List<Student> getAllStudents() { //return students; List<Student> students = new ArrayList<>(); studentRepository.findAll() .forEach(students::add); return students; } public Optional<Student> getStudentById(String Id) { return studentRepository.findById(Id); } public void addStudent(Student student) { studentRepository.save(student); } public void updateStudent(String Id, Student student) { studentRepository.save(student); } public void deleteStudent(String Id) { studentRepository.deleteById(Id);; } }
Really Helpful thanks
Thanks. You could also watch the video explanation in my channel