Category: java
Spring Boot mvc
Published on 04 Apr 2026
Explanation
Spring Boot Application
Entry Point: This is
the main class that starts the Spring
Boot application using
@SpringBootApplication annotation.
Code:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(
DemoApplication.class, args);
}
}
Explanation
Model Layer: Represents the database
entity and
maps to a table using JPA annotations
like @Entity and @Id.
Code:
@Entity
public class User {
@Id
@GeneratedValue(strategy =
GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// getters and setters
}
Explanation
Repository Layer: Handles database
operations using Spring
Data JPA by extending JpaRepository.
Code:
@Repository
public interface UserRepository extends
JpaRepository<User, Long> {
}
Explanation
Service Layer: Contains business
logic and communicates
between Controller and Repository.
Code:
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> getAllUsers() {
return userRepository.findAll();
}
public User saveUser(User user) {
return userRepository.save(user);
}
}
Explanation
Controller Layer: Handles HTTP requests and
returns responses.
It interacts with the Service layer.
Code:
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> getUsers() {
return userService.getAllUsers();
}
@PostMapping
public User createUser(
@RequestBody User user) {
return userService.saveUser(user);
}
}
Explanation
Configuration File (application.properties):
Used to configure database
connection and server settings.
Code:
spring.datasource.url=db_url spring.datasource.username=root spring.datasource.password=root spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true server.port=8080