Spring with JPA Repository and MySQL Database

Spring Data JPA (Stop hardcoding!)

  1. Create a new Spring Project called mvcdemo-data. Choose web, jpa and Apache Derby dependencies
  2. Examine the pom.xml
  3. Copy the models and resources package to the new project
  4. Test the new application
  5. Add the @Entity annotation to the Student class
  6. Annotate the Id with @Id annotation
  7. Create a new class called StudentRepository in the resources package
  8. Make the repository an interface that extends the CrudRepository (this would enable you to use the methods in CrudRepository interface)
  9. Add the Student and String as types to the repository
  10. Autowire the StudentRepository into the StudentService
  11. 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)
  12. Modify the addStudent to use the save method of the StudentRepository
  13. Add this line to the application.propeties file jpa.hibernate.ddl-auto=update
  14. Test the service
  15. Modify the getStudentById to use StudentRepository. Change the return type to Student<Optional>. Take Student parameter.
  16. Modify the update to use the StudentRepository save method. Takes Student as parameter.
  17. Modify the delete method to use Repository deleteById method. Takes the id

 

Spring Boot with MySQL Database

  1. Create a Spring Project. Add Jpa, MySql and web dependencies
  2. Create a model called Students in the models package
  3. Annotate the class with @Entity annotation and @Table(name=”Students)
  4. Annotate the Id field with the @Id annotation and @GeneratedValue
  5. Create a StudentController file in the resources package. Annotate with @RestController
  6. Annotate the class with a root url of /springmysql
  7. Create the StudentRepository in the resources package. This repository is an interface that extends CrudRepository.
  8. Autowire StudentRepository into StudentController
  9. Write a method getStudents in the StudentConroller to return List<Student>. Use findAll()
  10. Annotate the getStudents method with @GetMapping of /students. Cast to List<Student>
  11. Write a method addStudents to insert a Student and return a string saying how many records were saved. Use CrudRepository save() method.
  12. Annotate the Student parameter with @RequestBody
  13. Annotate the addStudents() method with @PostMapping of /students
  14. Write the database configuration in the application.properties file (driver class, url, username, password)
  15. Create a database in MySQL workbench create database student;
  16. 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);;
	}		
}

kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

2 thoughts on “Spring with JPA Repository and MySQL Database

Leave a Reply

Your email address will not be published. Required fields are marked *