Spring MVC Hibernate MySQL CRUD Example
Spring MVC Hibernate MySQL CRUD Example
In this post, we are going to see integration of Spring MVC, hibernate and mysql CRUD
example.
We have already seen integration of Spring Rest with hibernate in previous tutorial.
Here are steps to create a project with Spring MVC , hibernate and mySQL crud example.
Maven dependencies
2) We are using Spring 4 and Hibernate 4 for this application. It should work with
hibernate 3 also but we need to do small changes in hibernate configuration.
pom.xml
<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.arpit.java2blog</groupId>
<artifactId>SpringMVCHibernateCRUDExample</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVCHibernateCRUDExample Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.1</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<spring.version>4.2.1.RELEASE</spring.version>
<security.version>4.0.3.RELEASE</security.version>
<jdk.version>1.7</jdk.version>
<hibernate.version>4.3.5.Final</hibernate.version>
<org.aspectj-version>1.7.4</org.aspectj-version>
</properties>
</project>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Please change context:component-scan if you want to use different package for spring to
search for controller.Please refer to spring mvc hello world example for more understanding.
<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory"
ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>
</beans:beans>
dataSource bean is used to specify java data source. We need to provide driver, URL ,
Username and Password.
transactionManager bean is used to configure hibernate transaction manager.
hibernate4AnnotatedSessionFactory bean is used to configure FactoryBean that creates a
Hibernate SessionFactory. This is the common way to set up a shared Hibernate
SessionFactory in a Spring application context, so you can use this SessionFactory to inject
in Hibernate data access objects.
Create a applicationcontext.xml in WEB-INF folder, this file is used for bean configuration as
we are using spring-servlet.xml for bean configuration , we will keep this file empty.
package org.arpit.java2blog.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;
@Column(name="countryName")
String countryName;
@Column(name="population")
long population;
public Country() {
super();
}
public Country(int i, String countryName,long population) {
super();
this.id = i;
this.countryName = countryName;
this.population=population;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public long getPopulation() {
return population;
}
public void setPopulation(long population) {
this.population = population;
}
}
@Entity is used for making a persistent pojo class.For this java class,you will have
corresponding table in database. @Column is used to map annotated attribute to
corresponding column in table. So Create Country table in mysql database with following
code:
Create Controller
6) Create a controller named "CountryController.java" in package
org.arpit.java2blog.controller
package org.arpit.java2blog.controller;
import java.util.List;
import org.arpit.java2blog.model.Country;
import org.arpit.java2blog.service.CountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class CountryController {
@Autowired
CountryService countryService;
package org.arpit.java2blog.dao;
import java.util.List;
import org.arpit.java2blog.model.Country;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class CountryDAO {
@Autowired
private SessionFactory sessionFactory;
@Repository is specialised component annotation which is used to create bean at DAO layer.
We have use Autowired annotation to inject hibernate SessionFactory into CountryDAO class.
We have already configured hibernate SessionFactory object in Spring-Servlet.xml file.
package org.arpit.java2blog.service;
import java.util.List;
import org.arpit.java2blog.dao.CountryDAO;
import org.arpit.java2blog.model.Country;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service("countryService")
public class CountryService {
@Autowired
CountryDAO countryDao;
@Transactional
public List<Country> getAllCountries() {
return countryDao.getAllCountries();
}
@Transactional
public Country getCountry(int id) {
return countryDao.getCountry(id);
}
@Transactional
public void addCountry(Country country) {
countryDao.addCountry(country);
}
@Transactional
public void updateCountry(Country country) {
countryDao.updateCountry(country);
}
@Transactional
public void deleteCountry(int id) {
countryDao.deleteCountry(id);
}
}
@Service is specialised component annotation which is used to create bean at Service layer.
Create view
Create view called countryDetails.jsp in WEB-INF/view/ folder.
As you can see, we did not add any country to the list, so it is empty.
Lets add Country named India to country list and click submit.
Similarly we will add China , Bhutan and Nepal respectively and you will see below screen.
As you can see, china got deleted from above list.
Project structure: