Spring Boot DATA JPA CRUD Web App

CRUD stands for Create Read Update Delete.

Video guide of the process



01. Add the required dependencies

Ensure that you have the below dependencies in your pom.xml.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

Depending on the database, you are intending to connect to, add the appropriate jdbc-connecter dependency in you pom.xml.

For H2 : use the below dependency
1
2
3
4
5
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

Note: If you generate the project using IDE / Spring Initializer, then you will have an option to choose your database connector from the dependencies dropdown.

02. Define Entity

Create a class Product.java with the below definition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package snmaddula.product.crud.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Data;

@Data
@Entity
@Table(name = "PRODUCT")
public class Product {

@Id
@GeneratedValue
private Long id;
private String title;
private String description;
private Double price;
}

In case, if your lombok configuration is not working, you can define setters & getters in the above class.

03. Define Repository.

Create an interface ProductRepo.java that extends JpaReository as below.

1
2
3
4
5
6
7
8
9
10
11
package snmaddula.product.crud.repo;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import snmaddula.product.crud.entity.Product;

@Repository
public interface ProductRepo extends JpaRepository<Product, Long> {

}

04. Add Service Implementation

Create a service ProductService.java that talks to the repository to perform crud operations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package snmaddula.product.crud.service;

import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;
import snmaddula.product.crud.entity.Product;
import snmaddula.product.crud.repo.ProductRepo;

@Service
@RequiredArgsConstructor
public class ProductService {

private final ProductRepo productRepo;

public List<Product> fetchAll() {
return productRepo.findAll();
}

public Optional<Product> fetchById(Long id) {
return productRepo.findById(id);
}

public Product create(Product product) {
return productRepo.save(product);
}

public Product update(Long id, Product product) {
if (productRepo.existsById(id)) {
return productRepo.save(product);
}
return null;
}

public void remove(Long id) {
productRepo.deleteById(id);
}

public void removeAll() {
productRepo.deleteAllInBatch();
}

}

04. Add Controller Implementation

Create a rest controller ProductController.java and add appropriate CRUD mappings for Product.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package snmaddula.product.crud.controller;

import java.util.List;
import java.util.Optional;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
import snmaddula.product.crud.entity.Product;
import snmaddula.product.crud.service.ProductService;

@RestController
@RequiredArgsConstructor
@RequestMapping("/products")
public class ProductController {

private final ProductService productService;

@GetMapping
public List<Product> getAll() {
return productService.fetchAll();
}

@GetMapping("{id}")
public Optional<Product> getById(@PathVariable Long id) {
return productService.fetchById(id);
}

@PostMapping
public Product create(@RequestBody Product product) {
return productService.create(product);
}

@PutMapping("{id}")
public Product update(@PathVariable Long id, @RequestBody Product product) {
return productService.update(id, product);
}

@DeleteMapping("{id}")
public void removeById(@PathVariable Long id) {
productService.remove(id);
}

@DeleteMapping
public void removeAll() {
productService.removeAll();
}

}

05. CONNECT to the DATABASE

If you are using H2 database (in embed mode), then you pretty much need zero configuration for this app to work.

Optionally to view the H2 Console, you can add the below configuration elements.

1
spring.h2.console.enabled: true

With this, you will be able to access the H2 Console at http://localhost:8080/h2-console

Your configuration should look something to similar as show below.

Click Connect, to open up the console.

06. Configuring DataSource

If you are using any other database, then you would need to provide the connection parameters using the below properties.

1
2
3
4
spring.datasource.url=<jdbc-url>
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=