Spring MVC - Sample Project For Finding Doctors Online with MySQL
Spring MVC Framework follows the Model-View-Controller design pattern. It is used to develop web applications. It works around DispatcherServlet. DispatcherServlet handles all the HTTP requests and responses. With MySQL as the backend, we can store all doctor details and by using Spring MVC functionality we can get the details of doctors as the online pattern. Let us see it as a maven project here.
MySQL Queries:
As we are getting the details via MySQL, let us have some data for it
DROP DATABASE IF EXISTS geeksforgeeks; CREATE DATABASE geeksforgeeks; USE geeksforgeeks; DROP TABLE geeksforgeeks.DoctorsDetails; CREATE TABLE DoctorsDetails ( id int(6) unsigned NOT NULL, doctorName varchar(50) NOT NULL, doctorRegistrationNumber varchar(10) NOT NULL, qualification varchar(30) NOT NULL, gender varchar(10) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1; INSERT INTO geeksforgeeks.DoctorsDetails (id,doctorName, doctorRegistrationNumber, qualification,gender) VALUES (1,'doctorA','123-456','MDDCH','Female'); INSERT INTO geeksforgeeks.DoctorsDetails (id,doctorName, doctorRegistrationNumber, qualification,gender) VALUES (1,'doctorB','111-222','MSNeuro','Male'); INSERT INTO geeksforgeeks.DoctorsDetails (id,doctorName, doctorRegistrationNumber, qualification,gender) VALUES (1,'doctorC','222-444','MDGynae','Female'); INSERT INTO geeksforgeeks.DoctorsDetails (id,doctorName, doctorRegistrationNumber, qualification,gender) VALUES (1,'doctorD','199-998','MSNephro','Male'); INSERT INTO geeksforgeeks.DoctorsDetails (id,doctorName, doctorRegistrationNumber, qualification,gender) VALUES (1,'doctorE','444-666','MDCardio','Female'); SELECT * FROM geeksforgeeks.DoctorsDetails; --If required, at last point of time we can truncate table truncate table geeksforgeeks.DoctorsDetails;
DBData Output:

Project Structure:

This is going to get executed as a maven project
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.doctors</groupId>
<artifactId>SpringMVCFindDoctorsOnline</artifactId>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVCFindDoctorsOnline Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.1.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVCFindDoctorsOnline</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<shutdown>kill</shutdown>
<!-- Use it if required-->
</configuration>
</plugin>
<!-- This should be added to overcome Could not initialize
class org.apache.maven.plugin.war.util.WebappStructureSerializer -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</build>
</project>
We have to create a bean file and its structure should match with MySQL geeksforgeeks.DoctorsDetails
Doctor.java
public class Doctor {
private int id;
private String doctorName;
private String doctorRegistrationNumber;
private String gender;
private String qualification;
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDoctorName() {
return doctorName;
}
public void setDoctorName(String doctorName) {
this.doctorName = doctorName;
}
public String getDoctorRegistrationNumber() {
return doctorRegistrationNumber;
}
public void setDoctorRegistrationNumber(String doctorRegistrationNumber) {
this.doctorRegistrationNumber = doctorRegistrationNumber;
}
public String getQualification() {
return qualification;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}
}