Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. http://localhost:8080/getEmployee
  2. http://localhost:8080/getDepartment
  3.  
  4. @RequestMapping(value = "/getEmployees", method = RequestMethod.POST)
  5. public @ResponseBody List<Employee> getEmp() {
  6.  
  7. List<Employee> empList = null;
  8. empList = services.getEmployee();
  9. return empList;
  10. }
  11.  
  12. @RequestMapping(value = "/getDepartments", method = RequestMethod.GET)
  13. public @ResponseBody List<Department> getEmp() {
  14.  
  15. List<Department> deptList = null;
  16. deptList = services.getDepartment();
  17. return deptList;
  18. }
  19.  
  20. Table 1 (Employee)
  21. @Entity
  22. @Table(name = "employee")
  23. public class Employee implements java.io.Serializable {
  24.  
  25. private static final long serialVersionUID = 2650114334774359089L;
  26.  
  27. @Id
  28. @Column(name = "id", unique = true, nullable = false, length = 100)
  29. private String id;
  30.  
  31. @Column(name = "name", unique = true, nullable = false, length = 50)
  32. private String name;
  33.  
  34. // getter setter
  35.  
  36. Table 2 (Department)
  37. @Entity
  38. @Table(name = "department")
  39. public class Department implements java.io.Serializable {
  40.  
  41. private static final long serialVersionUID = 2650114334774359089L;
  42.  
  43. @Id
  44. @Column(name = "id", unique = true, nullable = false, length = 100)
  45. private String id;
  46.  
  47. @Column(name = "dept_name", unique = true, nullable = false, length = 50)
  48. private String dept_name;
  49.  
  50. // getter setter
  51.  
  52. @Autowired
  53. SessionFactory sessionFactory;
  54.  
  55. Session session = null;
  56. Transaction tx = null;
  57.  
  58. static final Logger LOGGER = Logger.getLogger(DataDaoImpl.class);
  59. public List<Employee> getEmployee() throws Exception {
  60. List<Employee> result = null;
  61. session = sessionFactory.openSession();
  62. String hql = "from Employee";
  63. Query lQuery = session.createQuery(hql);
  64. result = lQuery.list();
  65. tx = session.getTransaction();
  66. session.beginTransaction();
  67. tx.commit();
  68. return result;
  69. }
  70.  
  71. public List<Department> getDepartment() throws Exception {
  72. List<Department> result = null;
  73. session = sessionFactory.openSession();
  74. String hql = "from Department";
  75. Query lQuery = session.createQuery(hql);
  76. result = lQuery.list();
  77. tx = session.getTransaction();
  78. session.beginTransaction();
  79. tx.commit();
  80. return result;
  81. }
  82.  
  83. @Override
  84. public List<Employee> getEmployee() throws Exception {
  85. return dataDao.getEmployee();
  86. }
  87.  
  88. @Override
  89. public List<Department> getDepartment() throws Exception {
  90. return dataDao.getDepartment();
  91. }
  92.  
  93. <bean id="dataSource"
  94. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  95.  
  96. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  97. <property name="url" value="jdbc:mysql://localhost:3306/mydb" />
  98. <property name="username" value="root" />
  99. <property name="password" value="password"/>
  100. </bean>
  101.  
  102. <bean id="sessionFactory"
  103. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  104. <property name="dataSource" ref="dataSource" />
  105. <property name="annotatedClasses">
  106. <list>
  107. <value>com.srdh.model.Employee</value>
  108. <value>com.srdh.model.Department</value>
  109.  
  110. </list>
  111. </property>
  112. <property name="hibernateProperties">
  113. <props>
  114. <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
  115. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
  116. <prop key="hibernate.hbm2ddl.auto">update</prop>
  117. </props>
  118. </property>
  119. </bean>
  120.  
  121. <bean id="txManager"
  122. class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  123. <property name="sessionFactory" ref="sessionFactory" />
  124. </bean>
  125.  
  126. <bean id="persistenceExceptionTranslationPostProcessor"
  127. class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
  128.  
  129. <bean id="dataDao" class="com.test.dao.DaoImpl"></bean>
  130. <bean id="dataServices" class="com.test.service.ServicesImpl"></bean>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement