Spring MVC Tutorial

Core Spring MVC

Spring MVC - Annotation

Spring MVC - Form Handling

Spring MVC with JSTL

Spring MVC with REST API

Spring MVC with Database

Spring MVC - Sample Project For Finding Doctors Online with MySQL

Building a sample project for finding doctors online involves several steps. In this outline, I'll provide a broad overview of what's needed, and then delve into some specifics to get you started.

1. Project Setup:

  • Use Spring Boot for easy setup. Initialize your project using the Spring Initializr.

  • Dependencies to include: Web, Thymeleaf (for templating), JPA (for database operations), and MySQL.

2. Database Setup:

  • Create a database called doctors_db in MySQL.

  • Create a table called doctors with the following fields:

    • id (primary key)
    • name
    • specialization
    • location
    • rating
    • availability

3. Spring Configuration:

In application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/doctors_db
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.jpa.hibernate.ddl-auto=update

4. Create Entity, Repository, Service, and Controller layers:

Entity:

@Entity
public class Doctor {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String specialization;
    private String location;
    private Double rating;
    private Boolean availability;
    
    // getters, setters, constructors...
}

Repository:

public interface DoctorRepository extends JpaRepository<Doctor, Long> {
    List<Doctor> findByLocationAndSpecialization(String location, String specialization);
}

Service:

@Service
public class DoctorService {

    @Autowired
    private DoctorRepository doctorRepository;

    public List<Doctor> findDoctors(String location, String specialization) {
        return doctorRepository.findByLocationAndSpecialization(location, specialization);
    }
}

Controller:

@Controller
@RequestMapping("/doctors")
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    @GetMapping("/search")
    public String searchDoctors(@RequestParam String location, @RequestParam String specialization, Model model) {
        List<Doctor> doctors = doctorService.findDoctors(location, specialization);
        model.addAttribute("doctors", doctors);
        return "doctorList";
    }
}

5. Create the View:

Using Thymeleaf, create a doctorList.html file under src/main/resources/templates:

<!DOCTYPE html>
<html>
<head>
    <title>Doctor Search</title>
</head>
<body>

<h1>Doctor List</h1>

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Specialization</th>
            <th>Location</th>
            <th>Rating</th>
            <th>Availability</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="doctor : ${doctors}">
            <td th:text="${doctor.name}"></td>
            <td th:text="${doctor.specialization}"></td>
            <td th:text="${doctor.location}"></td>
            <td th:text="${doctor.rating}"></td>
            <td th:text="${doctor.availability}"></td>
        </tr>
    </tbody>
</table>

</body>
</html>

6. Running the Application:

Run the main Spring Boot application class. Once the application starts, you can navigate to http://localhost:8080/doctors/search?location=LocationName&specialization=SpecializationName to search for doctors based on location and specialization.

7. Enhancements:

  • Implement authentication and authorization.

  • Add doctor profiles with more details and reviews.

  • Implement a frontend framework for a better user experience.

  • Add filters for ratings and availability.

  • Implement CRUD operations for doctors for admin users.

This is a basic setup and can be expanded with more features and better user interface.