一个继承hibernateDaoSupport的类 UserDaoImpl.java
如下:
public class UserDaoImpl extends HibernateDaoSupport implements UserDao { public void addUser(UserBean user) {
/* Session session=myUtilTools.getSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
myUtilTools.closeSession(session);*/
System.out.println(user.getName()); //能输出值。。
System.out.println(this.getHibernateTemplate());//为NULL。
this.getHibernateTemplate().save(user);
}applicationContext.xml 配置如下:
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 给用户服务类注入实现类 -->
<bean id="UserDaoImpl" class="com.chengge.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
Web.xml配置如下下:<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
(问题:为什么this.getHibernateTemplate()的值为NULL。。郁闷中)
如下:
public class UserDaoImpl extends HibernateDaoSupport implements UserDao { public void addUser(UserBean user) {
/* Session session=myUtilTools.getSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
myUtilTools.closeSession(session);*/
System.out.println(user.getName()); //能输出值。。
System.out.println(this.getHibernateTemplate());//为NULL。
this.getHibernateTemplate().save(user);
}applicationContext.xml 配置如下:
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 给用户服务类注入实现类 -->
<bean id="UserDaoImpl" class="com.chengge.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
Web.xml配置如下下:<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
(问题:为什么this.getHibernateTemplate()的值为NULL。。郁闷中)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货