This post will show how to create a Student Enrollment Application using MYSQL DB with MyBatis framework in a Spring environment. This is a simple application that aims to collect the input details from the user during signup, save the details in the MYSQL DB and authenticate the same during login.
1. Create Java Web Application Project using Maven Template
To begin with, in the IDE, create a Java Maven project with the template of maven-archetype-webapp (Filter the catalog based on the string “webapp”) by providing appropriate values for GroupId and Artifact Id for the project. The sample web application directory structure is shown below with a standard deployment descriptor web.xml and Maven pom.xml
2. Update pom.xml
To make the above Maven Java Web Application project support the MyBatis framework, add the following dependencies to the existing pom.xml
- mybatis (for MyBatis support)
- mybatis-spring (for MyBatis-Spring integration support)
- jstl, spring-webmvc, servlet-api and spring-context-support (for Spring support)
- spring-test (may be optional, needed if Spring-test support is needed)
- mysql-connector-java (for MYSQL support)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
3. Modify web.xml
Modify the contents of the web.xml to include the following:
- A servlet and specify the location of the configuration file for the same. In this sample, a configuration file named springConfig.xml is created under WEB-INF/config folder in the project layout.
- A servlet-mapping to map the servlet created in the above step that should be invoked when the client specifies the url matching the url pattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
4. Create the Spring Configuration File
Create a Spring Bean Configuration file under the folder WEB-INF/config. If STS(Spring Tool Suite) is the IDE, go ahead and enable the context, mvc and tx namespaces. The springConfig.xml will be as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
After enabling the required namespaces, include the following (in between the <beans> and </beans> tags) to indicate that the application is annotation driven and base package for the context component scan.
1 2 3 |
|
Include the bean InternalResourceViewResolver of Spring to locate the jsp files
1 2 3 4 |
|
Include the bean for data source, where the properties of the MYSQL DB like url, username and password can be specified. Replace <include connection url> with the actual connection url for connecting to the MYSQL DB. Likewise, replace <include username> and <include password> with the actual username and password values.
1 2 3 4 5 6 |
|
Include the bean for transaction manager for scoping/controlling the transactions, that takes the data source defined above as reference (dependent)
1 2 3 4 |
|
Coming to the MyBatis specific configurations, include the bean for sqlSessionFactory which is the central configuration in a MyBatis application. This bean takes in three properties – dataSource (already configured above) – typeAliasesPackage (location where the model classes of this application resides) – mapperLocations (location where the mapper xml files for the model resides – this is not needed here as annotation based configurations are used instead)
More details of this can be read at http://mybatis.github.io/mybatis-3/
1 2 3 4 5 |
|
Include the bean for sqlSession
1 2 3 |
|
Next and finally, include the bean for MapperScannerConfigurer
1 2 3 |
|
5. Create JSP Files for Student Signup/Login
Create a folder named “jsp” under WEB-INF (This is where the jsp files will be created as indicated in the springConfig.xml for the InternalResourceViewResolver bean).
Create a file signup.jsp to include a form to get the input details like UserName, Password, FirstName, LastName, DateOfBirth and EmailAddress of the student. A snapshot of the signup page is as follows:
Next, create a file login.jsp to include a form with UserName and Password. A snapshot of the login page is as follows:
Also create success.jsp to indicate the login success and failure.jsp to indicate login failure (These are just pages used to display the contents – no processing logic involved).
This application uses twitter bootstrap http://getbootstrap.com/ and http://bootswatch.com/united/ as style sheets. It also uses a datepicker stylesheet as well to pop up a calendar for the DateOfBirth field in the Student Signup page (http://www.eyecon.ro/bootstrap-datepicker/).
A reference link to the files under webapp folder of this application can be found at https://github.com/elizabetht/StudentEnrollmentWithMyBatis/tree/master/src/main/webapp
6. Create packages for Controller, Model, Service and Mappers
Create packages each for the Spring Controller, Model and Service classes under the src/main/java folder. Also create a package for the MyBatis Mapper class under the same src/main/java folder.
A sample snapshot of the project after the package creation is as shown below:
7. Create classes for Model Tier
Create a POJO class named Student.java inside the package com.github.elizabetht.model to include the details of the Student model entity during signup. Create another POJO class named StudentLogin.java inside the same package com.github.elizabetht.model to include the Student Login details.
A reference link to the files for the Model classes can be found at https://github.com/elizabetht/StudentEnrollmentWithMyBatis/tree/master/src/main/java/com/github/elizabetht/model
8. Create classes for MyBatis Mapper
A Mapper in MyBatis framework is similar to the Repository tier in a Spring environment. Crude SQL queries takes its place here. Create an interface class named StudentMapper.java inside the package com.github.elizabetht.mapper to support the database operations.
There are two interface methods needed for the application’s purpose.
- To Insert the Student Signup details into the Database
- To Verify the Student Login details from the Database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
9. Create classes for Service Tier
Create an interface class named StudentService.java inside the package com.github.elizabetht.service to support the service tier operations.
1 2 3 4 5 |
|
Create a service tier implementation class (a POJO indeed) named StudentServiceImpl.java inside the package com.github.elizabetht.service. This is where the application logic goes – either to save the student details into the database or to verify the student (already saved) details from the database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
When using MyBatis with Spring, a mapper can be directly injected into the service tier. This is probably the strongest point of the Spring integration of MyBatis. This is the only tool that I am aware that lets to build the application with no imports to it.
10. Create class for Controller Tier
Create a Controller tier POJO class named StudentController.java inside the package com.github.elizabetht.controller. This is where the routing logic of the application goes – whether a signup or login action is called.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
11. Create the DB Schema in a MYSQL DB
Connect to the MySQL DB which is to be used for this application and create a new DB Schema named studentEnrollment using the MySQL Workbench. This is necessary as the DB Schema name of studentEnrollment is specified in the dataSource bean in springConfig.xml
Once the studentEnrollment DB Schema is created, create a table named student inside the DB Schema using the CREATE TABLE statement as follows:
1 2 3 4 5 6 7 8 9 10 |
|
12. Deploying the Application on Tomcat Server
Once the above steps are complete and the project is successfully built, the Java web application is ready to deployed on the Tomcat Server 7.
The Java web application can be deployed locally by right clicking on the project and choosing the “Run As->Run on Server” option.
The same can be deployed remotely on any native server that supports Tomcat by copying the WAR file (Right click on the project and choose Export as WAR File option) to /var/lib/tomcat7 folder (or appropriate tomcat directory) and restarting the tomcat server.
13. Clone or Download code
If using git, clone a copy of this project here: https://github.com/elizabetht/StudentEnrollmentWithMyBatis.git
In case of not using git, download the project as ZIP or tar.gz file here: https://github.com/elizabetht/StudentEnrollmentWithMyBatis/releases/tag/1.7