{"id":1929,"date":"2019-06-12T12:00:00","date_gmt":"2019-06-12T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/spring-with-jpa-repository-and-mysql-database\/"},"modified":"2026-07-05T03:23:46","modified_gmt":"2026-07-05T01:23:46","slug":"spring-with-jpa-repository-and-mysql-database","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/spring-with-jpa-repository-and-mysql-database\/","title":{"rendered":"Spring with JPA Repository and MySQL Database"},"content":{"rendered":"<p><strong>Spring Data JPA (Stop hardcoding!)<\/strong><\/p>\n<ol>\n<li>Create a new Spring Project called mvcdemo-data. Choose web, jpa and Apache Derby dependencies<\/li>\n<li>Examine the pom.xml<\/li>\n<li>Copy the models and resources package to the new project<\/li>\n<li>Test the new application<\/li>\n<li>Add the @Entity annotation to the Student class<\/li>\n<li>Annotate the Id with @Id annotation<\/li>\n<li>Create a new class called StudentRepository in the resources package<\/li>\n<li>Make the repository an interface that extends the CrudRepository (this would enable you to use the methods in CrudRepository interface)<\/li>\n<li>Add the Student and String as types to the repository<\/li>\n<li>Autowire the StudentRepository into the StudentService<\/li>\n<li>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)<\/li>\n<li>Modify the addStudent to use the save method of the StudentRepository<\/li>\n<li>Add this line to the application.propeties file jpa.hibernate.ddl-auto=update<\/li>\n<li>Test the service<\/li>\n<li>Modify the getStudentById to use StudentRepository. Change the return type to Student&lt;Optional&gt;. Take Student parameter.<\/li>\n<li>Modify the update to use the StudentRepository save method. Takes Student as parameter.<\/li>\n<li>Modify the delete method to use Repository deleteById method. Takes the id<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong>Spring Boot with MySQL Database<\/strong><\/p>\n<ol>\n<li>Create a Spring Project. Add Jpa, MySql and web dependencies<\/li>\n<li>Create a model called Students in the models package<\/li>\n<li>Annotate the class with @Entity annotation and @Table(name=\u201dStudents)<\/li>\n<li>Annotate the Id field with the @Id annotation and @GeneratedValue<\/li>\n<li>Create a StudentController file in the resources package. Annotate with @RestController<\/li>\n<li>Annotate the class with a root url of \/springmysql<\/li>\n<li>Create the StudentRepository in the resources package. This repository is an interface that extends CrudRepository.<\/li>\n<li>Autowire StudentRepository into StudentController<\/li>\n<li>Write a method getStudents in the StudentConroller to return List&lt;Student&gt;. Use findAll()<\/li>\n<li>Annotate the getStudents method with @GetMapping of \/students. Cast to List&lt;Student&gt;<\/li>\n<li>Write a method addStudents to insert a Student and return a string saying how many records were saved. Use CrudRepository save() method.<\/li>\n<li>Annotate the Student parameter with @RequestBody<\/li>\n<li>Annotate the addStudents() method with @PostMapping of \/students<\/li>\n<li>Write the database configuration in the application.properties file (driver class, url, username, password)<\/li>\n<li>Create a database in MySQL workbench create database student;<\/li>\n<li>use student;<\/li>\n<\/ol>\n<p>The mysql configuration is shown below. This code would be placed in the application.properties file<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">datasource.driver-class-name=com.mysql.cj.jdbc.Driver\ndatasource.password=root\ndatasource.username=root\ndatasource.url=jdbc:mysql:\/\/localhost:3307\/student\njpa.hibernate.ddl-auto=update\n<\/pre>\n<p>Test the application. Remember the Id is GeneratedValue, you need to only pass the name and the department parameters<\/p>\n<h5>Updates StudentService Class Using CrudRepository<\/h5>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Service<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">StudentService<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\n\t<span style=\"color: #555555; font-weight: bold;\">@Autowired<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> StudentRepository studentRepository<span style=\"color: #333333;\">;<\/span>\n\t\n\tList<span style=\"color: #333333;\">&lt;<\/span>Student<span style=\"color: #333333;\">&gt;<\/span> students <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> ArrayList<span style=\"color: #333333;\">&lt;&gt;(<\/span>Arrays<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">asList<\/span><span style=\"color: #333333;\">(<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Student<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"S2\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Software\"<\/span><span style=\"color: #333333;\">),<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Student<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"S2\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Saffron\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Accounting\"<\/span><span style=\"color: #333333;\">),<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">new<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Student<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"S3\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Emeka\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Management\"<\/span><span style=\"color: #333333;\">)<\/span>\n\t<span style=\"color: #333333;\">));<\/span>\n\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> List<span style=\"color: #333333;\">&lt;<\/span>Student<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getAllStudents<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #888888;\">\/\/return students;<\/span>\n\t\tList<span style=\"color: #333333;\">&lt;<\/span>Student<span style=\"color: #333333;\">&gt;<\/span> students <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> ArrayList<span style=\"color: #333333;\">&lt;&gt;();<\/span>\n\t\tstudentRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findAll<\/span><span style=\"color: #333333;\">()<\/span>\n\t\t<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">forEach<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #997700; font-weight: bold;\">students:<\/span><span style=\"color: #333333;\">:<\/span>add<span style=\"color: #333333;\">);<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> students<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> Optional<span style=\"color: #333333;\">&lt;<\/span>Student<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getStudentById<\/span><span style=\"color: #333333;\">(<\/span>String Id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span>  studentRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findById<\/span><span style=\"color: #333333;\">(<\/span>Id<span style=\"color: #333333;\">);<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">addStudent<\/span><span style=\"color: #333333;\">(<\/span>Student student<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\t\t\n\t\tstudentRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">save<\/span><span style=\"color: #333333;\">(<\/span>student<span style=\"color: #333333;\">);<\/span>\t\t\n\t<span style=\"color: #333333;\">}<\/span>\n\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">updateStudent<\/span><span style=\"color: #333333;\">(<\/span>String Id<span style=\"color: #333333;\">,<\/span> Student student<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\tstudentRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">save<\/span><span style=\"color: #333333;\">(<\/span>student<span style=\"color: #333333;\">);<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">deleteStudent<\/span><span style=\"color: #333333;\">(<\/span>String Id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\tstudentRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">deleteById<\/span><span style=\"color: #333333;\">(<\/span>Id<span style=\"color: #333333;\">);;<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\t\t\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[311],"tags":[],"class_list":["post-1929","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1929"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1929\/revisions"}],"predecessor-version":[{"id":2097,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1929\/revisions\/2097"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}