The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework with a focus on Spring Boot.
- Understanding Spring Boot
- Beginner Java developers
- Java developers interested in learning how to Build and Deploy RESTful Web Services
- Java Developers who want to develop web applications using the Spring framework
- Java Developers who want to develop web applications with microservices
- Java Developers who wish to develop Spring Boot Microservices with Spring Cloud
- Basic Java knowledge
Hello, my friends. In this video, we will continue working on our spring boot project. Let's get started. We built our project and downloaded our adminLTE template in the last lesson. We will now create our database. In projects, I like to keep my database in docker containers. However, I will use Postgre on my computer without docker for this project. Docker will be used to store several databases in the microservice project.
As you are aware, we do not need to create any databases in PostgreSQL when using JPA. We will only use database properties to build entities and alter application properties. So, let's get started. First, we require entities. I'd like to begin with the user. Create a new class and call it User. As you know, to convert any Postgre to an entity, we must use Entity annotations. Also, I don't want to get any warnings for deprecated elements. So, I want to use the SuppressWarnings annotation with deprecation. Now, let's change the table name to users. Done, now we can define our columns one by one. First Id, we need an Id annotation. Also for Id, we need a GeneratedValue annotation with AUTO.
Name, name must be filled, so I will use the notable property with false. This means we have to fill the name value for each row of the table. Email, in email also we must use unique property. Password, let's adjust the minimum size of password. Now, we must specify the role but first we must define the role entity, after that we can add the role list to this entity. Add a new class and name it Role. Id with Id and GeneratedValue annotation. Name, this will be enough.
Let's define a ManyToMany relationship between users and roles. We need ManyToMany annotations. Define a user list. Now, generate getters and setters. Done, Role table is ready. Go back to the users entity. First define a role list. Add ManyToMany annotation. With a JointTable annotation we can merge tables JoinColumn USER_ID referenceColumn ID. inverseJoinColumns ROLE_ID.
Done, now we can generate getters and setters. Done, now we have two entities, let's continue with products. Add a new class and name it Product. Entity, table name will be products. Let's define the columns, Id, name, price. I also want to store stock amount. Now I want to store sold products in a different table and I'll set up a relationship between the two. Create a new class and call it SoldProduct. Of course, we need entity, Id, piece. We'll keep them in the basket until the sale. product_Id, we will define bills for each sale, so I also want to define the bill_Id.
That's enough for this table, generate getters and setters. Actually you can use Lombok to eliminate the necessity for these getters and setters if you like. But as I previously stated, I want to use Lombok in the microservice project. Return to the product, define SoldProduct list. This will be a OneToMany relationship, generate getters and setters. Now we need a constructor with parameters. Now, let's set parameters to the fields. Done, let's continue with Bill. Entity, Table(name-bills).
Id with identity, customerName, total_price, bill_date. We need to confirm these bills, therefore I need to confirm column in the table, admin_confirmed, employee_confirmed. Let's generate a relationship between sold products. Done, now we need a constructor with parameters like product. In the constructor, we'll set new values to the fields. Done, generate getters and setters.
Now, that our entities are ready, we can update the application properties. Navigate to the Resources folder and open the application properties file. First, I'd like to define the server port number, Spring datasource class -name. Let's add URL. My databases URL is this, you have to replace this URL with your databases URL. Username, password, jpa.hibernate auto-update, show-sql_true. Done, now we can create and access our database with this URL. We have now finished creating our database, let's take a break here. In the next video we will continue working on our project by integrating the template. So, I'll see you in the next video.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.