Advertisement
Guest User

Untitled

a guest
Dec 25th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. package com.spring.demo;
  2. public class Employee {
  3.  
  4. private int id;
  5. private String name;
  6. private float salary;
  7. public int getId() {
  8. return id;
  9. }
  10. public void setId(int id) {
  11. this.id = id;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public float getSalary() {
  20. return salary;
  21. }
  22. public void setSalary(float salary) {
  23. this.salary = salary;
  24. }
  25. }
  26.  
  27. package com.spring.demo;
  28. import org.springframework.orm.hibernate5.HibernateTemplate;
  29.  
  30. public class EmployeeDao {
  31. HibernateTemplate template;
  32.  
  33. public void setTemplate(HibernateTemplate template) {
  34. this.template = template;
  35. }
  36.  
  37. public void saveEmployee(Employee e) {
  38. Integer i = (Integer) template.save(e);
  39. if (i > 0) {
  40. System.out.println("Success");
  41. } else {
  42. System.out.println("Not Success");
  43. }
  44. }
  45.  
  46. public void updateEmployee(Employee e) {
  47. template.update(e);
  48. }
  49.  
  50. public void deleteEmployee(Employee e) {
  51. template.delete(e);
  52. }
  53. }
  54.  
  55. package com.spring.demo;
  56. import org.springframework.context.support.ClassPathXmlApplicationContext;
  57.  
  58. public class Test {
  59. public static void main(String[] args) {
  60.  
  61. ClassPathXmlApplicationContext bean = new
  62. ClassPathXmlApplicationContext("spring.xml");
  63.  
  64. EmployeeDao emp = (EmployeeDao) bean.getBean("obj");
  65.  
  66. Employee e = new Employee();
  67. e.setId(2);
  68. e.setName("Amit Goyal");
  69. e.setSalary(40000);
  70.  
  71. emp.saveEmployee(e);
  72. // emp.updateEmployee(e);
  73.  
  74. bean.close();
  75. }
  76. }
  77.  
  78. <?xml version='1.0' encoding='UTF-8'?>
  79. <!DOCTYPE hibernate-mapping PUBLIC
  80. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  81. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  82.  
  83. <hibernate-mapping>
  84. <class name="com.spring.demo.Employee" table="amit1234">
  85. <id name="id">
  86. <generator class="assigned"></generator>
  87. </id>
  88. <property name="name"></property>
  89. <property name="salary"></property>
  90. </class>
  91.  
  92. </hibernate-mapping>
  93.  
  94. <?xml version="1.0" encoding="UTF-8"?>
  95. <beans xmlns="http://www.springframework.org/schema/beans"
  96. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  97. xmlns:p="http://www.springframework.org/schema/p"
  98. xsi:schemaLocation="http://www.springframework.org/schema/beans
  99. http://www.springframework.org/schema/beans/spring-beans-
  100. 4.2.xsd">
  101.  
  102.  
  103. <bean id="dataSource"
  104. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  105. <property name="driverClassName"
  106. value="oracle.jdbc.driver.OracleDriver"/>
  107. <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
  108. <property name="username" value="system" />
  109. <property name="password" value="tiger" />
  110. </bean>
  111.  
  112. <bean id="mysessionFactory"
  113. class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  114. <property name="dataSource" ref="dataSource"></property>
  115.  
  116. <property name="mappingResources">
  117. <list>
  118. <value>Employee.hbm.xml</value>
  119. </list>
  120. </property>
  121.  
  122. <property name="hibernateProperties">
  123. <props>
  124. <prop key="hibernate.dialect">
  125. org.hibernate.dialect.Oracle10gDialect
  126. </prop>
  127. <prop key="hibernate.hbm2ddl.auto">update</prop>
  128. <prop key="hibernate.show_sql">true</prop>
  129. </props>
  130. </property>
  131. </bean>
  132.  
  133. <bean id="template"
  134. class="org.springframework.orm.hibernate5.HibernateTemplate">
  135. <property name="sessionFactory" ref="mysessionFactory"></property>
  136. <property name="checkWriteOperations" value="false"></property>
  137. </bean>
  138.  
  139. <bean id="obj" class="com.spring.demo.EmployeeDao">
  140. <property name="template" ref="template"></property>
  141. </bean>
  142. </beans>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement