CRUD stands for Create, Read, Update, and Delete, which are the basic operations that can be performed on data in a database or data storage system. This concept is fundamental to database management and is often used in the context of web development, software engineering, and data management.
In this CRUD application, we are managing an employee list with fields such as firstname, lastname, and email.
-
Backend:
- Spring - A comprehensive framework for enterprise Java development.
-
Frontend:
- React - A popular JavaScript library for building user interfaces.
- Vite - A fast build tool for modern JavaScript projects.
-
Database:
- MySQL - A relational database management system.
-
Container:
- Docker - An open platform for developing, shipping, and running applications.
A Spring-based CRUD (Create, Read, Update, Delete) operation typically involves creating a web application that performs these basic database operations. Here, I'll provide a simple example using Spring Boot and Spring Data JPA for data persistence.
-
Create a Spring Boot Project: You can use Spring Initializer (https://start.spring.io/) or your favorite IDE to create a new Spring Boot project. Include the dependencies for "Spring Web" and "Spring Data JPA."
-
Define Entity Class: Define a class with
@Entity
to signify a JPA entity, encapsulating data to be stored. This class serves as a blueprint for objects persisted in a database in Spring applications.
@Entity
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email_id")
private String emailId;
}
- Create Repository Interface:
Create a repository interface extending
JpaRepository
for entity CRUD operations. JpaRepository provides pre-built methods for common database operations, simplifying data access code in Spring applications.
import org.springframework.data.jpa.repository.JpaRepository;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
- Implement Service Layer: Develop a service class that communicates with the repository, encapsulating methods for Create, Read, Update, and Delete (CRUD) operations, providing a clean abstraction for data manipulation in a Spring application.
@Service
public class ItemService {
private final ItemRepository itemRepository;
@Autowired
public ItemService(ItemRepository itemRepository) {
this.itemRepository = itemRepository;
}
public List<Item> getAllItems() {
return itemRepository.findAll();
}
public Optional<Item> getItemById(Long id) {
return itemRepository.findById(id);
}
public Item saveItem(Item item) {
return itemRepository.save(item);
}
public void deleteItem(Long id) {
itemRepository.deleteById(id);
}
}
- Create Controller: Create a controller class for handling HTTP requests and facilitating communication with the service layer, ensuring seamless interaction between the web interface and backend functionality.
@RestController
@RequestMapping("/items")
public class ItemController {
private final ItemService itemService;
@Autowired
public ItemController(ItemService itemService) {
this.itemService = itemService;
}
@GetMapping
public List<Item> getAllItems() {
return itemService.getAllItems();
}
@GetMapping("/{id}")
public Optional<Item> getItemById(@PathVariable Long id) {
return itemService.getItemById(id);
}
@PostMapping
public Item addItem(@RequestBody Item item) {
return itemService.saveItem(item);
}
@PutMapping("/{id}")
public Item updateItem(@PathVariable Long id, @RequestBody Item item) {
item.setId(id);
return itemService.saveItem(item);
}
@DeleteMapping("/{id}")
public void deleteItem(@PathVariable Long id) {
itemService.deleteItem(id);
}
}
This example demonstrates a basic Spring Boot application with CRUD operations using Spring Data JPA. Keep in mind that you may need to configure your database connection properties in the application.properties
or application.yml
file based on your database choice (e.g., MySQL, PostgreSQL, H2).
-
Clone the Repository:
git clone https://github.com/bishakhne0gi/Spring-Crud.git
-
Setup:
- Follow these steps to install Docker
- Open a Terminal inside the root of this repository you just cloned
- Run
docker compose up
Wait for a few minutes for the Docker Container to build and the application will start automatically.
-
Access the Application:
Open your browser and go to http://localhost:5173 to access the CRUD application frontend.
This project is licensed under the MIT License.
You have successfully set up the CRUD application locally. Feel free to explore, modify, and enhance the code to suit your needs. If you encounter any issues or have suggestions, please open an issue or create a pull request.