Hello! Today we will conduct a backend development course using Spring Boot. In this course, we will take a detailed look at the basics of Spring Boot, database integration, REST API development, and finally how to understand the project structure through diagrams.
1. What is Spring Boot?
Spring Boot is a framework that helps you use the Java-based Spring framework more easily. It supports developers in building applications quickly and allows you to create executable Spring applications without complex configurations.
The main features of Spring Boot are as follows:
- Auto configuration: Automatically configures Spring settings.
- Standalone applications: Can be easily packaged into a JAR file for deployment and execution.
- Production-ready: Provides external configuration features and monitoring metrics.
- Starter dependencies: Helps to easily add necessary dependencies.
2. Setting Up the Development Environment
To use Spring Boot, you need to install the Java Development Kit (JDK) and choose an IDE (Integrated Development Environment). In this course, we will use IntelliJ IDEA.
2.1 Installing the JDK
The JDK can be downloaded from the official Oracle website or OpenJDK. After installation, you need to set the PATH environment variable to use the JDK.
2.2 Installing IntelliJ IDEA
IntelliJ IDEA is an IDE provided by JetBrains. The Community Edition is offered for free and is suitable for Spring Boot development. Download and install it from the official website.
3. Creating a Spring Boot Project
Now let’s create a new Spring Boot project. Click on “New Project” in IntelliJ IDEA and select “Spring Initializr”.
3.1 Project Settings
Enter the following information:
- Group: com.example
- Artifact: demo
- Name: demo
- Description: Demo project for Spring Boot
- Package name: com.example.demo
- Packaging: Jar
- Java: 11
Then, in “Dependencies”, add “Spring Web”, “Spring Data JPA”, and “H2 Database”.
4. Integrating with the Database
Spring Boot supports easy integration with various databases. In this course, we will create a simple CRUD application using the H2 database.
4.1 H2 Database Configuration
The H2 database is an in-memory database. Configure it by adding the following to the project’s src/main/resources/application.properties
file:
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.h2.console.enabled=true
4.2 Creating the Entity Class
Now let’s create the Entity class that will be mapped to the database. Create a “model” package, and write the User
class as follows:
package com.example.demo.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String email; // Getters and Setters }
4.3 Creating the Repository Interface
To interact with the database using Spring Data JPA, create a Repository interface. Create a ‘repository’ package and write the interface as follows:
package com.example.demo.repository; import com.example.demo.model.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository{ }
4.4 Creating the Service Class
Create a Service class to handle business logic. Create a ‘service’ package and write the UserService class as follows:
package com.example.demo.service; import com.example.demo.model.User; import com.example.demo.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Autowired private UserRepository userRepository; public ListfindAll() { return userRepository.findAll(); } public User save(User user) { return userRepository.save(user); } }
4.5 Creating the Controller Class
Create a Controller class to handle HTTP requests. Create a ‘controller’ package and write the UserController class as follows:
package com.example.demo.controller; import com.example.demo.model.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping public ListgetAllUsers() { return userService.findAll(); } @PostMapping public User createUser(@RequestBody User user) { return userService.save(user); } }
5. Understanding RESTful APIs
Now let’s understand RESTful APIs using Spring Boot. REST stands for Representational State Transfer and is a web-based architectural style. RESTful APIs operate by creating, reading, modifying, and deleting resources through HTTP requests.
5.1 HTTP Methods
RESTful APIs use the following HTTP methods:
- GET: Retrieve resources
- POST: Create resources
- PUT: Modify resources
- DELETE: Delete resources
5.2 JSON Data
JSON (JavaScript Object Notation) format is commonly used to transmit data in RESTful APIs. JSON is a lightweight and human-readable data format widely used in web applications.
6. Understanding Project Structure Through Diagrams
Based on what we have discussed so far, let’s draw the project structure. Here is the overall project structure:
demo ├── src │ ├── main │ │ ├── java │ │ │ └── com.example.demo │ │ │ ├── controller │ │ │ │ └── UserController.java │ │ │ ├── model │ │ │ │ └── User.java │ │ │ ├── repository │ │ │ │ └── UserRepository.java │ │ │ └── service │ │ │ └── UserService.java │ │ └── resources │ │ └── application.properties └── pom.xml
7. Conclusion
In this course, we learned the basics of backend development using Spring Boot and how to implement a simple RESTful API integrated with a database. Spring Boot is a powerful framework, so I encourage you to continue learning and take advantage of its various features.
Don’t stop here; take on various projects! We wish you success on your development journey!