Professional Documents
Culture Documents
Integration Spring and Hibernate
Integration Spring and Hibernate
3.
4.
5.
6.
7. );
Step 2: Creating Employee Class
Employee.java
view plainprint?
1. package com.dineshonjava.sdnext.domain;
2.
3. import javax.persistence.Column;
4. import javax.persistence.Entity;
5. import javax.persistence.GeneratedValue;
6. import javax.persistence.GenerationType;
7. import javax.persistence.Id;
8. import javax.persistence.Table;
9.
10./**
11. * @author Dinesh Rajput
12. *
13. */
14.@Entity
15.@Table(name="Employee")
16.public class Employee {
17. @Id
18. @Column(name="EMPID")
19. @GeneratedValue(strategy=GenerationType.AUTO)
20. private int empid;
21.
22. @Column(name="NAME")
23. private String name;
24.
25. @Column(name="AGE")
26. private int age;
27.
28. @Column(name="SALARY")
29. private long salary;
30. /**
31. * @return the empid
32. */
33. public int getEmpid() {
34. return empid;
35. }
36. /**
37. * @param empid the empid to set
38. */
39. public void setEmpid(int empid) {
40. this.empid = empid;
41. }
42. /**
43. * @return the name
44. */
45. public String getName() {
46. return name;
47. }
48. /**
49. * @param name the name to set
50. */
51. public void setName(String name) {
82. }
83.}
Step 3: Creating the Spring Configuration File (spring.xml)
view plainprint?
1. <beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:con
text="http://www.springframework.org/schema/context" xmlns:p="http://ww
w.springframework.org/schema/p" xmlns:security="http://www.springframew
ork.org/schema/security" xmlns:tx="http://www.springframework.org/schema
/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmln
s="http://www.springframework.org/schema/beans" xsi:schemalocation="htt
p://www.springframework.org/schema/beans http://www.springframework.org/
schema/beans/spring-beans-2.5.xsd
2.
http://www.springframework.org/schema/context http://www.springframe
work.org/schema/context/spring-context-2.5.xsd
3.
http://www.springframework.org/schema/security http://www.springframe
work.org/schema/security/spring-security-2.0.4.xsd
4.
http://www.springframework.org/schema/aop http://www.springframewor
k.org/schema/aop/spring-aop-2.5.xsd
5.
http://www.springframework.org/schema/tx http://www.springframework.org/
schema/tx/spring-tx-2.5.xsd">
6.
7. <context:annotation-config></context:annotation-config>
8.
9. <context:component-scan basepackage="com.dineshonjava.sdnext.dao.impl">
10.</context:component-scan>
11.
12.<!-- Creating data source -->
13.<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource"
>
14. <property name="driverClassName" value="com.mysql.jdbc.Driver"></prop
erty>
15. <property name="url" value="jdbc:mysql://localhost:3306/DAVDB"></prope
rty>
16. <property name="username" value="root"></property>
17. <property name="password" value="root"></property>
18. <property name="initialSize" value="2"></property>
19. <property name="maxActive" value="5"></property>
20.</bean>
21.
22.<!-- Creating session factory -->
23.<bean class="org.springframework.orm.hibernate3.annotation.AnnotationSes
sionFactoryBean" id="sessionFactory">
24.
25.
<property name="hibernateProperties">
26.
27.
28.
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
29.
</prop>
30.
<prop key="hibernate.show_sql">true</prop>
31.
</props>
32.
</property>
33.
34.
<property name="annotatedClasses">
35.
<list>
36. <value>
37.
com.dineshonjava.sdnext.domain.Employee
38.
</value>
39.
40.
</list>
</property>
41.</bean>
42.
43.<bean class="com.dineshonjava.sdnext.dao.impl.EmployeeDaoImpl" id="em
ployeeDaoImpl">
44.
45.</property></bean>
46.</beans>
Step 4: Creating the DAO class (EmployeeDao.java)
view plainprint?
1. package com.dineshonjava.sdnext.dao;
2.
3. import java.util.List;
4.
5. import com.dineshonjava.sdnext.domain.Employee;
6.
7. /**
8.
9.
10. */
11.public interface EmployeeDao {
12. /**
13. * This is the method to be used to create
14. * a record in the Employee table.
15. */
16. void createEmployee(Employee employee);
17. /**
18. * This is the method to be used to list down
19. * a record from the Employee table corresponding
20. * to a passed Employee id.
21. */
22. Employee getEmployee(Integer empid);
23. /**
24. * This is the method to be used to list down
25. * all the records from the Employee table.
26. */
27. List<employee> listEmployees();
28. /**
29. * This is the method to be used to delete
30. * a record from the Employee table corresponding
31. * to a passed Employee id.
32. */
33. void delete(Employee employee);
34. /**
35. * This is the method to be used to update
36. * a record into the Employee table.
37. */
38. void update(Employee employee);
39.}
40.</employee>
SuperHibernateDaoSupport.java
view plainprint?
1. package com.dineshonjava.sdnext.dao.util;
2.
3. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
4.
5. /**
6.
7.
8.
*/
1. package com.dineshonjava.sdnext.dao.impl;
2.
3. import java.util.List;
4.
5. import org.springframework.stereotype.Repository;
6.
7. import com.dineshonjava.sdnext.dao.EmployeeDao;
8. import com.dineshonjava.sdnext.dao.util.SuperHibernateDaoSupport;
9. import com.dineshonjava.sdnext.domain.Employee;
10.
11./**
12. * @author Dinesh Rajput
13. *
14. */
15.@Repository
16.public class EmployeeDaoImpl extends SuperHibernateDaoSupport implemen
ts EmployeeDao {
17.
18. @Override
19. public void createEmployee(Employee employee) {
20. getHibernateTemplate().saveOrUpdate(employee);
21. }
22.
23. @Override
24. public Employee getEmployee(Integer empid) {
4. import org.springframework.context.support.ClassPathXmlApplicationContext;
5.
6. import com.dineshonjava.sdnext.dao.EmployeeDao;
7. import com.dineshonjava.sdnext.domain.Employee;
8.
9. /**
10. * @author Dinesh Rajput
11. *
12. */
13.public class EmpMainApp {
14.
15. /**
16. * @param args
17. */
18. public static void main(String[] args) {
19.
20.ApplicationContext context = new ClassPathXmlApplicationContext("spring.x
ml");
21.EmployeeDao empDao = (EmployeeDao) context.getBean("employeeDaoImpl
");
22.
23. Employee employee = new Employee();
24. employee.setName("Dinesh");
25. employee.setAge(25);
26. employee.setSalary(50000l);
System.out.print(emp);
}