Spring Hibernate MySql Example
In this example we will demonstrate how to use Hibernate in Spring framework in MySql Database.
Final project file structure should look exactly like following
Here is the Content for 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mahesh</groupId>
<artifactId>SpringHibernate</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Spring Hibernate Maven</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mahesh</groupId>
<artifactId>SpringHibernate</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Spring Hibernate Maven</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.10.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.16</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.6</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.4.GA</version>
</dependency>
</dependencies>
</project>
Here is the Content for Employee.java
package com.mahesh.training;
import java.io.Serializable;
public class Employee implements Serializable{
private int empId;
private String empName;
private String designation;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public Employee(int empId, String empName, String designation) {
super();
this.empId = empId;
this.empName = empName;
this.designation = designation;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
}
import java.io.Serializable;
public class Employee implements Serializable{
private int empId;
private String empName;
private String designation;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public Employee(int empId, String empName, String designation) {
super();
this.empId = empId;
this.empName = empName;
this.designation = designation;
}
public Employee() {
super();
// TODO Auto-generated constructor stub
}
}
Here is the Content for EmployeeHibernateDAO.java
package com.mahesh.training;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class EmployeeHibernateDAO {
private HibernateTemplate template;
public HibernateTemplate getTemplate() {
return template;
}
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public void addEmployee(Employee e)
{
template.save(e);
//template.
}
}
import org.springframework.orm.hibernate3.HibernateTemplate;
public class EmployeeHibernateDAO {
private HibernateTemplate template;
public HibernateTemplate getTemplate() {
return template;
}
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public void addEmployee(Employee e)
{
template.save(e);
//template.
}
}
Here is the Content for hibernateAppContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/trainingtestdb"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="minIdle" value="1"></property>
<property name="maxActive" value="10"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>Employee.hbm.xml</value>
</list>
</property>
</bean>
<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="hibernateDAO" class="com.mahesh.training.EmployeeHibernateDAO">
<property name="template" ref="template"></property>
</bean>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/trainingtestdb"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="minIdle" value="1"></property>
<property name="maxActive" value="10"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>Employee.hbm.xml</value>
</list>
</property>
</bean>
<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="hibernateDAO" class="com.mahesh.training.EmployeeHibernateDAO">
<property name="template" ref="template"></property>
</bean>
</beans>
Here is the Content for Employee.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 4 Jan, 2012 3:23:12 PM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
<class name="com.mahesh.training.Employee" table="EMPLOYEE_SPRING_TBL">
<id name="empId" type="int">
<column name="EMP_ID" />
</id>
<property name="empName" type="java.lang.String">
<column name="EMP_NAME" />
</property>
<property name="designation" type="java.lang.String">
<column name="DESIGNATION" />
</property>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 4 Jan, 2012 3:23:12 PM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
<class name="com.mahesh.training.Employee" table="EMPLOYEE_SPRING_TBL">
<id name="empId" type="int">
<column name="EMP_ID" />
</id>
<property name="empName" type="java.lang.String">
<column name="EMP_NAME" />
</property>
<property name="designation" type="java.lang.String">
<column name="DESIGNATION" />
</property>
</class>
</hibernate-mapping>
Here is the Content for EmployeeDAO.java
package com.mahesh.training;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
public class EmployeeDAO {
private SimpleJdbcTemplate template;
private SimpleJdbcInsert insertTemplate;
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public EmployeeDAO(DataSource ds) {
super();
// TODO Auto-generated constructor stub
this.dataSource=ds;
template=new SimpleJdbcTemplate(dataSource);
insertTemplate=new SimpleJdbcInsert(dataSource).withTableName("EMPLOYEE_SPRING_TBL");
}
public void addEmployee(int id,String name,String designation)
{
String sql="insert into EMPLOYEE_SPRING_TBL values(?,?,?)";
template.update(sql, id,name,designation);
/*insertTemplate.setTableName("EMPLOYEE_SPRING_TBL");
Map<String, Object> values=new HashMap<String, Object>();
values.put("empid", id);
values.put("empname", name);
values.put("designation", designation);
insertTemplate.execute(values);*/
}
}
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
public class EmployeeDAO {
private SimpleJdbcTemplate template;
private SimpleJdbcInsert insertTemplate;
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public EmployeeDAO(DataSource ds) {
super();
// TODO Auto-generated constructor stub
this.dataSource=ds;
template=new SimpleJdbcTemplate(dataSource);
insertTemplate=new SimpleJdbcInsert(dataSource).withTableName("EMPLOYEE_SPRING_TBL");
}
public void addEmployee(int id,String name,String designation)
{
String sql="insert into EMPLOYEE_SPRING_TBL values(?,?,?)";
template.update(sql, id,name,designation);
/*insertTemplate.setTableName("EMPLOYEE_SPRING_TBL");
Map<String, Object> values=new HashMap<String, Object>();
values.put("empid", id);
values.put("empname", name);
values.put("designation", designation);
insertTemplate.execute(values);*/
}
}
Here is the Content for applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/trainingtestdb"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<property name="minIdle" value="1"></property>
<property name="maxActive" value="10"></property>
</bean>
<bean id="employeeDAO" class="com.mahesh.training.EmployeeDAO">
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
</beans>
To run the Application below is the Test classes.
Here is the Content for HibernateSpringTest.java
package clientpack;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mahesh.training.Employee;
import com.mahesh.training.EmployeeHibernateDAO;
public class HibernateSpringTest {
public static void main(String[] args) {
ApplicationContext ctx=
new ClassPathXmlApplicationContext("hibernateAppContext.xml");
EmployeeHibernateDAO dao=(EmployeeHibernateDAO)ctx.getBean("hibernateDAO");
dao.addEmployee(new Employee(1, "Mahesh", "Tech Lead"));
dao.addEmployee(new Employee(2, "Chegu", "Tech Lead"));
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mahesh.training.Employee;
import com.mahesh.training.EmployeeHibernateDAO;
public class HibernateSpringTest {
public static void main(String[] args) {
ApplicationContext ctx=
new ClassPathXmlApplicationContext("hibernateAppContext.xml");
EmployeeHibernateDAO dao=(EmployeeHibernateDAO)ctx.getBean("hibernateDAO");
dao.addEmployee(new Employee(1, "Mahesh", "Tech Lead"));
dao.addEmployee(new Employee(2, "Chegu", "Tech Lead"));
}
}
OutPut:
Here the Content for JdbcTest.java
package clientpack;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mahesh.training.EmployeeDAO;
public class JdbcTest {
public static void main(String[] args) {
ApplicationContext ctx=
new ClassPathXmlApplicationContext("/applicationContext.xml");
EmployeeDAO dao=(EmployeeDAO)ctx.getBean("employeeDAO");
dao.addEmployee(3, "Mahesh", "Tech Lead");
dao.addEmployee(4, "Chegu", "Tech Lead");
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mahesh.training.EmployeeDAO;
public class JdbcTest {
public static void main(String[] args) {
ApplicationContext ctx=
new ClassPathXmlApplicationContext("/applicationContext.xml");
EmployeeDAO dao=(EmployeeDAO)ctx.getBean("employeeDAO");
dao.addEmployee(3, "Mahesh", "Tech Lead");
dao.addEmployee(4, "Chegu", "Tech Lead");
}
}
where can i download the source code
ReplyDelete