Tuesday, May 20, 2008

spring hibernate standalone example

Hi, This is developed in eclipse.I given the structure wt eclipse follows
1.First open eclipse File - new - javaproject After that it opens one new window give name for that project after that click next and
finish
2.structure is like this
siva
src
bin
.classpath
3.create package. right click on src new package
com.siva
com.siva.domain
com.siva.service
com.siva.dao
com.siva.mappings
config-it is new folder to keep jdbc.properties file it is not package but it is inside src folder only
4.create new class on first package the class like this


/**
*
*/
package com.siva;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.siva.service.SimpleServiceImpl;

/**
* @author sivakumar
*
*/
public class SimpleSpringHibernate
{
public static void main(String[] args) {
System.out.println("WELCOME TO SIMPLE SPRING HIBERNATE EXAMPLE");
ApplicationContext context= new ClassPathXmlApplicationContext ("applicationContext.xml");
System.out.println("application context loaded successfully");
SimpleServiceImpl simple =(SimpleServiceImpl)context.getBean("simpleService");
System.out.println("simple Service called successfully");
simple.getDetails();


}

}


5.Now we need one service class
it look like

package com.siva.service;

import com.siva.dao.SimpleDaoHibernate;

public class SimpleServiceImpl
{
private SimpleDaoHibernate aSimpleDao;
public SimpleServiceImpl(SimpleDaoHibernate simpleDao)
{
aSimpleDao = simpleDao;
}
public void getDetails()
{ System.out.println("inside SimpleServiceImpl method");
aSimpleDao.getDetails();

}

}
6.DAo class look the following

package com.siva.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.siva.domain.Emp;

public class SimpleDaoHibernate extends HibernateDaoSupport
{
public void getDetails()
{
System.out.println("insdie DAO Hibernate class");
String sql= "from Emp";
List details = (ArrayList)getHibernateTemplate().find(sql);
System.out.println("details is"+details.size());
System.out.println("Query executed successfully");
}
}


7.domain class like the following it is for OR Mapping


/**
*
*/
package com.siva.domain;

/**
* @author sivakumar
*
*/
public class Emp {
private Long id;
private String name;
private String salary;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}

}


8.The mapping hbm file look the following

<hibernate-mapping package="com.siva.domain"
<class name="Emp" table="emp"
name="id" column="id" type="java.lang.Long"

name="name" column="name" type="string"


name="salary" column="salary" type="string" />




9.appicationContext.xml it is like the following this file inside src folder.


beans
bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
property name="locations"
list
value
classpath:config/jdbc.properties
value
list
property
bean

bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> property name="url" value="${hibernate.sem.connection.url}" />
property name="driverClassName"
value="${hibernate.sem.connection.driver_class}" />
property name="username"
value="${hibernate.sem.connection.username}" />
property name="password"
value="${hibernate.sem.connection.password}" /> bean

bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
property name="hibernateProperties">
props
prop key="hibernate.dialect"> ${hibernate.sem.dialect}
prop
prop key="hibernate.show_sql"> ${hibernate.sem.show_sql}
prop
props
property
property name="dataSource"
ref bean="dataSource" /> property
property name="mappingDirectoryLocations">
list
value
classpath:/com/siva/mappings/
value
list
property
bean
bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
property name="sessionFactory"> ref bean="sessionFactory" />
property
bean


bean id="simpleDao" class="com.siva.dao.SimpleDaoHibernate">
property name="hibernateTemplate"> ref bean="hibernateTemplate" /> property
bean


bean id="simpleService"
class="com.siva.service.SimpleServiceImpl">
constructor-arg ref="simpleDao">constructor-arg
bean

beans




10.jdbc.properties file

#hibernate.sem.dialect=org.hibernate.dialect.Oracle9Dialect
#hibernate.sem.show_sql=true
#hibernate.sem.connection.driver_class=oracle.jdbc.driver.OracleDriver
#hibernate.sem.connection.url=jdbc:oracle:thin:@localhost:1521:XE
#hibernate.sem.connection.username=user
#hibernate.sem.connection.password=password
hibernate.sem.dialect=org.hibernate.dialect.MySQLDialect
hibernate.sem.show_sql=true
hibernate.sem.connection.driver_class=com.mysql.jdbc.Driver
hibernate.sem.connection.url=jdbc:mysql://localhost:3306/test
hibernate.sem.connection.username=root
hibernate.sem.connection.password=

2 comments:

Contact Form

Name

Email *

Message *