I'm use for this course an IntelliJ IDEA.
For me, that use VSCode, its perfect install the VSCode Keymap:
- File -> Settings -> Keymaps
- Click on
Get more keymaps in setting
- Search
VSCode
and install - Choose VSCode on
Keymap
panel - Save
It's a Project Dependencies Manager.
To install some dependencies, you need to search the code part that makes maven add library.
A code part like this needs to be past on pom.xml
:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.5.5.Final</version>
</dependency>
Saving the file (Ctrl + S
), maven will att your project.
To configure JPA, you need:
- Create a
META-INF
folder onsrc/main/resources
- Inside META-INF, create a
persistence.xml
file
OBS: The file/folder names needs to be EXACTLY equals!
On persistence.xml
, put:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="projectName" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost/yourDatabase?useSSL=false&serverTimezone=UTC" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect" />
</properties>
</persistence-unit>
</persistence>
Maybe you need create a container: docker run --name=java_mysql -d mysql/mysql-server
For Map Entities, you need to put on Entities Class a decorator called @Entity
.
Should be imported by javax.persistence
:
@Entity
public class User {
private Integer id;
private String username;
//[...]
}
For Primary Keys, we use this decorators over declaration:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
//[...]
Other columns naturally are created by Hibernate.
To make create
operations on Database, we need use a EntityManagerFactory
and EntityManager
from javax.persistence
:
public static void main(String[] args) {
EntityManagerFactory userEntityFactory = Persistence.createEntityManagerFactory("Course");
EntityManager userEntity = userEntityFactory.createEntityManager();
User user = new User(null, "Bruno Santana", "[email protected]", "123456789");
//Quando o JPA faz uma operação no banco diferente de consulta, ele precisa de uma transação
userEntity.getTransaction().begin();
userEntity.persist(user);
userEntity.getTransaction().commit();
System.out.println("Data writed on Database");
userEntity.close();
userEntityFactory.close();
}
The projectName
needs to be the exact name that contains in the name
property at persistence-unit
on persistence.xml
.
Finnaly we can close both the userEntity
and userEntityFactory
.
To make read
operations, we can use find
method:
User userFromDatabase = userEntity.find(User.class, 8);
To make remove
operations you need to parse non-monitored Entities (Object that you inserted and not close entities):
userEntity.getTransaction().begin();
userEntity.remove(userFromDatabase);
userEntity.getTransaction().commit();
Framework that provides all configs and basic packages that should able to create an basic API. Management transactions and create a repository.
- Go to Spring Initializr Site
- In Project, choose Maven
- In Spring Boot, don't change anything
- Fill Project metadata with the correct data
- Choose a LTS java version
- Click in GENERATE
- Unzip files and open project
To running application, just type CTRL + Shift + F10
to Build and Run.
To change default port and other configs, you can change the application.properties
in src/main/resources
:
server.port = ${port:3333}
In main package we need to create a subpackage called resourses
.
Create inside the new package a Category Class
called CategoryResource
:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value="/categories")
public class CategoryResource {
@RequestMapping(method = RequestMethod.GET)
public String list(){
return "List method working";
}
}
- Rest class controllers needs a decorator before declaration called
@RestController
. - We create another decorator
@RequestMapping(value="/categories")
parsing the endpoint - For this example, we create a GET Route, so we add a
@RequestMapping(method = RequestMethod.GET)
decorator before method.
Category
package com.brunosana.course.domain;
import java.io.Serializable;
import java.util.Objects;
public class Category implements Serializable {
public static final Long serialVersionUID = 1L;
private Integer id;
private String name;
public Category(){}
public Category(Integer id, String name){
this.id = id;
this.name = name;
}
public Integer getId(){
return id;
}
public String getName(){
return name;
}
public void setId(Integer id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Category category = (Category) o;
return Objects.equals(id, category.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
New get Method on RestController:
@RequestMapping(method = RequestMethod.GET)
public List<Category> list(){
Category category = new Category(1, "Info");
Category categoryTwo = new Category(2, "Office");
List<Category> list= new ArrayList<>();
list.add(category);
list.add(categoryTwo);
return list;
}
- Needs a constructor
- Needs getters and setters
- Needs
hashCode
andequals
methods to compare objects - Needs to implements Serializable (Talks java that the current class can be converted from a bit sequence)
H2 it's a databases used for tests, running local and with low memory
To create a H2, we will use this dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
In src/main/static/application.properties
we add:
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
So now we can run the application and go to the /h2
path. If we can connect, it's working (With decorators on Entity Domain Classes)!