Advertisement
Guest User

Untitled

a guest
Mar 12th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. @Configuration
  2. @EnableWebMvc
  3. @ComponentScan ("ua")
  4. public class WebConfig {
  5.  
  6. Logger logger = Logger.getLogger(WebConfig.class);
  7.  
  8. @Bean
  9. public InternalResourceViewResolver viewResolver() {
  10. InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  11. resolver.setPrefix("/WEB-INF/pages");
  12. resolver.setSuffix(".jsp");
  13. return resolver;
  14. }
  15.  
  16. @Bean (name = "sessionFactory")
  17. public SessionFactory getSessionFactory() {
  18. org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration();
  19. SessionFactory sessionFactory = configuration.configure("resources/hibernate.cfg.xml").buildSessionFactory();
  20. logger.info("get Session Factory");
  21. return sessionFactory;
  22.  
  23. }
  24.  
  25. @Bean
  26. public ClientDaoImpl dao(){
  27. return new ClientDaoImpl();
  28. }
  29.  
  30. @Bean
  31. public ClientServiceImpl service(){
  32. return new ClientServiceImpl();
  33. }
  34. }
  35.  
  36. public interface ClientDao {
  37.  
  38. public void addClient(Client client);
  39.  
  40. public List<Client> listClient();
  41.  
  42. public void removeClient (Client client);
  43.  
  44. }
  45.  
  46.  
  47. @Repository
  48. public class ClientDaoImpl implements ClientDao {
  49.  
  50. @Autowired
  51. @Qualifier(value = "sessionFactory")
  52. private SessionFactory sessionFactory;
  53.  
  54. @Override
  55. public void addClient(Client client) {
  56. sessionFactory.getCurrentSession().save(client);
  57. sessionFactory.close();
  58. }
  59.  
  60. @Override
  61. public List<Client> listClient() {
  62. List<Client> result = sessionFactory.getCurrentSession().createCriteria(Client.class).list();
  63. sessionFactory.close();
  64. return result;
  65.  
  66. }
  67.  
  68. @Override
  69. public void removeClient(Client client) {
  70. sessionFactory.getCurrentSession().delete(client);
  71. sessionFactory.close();
  72.  
  73. }
  74. }
  75.  
  76. public interface ClientService {
  77.  
  78. public void addClient(Client client);
  79.  
  80. public List<Client> listClient();
  81.  
  82. public void removeClient(Client client);
  83. }
  84.  
  85.  
  86.  
  87. @Service
  88. public class ClientServiceImpl implements ClientService {
  89.  
  90. @Autowired
  91. private ClientDaoImpl clientDaoImpl;
  92.  
  93. @Override
  94. @Transactional
  95. public void addClient(Client client) {
  96. clientDaoImpl.addClient(client);
  97. }
  98.  
  99. @Override
  100. @Transactional
  101. public List<Client> listClient() {
  102. return clientDaoImpl.listClient();
  103. }
  104.  
  105. @Override
  106. @Transactional
  107. public void removeClient(Client client) {
  108. clientDaoImpl.removeClient(client);
  109. }
  110. }
  111.  
  112. @Controller
  113. public class MainController {
  114.  
  115. @Qualifier("service")
  116. @Autowired
  117. private ClientServiceImpl clientService;
  118. @RequestMapping(value = "/", method = RequestMethod.GET)
  119. public ModelAndView mainPage (){
  120. ModelAndView modelAndView = new ModelAndView();
  121. List<Client> allClient = clientService.listClient();
  122. modelAndView.addObject("allClient", allClient);
  123. modelAndView.setViewName("index");
  124. return modelAndView;
  125. }
  126.  
  127.  
  128. }
  129.  
  130. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  131. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  132. <display-name>Blog Servlet</display-name>
  133.  
  134. <servlet>
  135. <servlet-name>servletController</servlet-name>
  136. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  137. <init-param>
  138. <param-name>contextClass</param-name>
  139. <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
  140. </init-param>
  141. <init-param>
  142. <param-name>contextConfigLocation</param-name>
  143. <param-value>ua.config</param-value>
  144. </init-param>
  145. </servlet>
  146.  
  147.  
  148. <servlet-mapping>
  149. <servlet-name>servletController</servlet-name>
  150. <url-pattern>/</url-pattern>
  151. </servlet-mapping>
  152. </web-app>
  153.  
  154. <!DOCTYPE hibernate-configuration PUBLIC
  155. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  156. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  157. <hibernate-configuration>
  158. <session-factory>
  159. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  160. <property name="connection.url">jdbc:mysql://localhost:3306/postschema</property>
  161. <property name="connection.username">root</property>
  162. <property name="connection.password">peroser12</property>
  163. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  164. <property name="show_sql">true</property>
  165. <property name="hbm2ddl.auto">update</property>
  166.  
  167. <mapping class="ua.model.Client"/>
  168. </session-factory>
  169.  
  170. </hibernate-configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement