Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http://localhost:8080/getEmployee
- http://localhost:8080/getDepartment
- @RequestMapping(value = "/getEmployees", method = RequestMethod.POST)
- public @ResponseBody List<Employee> getEmp() {
- List<Employee> empList = null;
- empList = services.getEmployee();
- return empList;
- }
- @RequestMapping(value = "/getDepartments", method = RequestMethod.GET)
- public @ResponseBody List<Department> getEmp() {
- List<Department> deptList = null;
- deptList = services.getDepartment();
- return deptList;
- }
- Table 1 (Employee)
- @Entity
- @Table(name = "employee")
- public class Employee implements java.io.Serializable {
- private static final long serialVersionUID = 2650114334774359089L;
- @Id
- @Column(name = "id", unique = true, nullable = false, length = 100)
- private String id;
- @Column(name = "name", unique = true, nullable = false, length = 50)
- private String name;
- // getter setter
- Table 2 (Department)
- @Entity
- @Table(name = "department")
- public class Department implements java.io.Serializable {
- private static final long serialVersionUID = 2650114334774359089L;
- @Id
- @Column(name = "id", unique = true, nullable = false, length = 100)
- private String id;
- @Column(name = "dept_name", unique = true, nullable = false, length = 50)
- private String dept_name;
- // getter setter
- @Autowired
- SessionFactory sessionFactory;
- Session session = null;
- Transaction tx = null;
- static final Logger LOGGER = Logger.getLogger(DataDaoImpl.class);
- public List<Employee> getEmployee() throws Exception {
- List<Employee> result = null;
- session = sessionFactory.openSession();
- String hql = "from Employee";
- Query lQuery = session.createQuery(hql);
- result = lQuery.list();
- tx = session.getTransaction();
- session.beginTransaction();
- tx.commit();
- return result;
- }
- public List<Department> getDepartment() throws Exception {
- List<Department> result = null;
- session = sessionFactory.openSession();
- String hql = "from Department";
- Query lQuery = session.createQuery(hql);
- result = lQuery.list();
- tx = session.getTransaction();
- session.beginTransaction();
- tx.commit();
- return result;
- }
- @Override
- public List<Employee> getEmployee() throws Exception {
- return dataDao.getEmployee();
- }
- @Override
- public List<Department> getDepartment() throws Exception {
- return dataDao.getDepartment();
- }
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/mydb" />
- <property name="username" value="root" />
- <property name="password" value="password"/>
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="annotatedClasses">
- <list>
- <value>com.srdh.model.Employee</value>
- <value>com.srdh.model.Department</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
- <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
- <prop key="hibernate.hbm2ddl.auto">update</prop>
- </props>
- </property>
- </bean>
- <bean id="txManager"
- class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <bean id="persistenceExceptionTranslationPostProcessor"
- class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
- <bean id="dataDao" class="com.test.dao.DaoImpl"></bean>
- <bean id="dataServices" class="com.test.service.ServicesImpl"></bean>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement