使用Spring Web应用程序配置Hibernate

时间:2018-06-18 14:20:04

标签: hibernate

  

当我整合我的春天时,伙计们会遇到以下错误   应用程序使用hibernate

     

最重要的是我的应用程序代码可能会出错   包括       控制器,dao接口,dao implimentation,hibernate configutation和所有其他请记录我的代码和suugesst   在开发这种类型的时候,我越来越被困了   的一个应用

    Jun 18, 2018 7:07:02 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
    WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Helloworld' did not find a matching property.
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server version:        Apache Tomcat/7.0.88
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server built:          May 7 2018 11:12:57 UTC
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server number:         7.0.88.0
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: OS Name:               Windows 8.1
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: OS Version:            6.3
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Architecture:          amd64
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Java Home:             C:\Program Files\Java\jre1.8.0_171
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: JVM Version:           1.8.0_171-b11
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: JVM Vendor:            Oracle Corporation
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: CATALINA_BASE:         D:\Eclipe kepler workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: CATALINA_HOME:         C:\apache-tomcat-7.0.88-windows-x64\apache-tomcat-7.0.88
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dcatalina.base=D:\Eclipe kepler workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dcatalina.home=C:\apache-tomcat-7.0.88-windows-x64\apache-tomcat-7.0.88
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dwtp.deploy=D:\Eclipe kepler workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Djava.endorsed.dirs=C:\apache-tomcat-7.0.88-windows-x64\apache-tomcat-7.0.88\endorsed
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dfile.encoding=Cp1252
    Jun 18, 2018 7:07:02 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
    INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_171\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Inno Setup 5;C:\Program Files (x86)\WiX Toolset v3.10\bin;;.
    Jun 18, 2018 7:07:02 PM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["http-bio-8080"]
    Jun 18, 2018 7:07:02 PM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
    Jun 18, 2018 7:07:02 PM org.apache.catalina.startup.Catalina load
    INFO: Initialization processed in 2060 ms
    Jun 18, 2018 7:07:03 PM org.apache.catalina.core.StandardService startInternal
    INFO: Starting service Catalina
    Jun 18, 2018 7:07:03 PM org.apache.catalina.core.StandardEngine startInternal
    INFO: Starting Servlet Engine: Apache Tomcat/7.0.88
    Jun 18, 2018 7:07:04 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
    WARNING: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [266] milliseconds.
    Jun 18, 2018 7:07:07 PM org.apache.catalina.startup.TldConfig execute
    INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    Jun 18, 2018 7:07:07 PM org.apache.catalina.core.ApplicationContext log
    INFO: No Spring WebApplicationInitializer types detected on classpath
    Jun 18, 2018 7:07:07 PM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring root WebApplicationContext
    Jun 18, 2018 7:07:07 PM org.springframework.web.context.ContextLoader initWebApplicationContext
    INFO: Root WebApplicationContext: initialization started
    Jun 18, 2018 7:07:07 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
    INFO: Refreshing Root WebApplicationContext: startup date [Mon Jun 18 19:07:07 IST 2018]; root of context hierarchy
    Jun 18, 2018 7:07:07 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
    Jun 18, 2018 7:07:08 PM org.springframework.web.context.support.XmlWebApplicationContext refresh
    WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloWorldController': Unsatisfied dependency expressed through field 'categoryDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CategoryDao': Unsatisfied dependency expressed through field 'sessionfactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    Jun 18, 2018 7:07:08 PM org.springframework.web.context.ContextLoader initWebApplicationContext
    SEVERE: Context initialization failed
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloWorldController': Unsatisfied dependency expressed through field 'categoryDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CategoryDao': Unsatisfied dependency expressed through field 'sessionfactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5157)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5680)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1702)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1692)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CategoryDao': Unsatisfied dependency expressed through field 'sessionfactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
        ... 24 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
        ... 37 more

    Jun 18, 2018 7:07:08 PM org.apache.catalina.core.StandardContext listenerStart
    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloWorldController': Unsatisfied dependency expressed through field 'categoryDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CategoryDao': Unsatisfied dependency expressed through field 'sessionfactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5157)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5680)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1702)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1692)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CategoryDao': Unsatisfied dependency expressed through field 'sessionfactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
        ... 24 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
        ... 37 more

    Jun 18, 2018 7:07:08 PM org.apache.catalina.core.StandardContext startInternal
    SEVERE: One or more listeners failed to start. Full details will be found in the appropriate container log file
    Jun 18, 2018 7:07:09 PM org.apache.catalina.core.StandardContext startInternal
    SEVERE: Context [/HelloWorld] startup failed due to previous errors
    Jun 18, 2018 7:07:09 PM org.apache.catalina.core.ApplicationContext log
    INFO: Closing Spring root WebApplicationContext
    Jun 18, 2018 7:07:09 PM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["http-bio-8080"]
    Jun 18, 2018 7:07:09 PM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["ajp-bio-8009"]
    Jun 18, 2018 7:07:09 PM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 6172 ms



