简体   繁体   English

SpringMVC我在DAOimp中的SessionFactory无法正常工作

[英]SpringMVC my SessionFactory in DAOimp doesn't work

I have this in my servlet: 我的servlet中有这个:

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

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/test" />
    <property name="username" value="root" />
    <property name="password" value="12345" />
</bean>

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.tricas.models" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            <prop key="hibernate.default_schema">test</prop>
            <prop key="format_sql">true</prop>
            <prop key="use_sql_comments">true</prop>
            <!--<prop key="hibernate.hbm2ddl.auto">create</prop> -->
        </props>
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

and my DaoImp 和我的DaoImp

@Repository
@Transactional
public class UserDaoImp implements UserDao {

@Autowired
SessionFactory session;

public List<Users> list() {

    return session.getCurrentSession().createQuery("from Users").list();
}

here is my HibernateUtil 这是我的HibernateUtil

private static final SessionFactory sessionFactory;

static {
    try {
        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
        sessionFactory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

And After executing the application I have a NullPointerException: 在执行应用程序后,我有一个NullPointerException:

SEVERE [http-nio-8084-exec-97] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service () for servlet [spring-web] in context with path [/ Holaspringmvc] threw exception [Request processing failed; 在路径为[/ Holaspringmvc]的上下文中,严重程度[http-nio-8084-exec-97] org.apache.catalina.core.StandardWrapperValve.invoke Servlet [spring-web]的Servlet.service()抛出异常[请求处理失败; Nested exception is java.lang.NullPointerException] with root cause Java.lang.NullPointerException At com.tricas.dao.UserDaoImp.list (UserDaoImp.java:32) Please help me. 嵌套异常是java.lang.NullPointerException],其根本原因是Java.lang.NullPointerException在com.tricas.dao.UserDaoImp.list(UserDaoImp.java:32)上,请帮帮我。

Be simpler. 简单一点。 Just declare SessionFactory bean 只需声明SessionFactory bean

@Bean
public AbstractSessionFactoryBean sessionFactoryBean(){
    AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean();
    sessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
    return sessionFactoryBean;
}

similar for LocalSessionFactoryBean LocalSessionFactoryBean类似

btw: did you define component-scan ? 顺便说一句:您定义了component-scan吗?

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

I found the error, results that I had to define in the service and in the controller also with @Autowired 我发现了错误,也必须使用@Autowired在服务和控制器中定义结果

Here is my Service. 这是我的服务。

@Autowired
UserDao usrdao;

//private UserDao usrdao = new UserDaoImp();

@Transactional
public List<Users> getAllUsers() {        
    return usrdao.list();
}

and here is my controller 这是我的控制器

@Autowired
UserService usrv;

//private UserService usrv = new UserService();

@RequestMapping(value = "/verusuarios", method = RequestMethod.GET)
public String listPersons(Model model) {
    List<Users> list = usrv.getAllUsers();
    model.addAttribute("user", new Users());
    model.addAttribute("list", list);
    return "verusuarios";
}

Additionally I must add to guide me from this answer: answer here 此外,我还必须从这个答案中引导我: 在这里回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM