Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Configuration
- @EnableWebMvc
- @ComponentScan ("ua")
- public class WebConfig {
- Logger logger = Logger.getLogger(WebConfig.class);
- @Bean
- public InternalResourceViewResolver viewResolver() {
- InternalResourceViewResolver resolver = new InternalResourceViewResolver();
- resolver.setPrefix("/WEB-INF/pages");
- resolver.setSuffix(".jsp");
- return resolver;
- }
- @Bean (name = "sessionFactory")
- public SessionFactory getSessionFactory() {
- org.hibernate.cfg.Configuration configuration = new org.hibernate.cfg.Configuration();
- SessionFactory sessionFactory = configuration.configure("resources/hibernate.cfg.xml").buildSessionFactory();
- logger.info("get Session Factory");
- return sessionFactory;
- }
- @Bean
- public ClientDaoImpl dao(){
- return new ClientDaoImpl();
- }
- @Bean
- public ClientServiceImpl service(){
- return new ClientServiceImpl();
- }
- }
- public interface ClientDao {
- public void addClient(Client client);
- public List<Client> listClient();
- public void removeClient (Client client);
- }
- @Repository
- public class ClientDaoImpl implements ClientDao {
- @Autowired
- @Qualifier(value = "sessionFactory")
- private SessionFactory sessionFactory;
- @Override
- public void addClient(Client client) {
- sessionFactory.getCurrentSession().save(client);
- sessionFactory.close();
- }
- @Override
- public List<Client> listClient() {
- List<Client> result = sessionFactory.getCurrentSession().createCriteria(Client.class).list();
- sessionFactory.close();
- return result;
- }
- @Override
- public void removeClient(Client client) {
- sessionFactory.getCurrentSession().delete(client);
- sessionFactory.close();
- }
- }
- public interface ClientService {
- public void addClient(Client client);
- public List<Client> listClient();
- public void removeClient(Client client);
- }
- @Service
- public class ClientServiceImpl implements ClientService {
- @Autowired
- private ClientDaoImpl clientDaoImpl;
- @Override
- @Transactional
- public void addClient(Client client) {
- clientDaoImpl.addClient(client);
- }
- @Override
- @Transactional
- public List<Client> listClient() {
- return clientDaoImpl.listClient();
- }
- @Override
- @Transactional
- public void removeClient(Client client) {
- clientDaoImpl.removeClient(client);
- }
- }
- @Controller
- public class MainController {
- @Qualifier("service")
- @Autowired
- private ClientServiceImpl clientService;
- @RequestMapping(value = "/", method = RequestMethod.GET)
- public ModelAndView mainPage (){
- ModelAndView modelAndView = new ModelAndView();
- List<Client> allClient = clientService.listClient();
- modelAndView.addObject("allClient", allClient);
- modelAndView.setViewName("index");
- return modelAndView;
- }
- }
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display-name>Blog Servlet</display-name>
- <servlet>
- <servlet-name>servletController</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextClass</param-name>
- <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
- </init-param>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>ua.config</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>servletController</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- </web-app>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://localhost:3306/postschema</property>
- <property name="connection.username">root</property>
- <property name="connection.password">peroser12</property>
- <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="show_sql">true</property>
- <property name="hbm2ddl.auto">update</property>
- <mapping class="ua.model.Client"/>
- </session-factory>
- </hibernate-configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement