Right, now we have managed to come up with a database of sorts. What we need to do is to initiate the project, and implement the database, correct? So, we'll head over to start.spring.io to initialize our project.
Let me say something in a bit.
First off, I really don't like gradle so I'll use maven instead. Another thing, please do pay attention to the java version down there. Use Java 8, it works ...
After clicking generate, the project should be downloaded as a zip file. Open the project using your favorite editor ( mine is intellij idea )
Last time I had an argument of sorts with a workmate/acquaintance of mine over project folder structures. To be fairly honest, at the end of the day, it depends on your preference. If you feel like my folder structure is weird ... then good for you!
Anyway, I normally use the following structure:
I love this structure because it creates a separation of concerns. Models have their own folder, DTOs have their own folder and controllers have their own folders ... and so on. Like I said earlier, it's just a matter of preference.
Anyway, stupid things aside, now we are going to be implementing the models. First, we'll start with the Employee model. Java is object oriented, which means that everything is an object ... eh ... I'm sure you understand.
To be honest, I'm very lazy. As most lazy people do, I'm going to use lombok instead of defining getters and setters manually.
Anyway, before we get into that, we need to open the application.properties under src > main > resources and put the database configurations there.
When done with that, let's get into defining the model. Under the models package, create a new file called Employee.java. The initial code, before defining the relationships, looks like the following:
package com.dull.piqued.models;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import javax.persistence.*;
import java.util.Date;
@Getter
@Setter
@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false, updatable = false)
private long ecNumber;
@Column(nullable = false)
private String firstname;
@Column(nullable = false)
private String lastname;
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false, unique = true)
private String phone;
@Column(nullable = false)
private String physicalAddress;
@Column
private double salary;
@Column
private Date hireDate;
@CreationTimestamp
private Date createdAt;
@UpdateTimestamp
private Date updatedAt;
}
Now, I'll explain what the code does in the next tutorial... chao 👋
Top comments (0)