Advertisement
Guest User

Untitled

a guest
Mar 9th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:security="http://www.springframework.org/schema/security"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:jee="http://www.springframework.org/schema/jee"
  8. xmlns:lang="http://www.springframework.org/schema/lang"
  9. xmlns:p="http://www.springframework.org/schema/p"
  10. xmlns:tx="http://www.springframework.org/schema/tx"
  11. xmlns:util="http://www.springframework.org/schema/util"
  12. xmlns:mvc="http://www.springframework.org/schema/mvc"
  13. xsi:schemaLocation="http://www.springframework.org/schema/beans
  14. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  15. http://www.springframework.org/schema/context
  16. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  17. http://www.springframework.org/schema/tx
  18. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  19. http://www.springframework.org/schema/mvc
  20. https://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  21. http://www.springframework.org/schema/security
  22. http://www.springframework.org/schema/security/spring-security-4.0.xsd">
  23. <mvc:annotation-driven/>
  24. <tx:annotation-driven transaction-manager="txManager"/>
  25. <context:component-scan base-package="ua"/>
  26. <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  27. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
  28. <property name="prefix" value="/webapp/jsp"/>
  29. <property name="suffix" value=".jsp"/>
  30. </bean>
  31.  
  32. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  33. <property name="dataSource" ref="dataSource"/>
  34. <property name="packagesToScan" value="ua.model"/>
  35. <property name="hibernateProperties">
  36. <props>
  37. <prop key="hibernate.hbm2ddl.auto">create</prop>
  38. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  39. <prop key="hibernate.show_sql">true</prop>
  40. </props>
  41. </property>
  42. </bean>
  43. <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  44. <property name="sessionFactory" ref="sessionFactory"/>
  45. </bean>
  46. <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
  47. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  48. <property name="url" value="jdbc:mysql://localhost:3306/postschema"/>
  49. <property name="username" value="root"/>
  50. <property name="password" value="peroser12"/>
  51. </bean>
  52. </beans>
  53.  
  54. @Controller
  55. public class SSController {
  56.  
  57. Logger logger = Logger.getLogger(SSController.class);
  58.  
  59. private PostDAO postDAO;
  60.  
  61. private CategoryDAO categoryDAO;
  62.  
  63. @RequestMapping (value = "/index", method = RequestMethod.GET)
  64. public ModelAndView allList (){
  65. ModelAndView modelAndView = new ModelAndView();
  66. List<Post> posts = postDAO.getAll();
  67. List<Category> categories = categoryDAO.getAll();
  68. modelAndView.addObject("posts", posts);
  69. modelAndView.addObject("categories", categories);
  70. modelAndView.setViewName("posts");
  71. return modelAndView;
  72. }
  73. }
  74.  
  75. public abstract class AbstractDAOImplementation<G> implements AbstractDao<G> {
  76.  
  77. @Autowired
  78. @Qualifier(value = "sessionFactory")
  79. private SessionFactory sessionFactory;
  80.  
  81. @Transactional
  82. @Override
  83. public void create(G entity) {
  84. Session session = sessionFactory.openSession();
  85. session.beginTransaction();
  86. session.save(entity);
  87. session.getTransaction().commit();
  88. session.close();
  89. }
  90. @Transactional
  91. @Override
  92. public void delete(G entity) {
  93. Session session = sessionFactory.openSession();
  94. session.beginTransaction();
  95. session.delete(entity);
  96. session.getTransaction().commit();
  97. session.close();
  98. }
  99. @Transactional
  100. @Override
  101. public G edit(G entity) {
  102. Session session = sessionFactory.openSession();
  103. session.beginTransaction();
  104. session.saveOrUpdate(entity);
  105. session.getTransaction().commit();
  106. session.close();
  107.  
  108. return entity;
  109. }
  110. @Transactional
  111. @Override
  112. public G getById(long id) {
  113. Session session = sessionFactory.openSession();
  114. session.beginTransaction();
  115. G entity = getEntityById(session, id);
  116. session.getTransaction().commit();
  117. session.close();
  118. return entity;
  119. }
  120. @Transactional
  121. @Override
  122. public List<G> getAll() {
  123. Session session = sessionFactory.openSession();
  124. session.beginTransaction();
  125. List<G> result = getAllEntity(session);
  126. return result;
  127. }
  128.  
  129. public abstract G getEntityById (Session session, long id);
  130.  
  131. public abstract List<G> getAllEntity(Session session);
  132. }
  133.  
  134. public class PostDAO extends AbstractDAOImplementation<Post> {
  135. @Override
  136. public Post getEntityById(Session session, long id) {
  137. Post result = (Post) session.createQuery("from Post where id="+id);
  138. return result;
  139. }
  140.  
  141. @Override
  142. public List<Post> getAllEntity(Session session) {
  143. List<Post> result = session.createQuery("from Post ").list();
  144. return result;
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement