https://github.com/geloodev/learning-spring-boot
My first learning Spring Boot project, through the Amigoscode's course.
https://github.com/geloodev/learning-spring-boot
java maven postgres postgresql spring spring-boot yml
Last synced: about 1 month ago
JSON representation
My first learning Spring Boot project, through the Amigoscode's course.
- Host: GitHub
- URL: https://github.com/geloodev/learning-spring-boot
- Owner: geloodev
- Created: 2023-07-20T17:24:57.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-08-15T17:45:33.000Z (over 1 year ago)
- Last Synced: 2025-01-26T10:08:35.397Z (3 months ago)
- Topics: java, maven, postgres, postgresql, spring, spring-boot, yml
- Language: Java
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Learning Spring Boot 🍃
This project was created to learning the basics of the Spring Boot framework while following Amigoscode’s Spring Boot for Beginners course.
### Tools
[](https://skillicons.dev)## Java Files
The Java files follow a simple MVC-like architecture:- **Model:** Customer.java
- **Controller:** CustomerController.java
- **Repository:** CustomerRepository.java### Customer.java
Annotations: ```@Entity``` defines the Entity Table, ```@Id``` designates the Primary Key, ```@SequenceGenerator``` creates the PK in a sequential manner, and ```@GeneratedValue``` specifies how the PK will be generated.```java
package com.geloodev;import java.util.Objects;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.SequenceGenerator;@Entity
public class Customer {@Id
@SequenceGenerator(
name = "customer_id_sequence",
sequenceName = "customer_id_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "customer_id_sequence"
)
private Integer id;private String name;
private String email;
private Integer age;public Customer(Integer id, String name, String email, Integer age) {
this.id = id;
this.name = name;
this.email = email;
this.age = age;
}public Customer() {}
public Integer getId() {
return id;
}public void setId(Integer id) {
this.id = id;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public String getEmail() {
return email;
}public void setEmail(String email) {
this.email = email;
}public Integer getAge() {
return age;
}public void setAge(Integer age) {
this.age = age;
}@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;Customer customer = (Customer) o;
return Objects.equals(id, customer.id) &&
Objects.equals(name, customer.name) &&
Objects.equals(email, customer.email) &&
Objects.equals(age, customer.age);
}@Override
public int hashCode() {
return Objects.hash(id, name, email, age);
}@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';}
}
```### CustomerController.java
Annotations: ```@RestController``` designates the class as a REST Controller, ```@RequestMapping``` is specifies the path where functions will make HTTP Requests, ```@GetMapping``` sets the function to do GET Requests, and the same are to ```@PostMapping``` for POST Requests, ```@DeleteMapping``` for DELETE Requests and ```@PutMapping``` for PUT Requests. Additionally, ```@RequestBody``` maps the HTTP Request Body to a object, and ```@PathVariable``` handle template variables in the request URI and set then as method parameters.```java
package com.geloodev;import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("api/v1/customers")
public class CustomerController {
private final CustomerRepository customerRepository;public CustomerController(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}@GetMapping
public List getCustomers() {
return customerRepository.findAll();
}record CustomerRequest(
String name,
String email,
Integer age
) {}@PostMapping
public void addCustomer(@RequestBody CustomerRequest request) {
Customer customer = new Customer();
customer.setName(request.name());
customer.setEmail(request.email());
customer.setAge(request.age());
customerRepository.save(customer);
}@DeleteMapping("/{customerId}")
public void deleteCustomer(@PathVariable("customerId") Integer id) {
customerRepository.deleteById(id);
}@PutMapping("/{id}")
public void updateCustomer(
@PathVariable("id") Integer id,
@RequestBody CustomerRequest request
) {
Customer customer = customerRepository.findById(id)
.orElseThrow();
customer.setName(request.name());
customer.setEmail(request.email());
customer.setAge(request.age());customerRepository.save(customer);
}
}
```### Customer.java
```JpaRepository``` extends the ```CrudRepository``` and receives the domain class and the ID type that it should handle.]```java
package com.geloodev;import org.springframework.data.jpa.repository.JpaRepository;
public interface CustomerRepository extends JpaRepository {
}
```## Other Files
### pom.xml
The Maven configuration for dependencies, plugins, etc.```xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.1.1
com.geloodev
learning-spring-boot
0.0.1-SNAPSHOT
learning-spring-boot
Demo project for Spring Boot
17
org.postgresql
postgresql
runtime
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
```
### docker-compose.yml
The Docker configuration to manage the PostgreSQL container.```yaml
services:
db:
container_name: first-spring-boot
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
PGDATA: /data/postgres
volumes:
- db:/data/postgres
ports:
- "5433:5432"
networks:
- db
restart: unless-stoppednetworks:
db:
driver: bridgevolumes:
db:
```