-1

sessionFactorynull在我的 dao 文件中,我在 spring 中配置了 sessionFactoryxml file

应用上下文文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/tx
 http://www.springframework.org/schema/tx/spring-tx.xsd">

 <context:annotation-config />
<context:component-scan base-package="com.tsc.erp" />
 <tx:annotation-driven transaction-manager="transactionManager" />

 <bean id="transactionManager"
 class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />

 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
 <property name="configLocation" value="classpath:hibernate.cfg.xml" />
 <property name="packagesToScan" value="com.tsc.erp.dao"/>
 <property name="dataSource" ref="dataSource" />
 <property name="hibernateProperties">
 <props>
 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
 <prop key="hibernate.show_sql">false</prop>

 </props>
 </property>
 </bean>

    <!--  <bean id="datasourcePropertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:datasource.properties</value>
        </property>
        <property name="ignoreUnresolvablePlaceholders">
            <value>true</value>
        </property>
    </bean> -->

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



<bean id="userdao" class="com.tsc.erp.dao.TscUserHome"  >
<property name="sessionFactory"><ref bean="sessionFactory"/></property>

</bean>


</beans>

DAO 文件

package com.tsc.erp.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Repository;

@Repository
public class TscUserHome implements Tscuserdao{

    @Autowired
    private SessionFactory sessionFactory;



    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {

            this.sessionFactory = sessionFactory;  
    }





    public List showEmployee() {
          System.out.println(sessionFactory);
          List emp = null;
          Session session = sessionFactory.openSession();
        Transaction tx = null;
          try{
             tx = session.beginTransaction();

        emp = session.createQuery("from tsc_user").list();

          tx.commit();
          }

          catch (HibernateException e) {
             if (tx!=null) 
                 {tx.rollback();
             e.printStackTrace(); 
          }

          }
          catch(NullPointerException ne)
          {

              ne.getStackTrace();
          }

          finally {

          }
          session.close(); 
            sessionFactory.close(); 
        return emp;


      }




}

web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>esystem</display-name>


  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

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


  <welcome-file-list>

    <welcome-file>loginpage.jsp</welcome-file>
  </welcome-file-list>
</web-app>

服务文件(我在哪里使用我的 TscUserHome)

包 com.tsc.erp.services;

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

import com.tsc.erp.dao.TscUserHome;

public class Loginservice
{
     private  List parti_user = new ArrayList();
    private TscUserHome logindao=new TscUserHome();

    public  void authentication()
    {
        try{
            System.out.println("first");
        parti_user =logindao.showEmployee();

        System.out.println(parti_user);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }




}
4

2 回答 2

0

您必须在您的 dao bean 中注入 sessionFactory 对象。例如

<bean id="MyDAO" class="com.mine.MyDAO">
<property name="sessionFactory" ref="id of your sessionFactory bean" />
</bean>
于 2013-10-03T11:45:59.147 回答
0

首先检查您是否在DAO中制作了sessionFactory的getter方法,否则请分享您创建sessionFactory和DAO的xml。

于 2013-10-03T12:00:23.877 回答