Spring MVC and JDBC CRUD with zero XML

In my other tutorials Spring MVC and Spring JDBC ExampleSpring MVC and JDBC CRUD Example  I have used XML configurations but in this tutorial I have used only Annotation, so it means it uses zero XML configurations. This example shows how MVC(Model, View, Controller) works in Spring 4.x. In this tutorial you will also find how JDBC works with Spring MVC. We will also see how annotation like @Autowired works in Spring. You will also see how DispatcherServlet, DataSource and ViewResolver are configured in Spring using only Annotation. This example also shows how to integrate Spring 4 with Maven 3.

You may also read http://www.roytuts.com/spring-boot-mvc-with-maven/

For this tutorial we will create maven based web project in Eclipse.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.


The following configurations are required in order to run the application

Eclipse Kepler
JDK 1.8
Tomcat 8
Have maven 3 installed and configured
Spring 4 dependencies in pom.xml

Now we will see the below steps how to create a maven based project in Eclipse
Step 1. Create a maven based web project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.


Now enter the required fields (Group Id, Artifact Id) as shown below

Group Id : com.roytuts
Artifact Id : spring-mvc-zero-xml

The final project looks like below

spring mvc zero xml

Step 2. Modify the pom.xml file as shown below.

Step 3. If you see JRE System Library[J2SE-1.5] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.5], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Change also the Compiler compliance level as 1.8 from Java -> Compiler.

Step 4. Now when the build process finished then delete web.xml file from WEB-INF

Step 5. Create WebMvcConfig class under src/main/java directory with the below source code

The above class is a configuration class for ViewResolver and JdbcTemplate and methods are annotated with @Bean because they will be automatically resolved as Spring beans. If we do not give any name to @Bean then it will automatically name the bean with the same return type with first letter in lower case. For examples, bean name for public ViewResolver getViewResolver() will be viewResolver and public JdbcTemplate getJdbcTemplate() will be jdbcTemplate. We have annotated with @EnableWebMvc because this class is used in Spring web MVC. We have an annotation @ComponentScan for base package “com.roytuts.springmvc” because we want to let Spring find the annotated classes with @Controller, @Repository, @Service etc. We have also retrieved the datasource properties using @PropertySource annotation.

Below is the application.properties file put under src/main/resources

Step 6. Create WebAppInitializer class under src/main/java directory

So this class initializes few things like whatever we defined in the WebMvcConfig along with DispatcherServlet and Servlet mapping when the application starts up.

Step 7. Create a directory called views under webapp directory for putting created jsp views.

Step 8. Create MySQL table teacher in database roytuts

Step 9. Dump some data into the table

Step 10. Now create POJO class and mapper class which will map Java object to database table teacher

Mapper class

Step 11. Create DAO class for querying database table

Step 12. Create the service class for processing logic or business logic

Step 13. Create Spring controller class which will handle user request and response

Step 14. Now create teacher.jsp file under webapp/views directory for displaying single teacher details

Step 15. Create teachers.jsp file under webapp/views directory for displaying all teachers details

Step 16. Create add.jsp file under webapp/views directory for adding new teacher

Step 17. Create update.jsp file under webapp/views directory for updating new teacher

Step 20. Now run the application on Tomcat server 8 and when the application successfully deployed onto the server, please hit the URL http://localhost:8080/spring-mvc-zero-xml/teachers , you will below output in the browser

spring mvc zero xml

When you hit the URL http://localhost:8080/spring-mvc-zero-xml/teacher/16 in the browser, you will see the below output

spring mvc zero xml

When you click on the update link on page http://localhost:8080/spring-mvc-zero-xml/teachers in the browser, you will see the below output

spring mvc zero xml

When you click on the delete link on page http://localhost:8080/spring-mvc-zero-xml/teachers in the browser, you will see the below output

spring mvc zero xml

When you hit the URL http://localhost:8080/spring-mvc-zero-xml/addTeacher in the browser, you will see the below output

spring mvc zero xml

After add, update and delete of teacher the page navigates automatically to the all teachers page.

Thanks for reading.


Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.