Curso Java COMPLETO
[Link]
Dr. Nelio Alves
Projeto web services com Spring Boot e JPA / Hibernate
Objetivos
Criar projeto Spring Boot Java
Implementar modelo de domínio
Estruturar camadas lógicas: resource, service, repository
Configurar banco de dados de teste (H2)
Povoar o banco de dados
CRUD - Create, Retrieve, Update, Delete
Tratamento de exceções
Github:
[Link]
Domain Model
Domain Instance
Logical Layers
Project created
Checklist:
File -> New -> Spring Starter Project
o Maven
o Java 11
o Packing JAR
o Dependencies: Spring Web Starter
User entity and resource
Basic entity checklist:
Basic attributes
Associations (instantiate collections)
Constructors
Getters & Setters (collections: only get)
hashCode & equals
Serializable
H2 database, test profile, JPA
Checklist:
JPA & H2 dependencies
[Link]
[Link]
Entity: JPA mapping
Dependencies:
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
[Link]:
[Link]=test
[Link]-in-view=true
[Link]:
[Link]=jdbc:h2:mem:testdb
[Link]=sa
[Link]=
[Link]=true
[Link]=/h2-console
[Link]-sql=true
[Link].format_sql=true
JPA repository, dependency injection, database seeding
Checklist:
UserRepository extends JPARepository<User, Long>
Configuration class for "test" profile
@Autowired UserRepository
Instantiate objects in memory
Persist objects
Objects:
User u1 = new User(null, "Maria Brown", "maria@[Link]", "988888888", "123456");
User u2 = new User(null, "Alex Green", "alex@[Link]", "977777777", "123456");
Service layer, component registration
Order, Instant, ISO 8601
Basic new entity checklist:
Entity
o "To many" association, lazy loading, JsonIgnore
Repository
Seed
Service
Resource
Objects:
Order o1 = new Order(null, [Link]("2019-06-20T[Link]Z"), u1);
Order o2 = new Order(null, [Link]("2019-07-21T[Link]Z"), u2);
Order o3 = new Order(null, [Link]("2019-07-22T[Link]Z"), u1);
OrderStatus enum
Category
Objects:
Category cat1 = new Category(null, "Electronics");
Category cat2 = new Category(null, "Books");
Category cat3 = new Category(null, "Computers");
Product
Objects:
Product p1 = new Product(null, "The Lord of the Rings", "Lorem ipsum dolor sit amet, consectetur.", 90.5, "");
Product p2 = new Product(null, "Smart TV", "Nulla eu imperdiet purus. Maecenas ante.", 2190.0, "");
Product p3 = new Product(null, "Macbook Pro", "Nam eleifend maximus tortor, at mollis.", 1250.0, "");
Product p4 = new Product(null, "PC Gamer", "Donec aliquet odio ac rhoncus cursus.", 1200.0, "");
Product p5 = new Product(null, "Rails for Dummies", "Cras fringilla convallis sem vel faucibus.", 100.99, "");
Many-to-many association with JoinTable
OrderItem, many-to-many association with extra attributes
Checklist:
OrderItemPK
OrderItem
Order one-to-many association
Seed
Objects:
OrderItem oi1 = new OrderItem(o1, p1, 2, [Link]());
OrderItem oi2 = new OrderItem(o1, p3, 1, [Link]());
OrderItem oi3 = new OrderItem(o2, p3, 2, [Link]());
OrderItem oi4 = new OrderItem(o3, p5, 2, [Link]());
Product-OrderItem one-to-many association
Payment, one-to-one association
Subtotal & Total methods
User insert
Checklist:
UserService
UserResource
Test:
{
"name": "Bob Brown",
"email": "bob@[Link]",
"phone": "977557755",
"password": "123456"
}
User delete
Checklist:
UserService
UserResource
User update
Checklist:
UserService
UserResource
Test:
{
"name": "Bob Brown",
"email": "bob@[Link]",
"phone": "977557755"
}
Exception handling - findById
Checklist:
NEW CLASS: [Link]
NEW CLASS: [Link]
NEW CLASS: [Link]
UserService
Exception handling - delete
Checklist:
NEW CLASS: [Link]
ResourceExceptionHandler
UserService
o EmptyResultDataAccessException
o DataIntegrityViolationException
Exception handling - update
Checklist:
UserService
o EntityNotFoundException
Create Heroku app & provision PostgreSQL
Checklist:
Heroku Sign Up
Create app
Provision PostgreSQL
o App dashboard -> Resources
o Search "postgres" -> select "Heroku Postgres"
Install local PostgreSQL
Checklist:
Download and install: [Link]
o Super user: postgres
o Password: 1234567
o Port: 5432
Start/stop service: Task manager -> Services
Check instalation
o Start pgAdmin
o Databases -> Create -> Database
Encoding: UTF8
Dev profile
Checklist:
PgAdmin: create local database: create database springboot_course
Add PostgreSQL Maven dependency
<dependency>
<groupId>[Link]</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
Create file: [Link]
[Link]=jdbc:postgresql://localhost:5432/springboot_course
[Link]=postgres
[Link]=1234567
[Link].non_contextual_creation=true
[Link]-auto=update
[Link]-sql=true
[Link].format_sql=true
[Link]=MYJWTSECRET
[Link]=3600000
Update [Link]: [Link]=dev
Run application
Get SQL script from local PostgreSQL
PgAdmin: get SQL script:
o Select database
o Tools -> Backup
Format: Plain
Encoding: UTF8
Dump options:
Only schema: YES
Blobs: NO
Do not save: (ALL)
Verbose messages: NO
Delete instructions before CREATE statements
Run SQL Script
Checklist:
App dashboard -> Settings - > Config Vars
EXAMPLE:
postgres://wavglvupbdad:358f443aafe452eca4c58fbc15d02e50b08130c7aaea3aff6c4f59c
13f9abb@[Link]/d7u9ub86cdsu
user: wavglvupbdad
password: 358f443aafe452eca4c58fbc15d02e50b08130c7aaea3aff6c4f59c13f9abb
server: [Link]
port: 5432
database: d7u9ub86cdsu
PgAdmin: Servers -> Create -> Server
o Advanced -> DB rescriction: (database)
Database -> Query Tool
o Load and run SQL Script
Deploy app to Heroku
Heroku app dashboard -> Deploy
heroku git:remote -a myapp
git remote -v
Setup Heroku app Config Vars
o DATABASE_URL
o JWT_EXPIRATION
o JWT_SECRET
Create: [Link]
[Link]=${DATABASE_URL}
[Link]-auto=none
[Link]-sql=false
[Link].format_sql=false
[Link]=${JWT_SECRET}
[Link]=${JWT_EXPIRATION}
Update [Link]: [Link]=prod
Create files: [Link]
[Link]=11
Send to Heroku:
git add .
git commit -m "Deploy app to Heroku"
git push heroku master