非法参数会话工厂requiredjava.lang.IllegalArgumentException:'sessionFactory'或'hibernateTemplate'是必需的hibernate5

时间:2018-06-14 06:18:25

标签: hibernate spring-mvc

以下是我的项目结构: enter image description here

我有这些文件:

Web.xml的内容如下:

<display-name>Java EE Starter Application</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/spring-mvc-context.xml

    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>springHibernateDemo2</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springHibernateDemo2</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

spring-mvc-context.xml的内容如下:

<context:component-scan base-package="com.abhinav" />

<mvc:annotation-driven/>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<mvc:resources mapping="/static/**" location="/" />

<mvc:default-servlet-handler />

db-config.xml具有以下内容:

<context:property-placeholder location="classpath:database.properties"/> 
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
    <property name="driverClassName" value="${database.driverClassName}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>            
      </props>
        </property>
            <property name="packagesToScan" value="com.abhinav.data"></property> 
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
    <constructor-arg name="sessionFactory" ref="sessionFactory"/>  
</bean> 

<bean class="com.abhinav.data.UserDao2" />
<tx:annotation-driven transaction-manager="transactionManager" />       

BaseDao.java

public class BaseDao extends HibernateDaoSupport {

    @Autowired
    SessionFactory sessionFactory;

    @SuppressWarnings("unchecked")
    public List getAll(Class clazz) {
        return getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(clazz));
    }
}


@Repository("userDao2")
public class UserDao2 extends BaseDao {

    @SuppressWarnings("unchecked")
    public List<User> getAllUsers() {
        String sqlQuery = "select id, first_name, email from users";
        SQLQuery query = currentSession().createSQLQuery(sqlQuery);
        query.addEntity(User.class);

        return (List<User>)query.list();
    }
}

UserController.java

@Controller
public class UserController {

    @Autowired
    private UserDao2 userDao2 =  new UserDao2();

    @RequestMapping(value="/users", method = RequestMethod.GET)

    public String displayUsers (Model model) {
        model.addAttribute("users",userDao2.getAllUsers());
        return "users";
    }   

}

我收到“sessionFactory”所需的错误。无法弄清楚我哪里出错了。几天来我一直坚持不懈。

0 个答案:

没有答案