Below is my all relavent classes 

This is my hibernate configuration file 

    package com.programcreek.helloworld.config;

    import java.util.Properties;

    import javax.sql.DataSource;

    import org.apache.commons.dbcp2.BasicDataSource;
    import org.hibernate.SessionFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.orm.hibernate5.HibernateTransactionManager;
    import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
    import org.springframework.transaction.annotation.EnableTransactionManagement;

    @Configuration
    @ComponentScan(basePackages = { "com.programcreek.helloworld.dto" })
    @EnableTransactionManagement
    public class HibernateConfig {

        private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/crmdb";
        private final static String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
        private final static String DATABASE_DIALECT = "org.hibernate.dialect.MySQLDialect";
        private final static String DATABASE_USERNAME = "root";
        private final static String DATABASE_PASSWORD = "root";

        @Bean
        public DataSource getDataSource() {

            BasicDataSource datasource = new BasicDataSource();
            datasource.setDriverClassName(DATABASE_DRIVER);
            datasource.setUrl(DATABASE_URL);
            datasource.setUsername(DATABASE_USERNAME);
            datasource.setPassword(DATABASE_PASSWORD);

            return datasource;
        }

        // sessionfactory will be avaible

        @Bean
        public SessionFactory getSessionFactory(DataSource datasource) {

            LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(
                    datasource);

            builder.addProperties(getHibernateProperties());
            builder.scanPackages("com.programcreek.helloworld.dto");

            return builder.buildSessionFactory();
        }

        private Properties getHibernateProperties() {
            // TODO Auto-generated method stub

            Properties properties = new Properties();
            properties.put("hibernate_dialect", DATABASE_DIALECT);
            properties.put("hibernate.show_sql", "true");
            properties.put("hibernate.format_sql", "true");
            return properties;
        }



        @Bean
        public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory){

            HibernateTransactionManager hibernateTransactionManager=new HibernateTransactionManager(sessionFactory);
            return hibernateTransactionManager;


        }
    }





> following is the controller

    package com.programcreek.helloworld.controller;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;

    import com.programcreek.helloworld.dao.CategoryDao;
    import com.programcreek.helloworld.dto.Category;

    @Controller
    public class HelloWorldController {

        @Autowired
        private CategoryDao categoryDao;

        // String message = "Welcome to Spring MVC!";

        @RequestMapping(value = { "/home", "/" })
        public ModelAndView index() {
            // for laoding homepage
            ModelAndView mv = new ModelAndView("helloworld");
            mv.addObject("title", "Home");

            // passing the list of categories
            mv.addObject("categories", categoryDao.list());

            mv.addObject("UserClickHome", true);
            return mv;
        }

        @RequestMapping(value = { "/about" })
        public ModelAndView about() {
            // for laoding homepage
            ModelAndView mv = new ModelAndView("helloworld");
            mv.addObject("title", "About Us");
            mv.addObject("UserClickAbout", true);
            return mv;
        }

        @RequestMapping(value = { "/contact" })
        public ModelAndView contact() {
            // for laoding homepage
            ModelAndView mv = new ModelAndView("helloworld");
            mv.addObject("title", "Contact Us");
            mv.addObject("UserClickContact", true);
            return mv;
        }

        /*
         * Methods to laod all products and based on category
         */

        @RequestMapping(value = { "/show/all/products" })
        public ModelAndView showAllProducts() {
            // for laoding homepage
            ModelAndView mv = new ModelAndView("helloworld");
            mv.addObject("title", "All Products");

            // passing the list of categories
            mv.addObject("categories", categoryDao.list());
            mv.addObject("UserClickAllproducts", true);
            return mv;
        }

        @RequestMapping(value = { "/show/category/{id}/products" })
        public ModelAndView showCategoryProducts(@PathVariable("id") int id) {

            // for laoding product by id
            ModelAndView mv = new ModelAndView("helloworld");

            Category category = null;

            category = categoryDao.get(id);
            mv.addObject("title", category.getName());

            // passing the list of categories
            mv.addObject("categories", categoryDao.list());

            // passing the single category object
            mv.addObject("category", category);

            mv.addObject("UserClickCategoryProducts", true);
            return mv;
        }

    }




> 
> 
> Then this is implementation for it

    package com.programcreek.helloworld.daoimpl;

    import java.util.ArrayList;
    import java.util.List;

    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Transactional;

    import com.programcreek.helloworld.dao.CategoryDao;
    import com.programcreek.helloworld.dto.Category;

    @Repository("CategoryDao")
    @Transactional
    public class CategoryDaoImpl implements CategoryDao {

        @Autowired
        private SessionFactory sessionfactory;

            public List<Category> list() {
            // TODO Auto-generated method stub
            return null;
        }


    }

0 个答案:

没有答案