This tutorial aims at creating a Dynamic Web Application using Java Spring which implements CRUD(Create - Read - Update - Delete) operations in MySQL database through hibernate.
We will use the following for this tutorial:
- Netbeans 8.0 IDE
- Apache Tomcat 8 ( Version 9 work with netbeans V8.2+)
- MySQL
- Spring
- Hibernate 4
Setting up the database:
Create table student in database tutorial in MySql:
create database tutorial;
use tutorial;
create table student(
roll int primary key,
name varchar(30) not null,
school varchar(30) not null
);
Setting up project in Netbeans:
- Create a new project under web application and name it "webApplication".
- Under Server selection, select Apache Tomcat 8.
- Under Frameworks, select Spring and Hibernate.
When you create new project, web.xml file is generated in WEB-INF folder. When application starts, web.xml loads and scans entire file. Edit this file add
homepage "redirect.jsp"
servlet named "spring"
servlet "spring" mapping with url-pattern "/"
redirect.jsp
Spring-Hibernate
spring
org.springframework.web.servlet.DispatcherServlet
1
spring
/
We have named "spring" servlet in web.xml file, so we need to create this file and add Spring and Hibernate configurations in it. Since the name is spring, servlet name will be "spring-servlet.xml"
This xml file contains base package location(com.tutorial), MVC view resolver, DataSource definition, Hibernate session factory and Hibernate transaction manager.
Place the following credentials in datasource bean:
driverName = com.mysql.jdbc.Driver
databaseurl = jdbc:mysql://localhost/tutorial
username =
password =
org.hibernate.dialect.MySQSDialect
"" in above servlet code means that the project will be annotation driven. Hibernate will search for annotations in the project.
Now under com.tutorial, create an entity package. Under this package, create Student.java class file which acts as an entity.
Student.java
package com.tutorial.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "roll")
private Integer roll;
@Column(name = "name")
private String name;
@Column(name = "school")
private String school;
public Integer getRoll() {
return roll;
}
public void setRoll(Integer roll) {
this.roll = roll;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
}
}
@Table denotes table with name = student
@Id denotes field with Primary Key
@Column denotes a field in the table
Now we need to create a Data Access Object named StudentDAO.java which will be placed in com.tutorial.dao package. This class will contain an interface for adding student, searching and removing student with id, viewing all students.
StudentDAO.java
package com.tutorial.dao;
import com.tutorial.entity.Student;
public interface StudentDAO{
public void saveStudentObj(Student obj);
public List getAllStudents();
public Student getStudentObj(int id);
public void removeStudentObj(int id);
To make our code modular, we will write the above method's implementation in another file StudentDAOImplementation.java.
@Repository annotation means that this file acts as the database of the application.
StudentDAOImplementation.java
package com.tutorial.dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.tutorial.entity.Student;
@Repository
public class StudentDAOImplementation implements StudentDAO {
@Autowired
SessionFactory sessionFactory;
@Override
public void saveStudentObj(Student studentObj) {
sessionFactory.getCurrentSession().saveOrUpdate(studentObj);
}
@Override
public List getAllStudents() {
Session currentSession = sessionFactory.getCurrentSession();
List theQuery = currentSession.createQuery("from Student").list();
return theQuery;
}
@Override
public Student getStudentObj(int id) {
Student studentObj = (Student) sessionFactory.getCurrentSession().get(Student.class, id);
return studentObj;
}
@Override
public void removeStudentObj(int id) {
Student studentObj = (Student) sessionFactory.getCurrentSession().get(Student.class, id);
sessionFactory.getCurrentSession().delete(studentObj);
}
Now lets create services for