Spring Boot 2 CRUD + MVC + JPA + H2 Part 1

Although we go with a slow pace and very superficially, I’m showing you the use of Spring Boot with different components and as used in the different layers of a Web application to create a project.

We saw Spring Boot + MVC in the view layer in previous tutorials and in this post you will learn how to integrate easily with the persistence layer using JPA and the embedded engine H2 database

What is JPA?

The acronym JPA in English is Java Persistence Api. And it allows us to have a bi-directional relationship between a database and an object-oriented system. This relationship is known as ORM (Object relational Mapping) and in simple words is to have an Entity (POJO) that contains the same attributes as a table in the database.

This configuration in Spring Boot is made trough annotations and the purpose is to avoid writing code difficult to maintain.

Demonstration:

Remember that a POJO must have only the builders, getters and setters. But in this case Spring Boot I recommend over-write the methods toString (), Equals () and HashCode ().

    1. (Create the POJO and define it as an entity):Jpa Entity
    2. Create the attribute that will contain the primary key of the table:
      1. @Id-You can only have one attribute with this notation that tells JPA that this is your primary key.
      2. @GeneratedValue-serves to indicate that this value is autoincrement as is done in MySQL
      3. @Column-Has the same behavior as @Table, if the name of the attribute is equal to the name of the column, this notation is not required

        Hibernate annotations
        hibernate annotations

In the next section you can see two videos. One with example of how to create user table and its visualization with H2 and its console embedded and in the second integration and a simple CRUD with Spring MVC + Thymeleaf

code required to enter in the H2 console: 
JDBC URL: jdbc: H2: mem: testdb

 

GitHub: GitHub repository

Remember to leave your comments and doubts in the section below.

Thank you,

@Cruizg93

Spring Boot Introduction:

  1. Spring Boot – 01 – What is it? And how do you eat? 
  2. Spring Boot + MVC Part 1 | Using JSP?
  3. Spring Boot + MVC Part 2 | Using Thymeleaf
  4. Spring Boot CRUD + MVC + JPA + H2 Part 1 (current)
  5. Spring Boot CRUD + MVC + JPA relations 1:1 and *:*

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *