SpringMVC我在DAOimp中的SessionFactory不起作用

时间:2017-01-24 23:56:50

标签: java spring hibernate spring-mvc

我的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>

和我的DaoImp

@Repository
@Transactional
public class UserDaoImp implements UserDao {

@Autowired
SessionFactory session;

public List<Users> list() {

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

这是我的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;
}

执行应用程序后,我有一个NullPointerException:

SEVERE [http-nio-8084-exec-97] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service()for servlet [spring-web]在上下文中,路径[/ Holaspringmvc]引发了异常[请求处理失败;具有根本原因的嵌套异常是java.lang.NullPointerException]   显示java.lang.NullPointerException 在com.tricas.dao.UserDaoImp.list(UserDaoImp.java:32) 请帮帮我。

2 个答案:

答案 0 :(得分:0)

更简单。只需声明SessionFactory bean

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

类似LocalSessionFactoryBean

btw:你定义了组件扫描吗?

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

答案 1 :(得分:0)

我发现错误,我必须在服务和控制器中定义的结果 @Autowired

这是我的服务。

@Autowired
UserDao usrdao;

//private UserDao usrdao = new UserDaoImp();

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

这是我的控制器

@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";
}

此外,我必须添加以指导我从这个答案:answer here