{"id":1930,"date":"2019-06-14T12:00:00","date_gmt":"2019-06-14T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/h2-in-memory-database-step-by-step-tutorial\/"},"modified":"2026-07-05T03:23:49","modified_gmt":"2026-07-05T01:23:49","slug":"h2-in-memory-database-step-by-step-tutorial","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/h2-in-memory-database-step-by-step-tutorial\/","title":{"rendered":"H2 In-Memory Database Step by Step Tutorial"},"content":{"rendered":"<p><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/spring-mvc-step-by-step-tutorials-with-source-codes\/\">This is Similar to the Spring with MVC Procedure<\/a><\/p>\n<ol>\n<li>Create a new Starter project. Add Web, jpa and H2 dependencies<\/li>\n<li>Examine the pom.xml<\/li>\n<li>Create a Student class in the models package<\/li>\n<li>Annotate the class with @Entity annotation, Annotate the the Id with @Id annotation and @GeneratedValue<\/li>\n<li>Create a StudentRepostory interface in the repositories package. Make it extend CrudRepository&lt;T, Type&gt;<\/li>\n<li>Create a StudentController in the controllers package. Annotate with @RestController<\/li>\n<li>Annotate the controller with @RequestMapping of \/springh2<\/li>\n<li>Enable h2 console, datasource and url in the application.properties file<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">spring.h2.console.enabled=true\nspring.datasource.platform=h2\nspring.datasource.url=jdbc:h2:mem:<span style=\"color: #007700;\">&lt;dbname&gt;<\/span>\nspring.jpa.hibernate.ddl-auto=update\n<\/pre>\n<p>&nbsp;<\/p>\n<ol>\n<li>Run the application<\/li>\n<li>Visit http:\/\/localhost:8080\/h2-console<\/li>\n<li>Change the jdbc url to what you already specified in the application.properties<\/li>\n<li>Click on Connect. Notice that the STUDENT table is created. This is made possible by springboot autoconfiguration<\/li>\n<li>Create a separate data.sql file in the resources folder. Write a query to insert a couple of records in the database<\/li>\n<li>Relaunch the application.<\/li>\n<li>Refresh the console and check that this initial data is inserted<\/li>\n<li>Autowire the StudentRepository into the StudentController<\/li>\n<li>Write a method getAllStudents in the Controller that uses the findAll() method of the repository to return all student records<\/li>\n<li>Add the @GetMapping of \/students to the getAll Students method<\/li>\n<li>Write the getStudentByID method to return the student with a particular Id<\/li>\n<li>Test the application<\/li>\n<\/ol>\n<p><strong>Add Update and Delete Methods<\/strong><\/p>\n<ol start=\"21\">\n<li>Write the Add Method<\/li>\n<li>Write the Update Method<\/li>\n<li>Write the Delete Method<\/li>\n<li>Test the application<\/li>\n<li>Use Advanced Rest Client to add some records<\/li>\n<li>Check Spring Console to see that the records are there<\/li>\n<\/ol>\n<p><strong>Implementing a Service Layer (You should be able to do this by now)<\/strong><\/p>\n<ol start=\"27\">\n<li>To Implements a Service Layer, simply add a class called StudentService.<\/li>\n<li>Annotate this class with the @Service annotation<\/li>\n<li>Write the all the same methods you have in the controller file in in the service file. (without the @RequestMapping, @PathVariable or @RequestBody annotations<\/li>\n<li>Adjust the StudentController file so that it calls the functions in the StudentService file<\/li>\n<li>Modify the StudentService class to calls methods in the StudentRepository.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This is Similar to the Spring with MVC Procedure Create a new Starter project. Add Web, jpa and H2 dependencies Examine the pom.xml Create a &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":[414],"tags":[],"class_list":["post-1930","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1930","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=1930"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1930\/revisions"}],"predecessor-version":[{"id":2098,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1930\/revisions\/2098"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}