applicationContext.xml
<?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:c
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/s ... ing-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver </value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/htwebsite </value>
</property>
<property name="username">
<value>root </value>
</property>
<property name="password">
<value>root </value>
</property>
</bean>
<!-- hibernate3 session Bean-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"> <!-- 注入数据源 -->
<ref local="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>test/Logintable.hbm.xml </value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true </prop>
</props>
</property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!--Dao Configuration-->
<bean id="LogintableDao" class="test.LogintableDao">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
<property name="hibernateTemplate">
<ref local="hibernateTemplate"/>
</property>
</bean>
<bean name="adduser" class="action.AddUserAction">
</bean>
</beans>
web.config
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
test </display-name>
<servlet>
<servlet-name>log4j </servlet-name>
<servlet-class>com.apache.jakarta.log4j.Log4jInit </servlet-class>
<init-param>
<param-name>log4j </param-name>
<param-value>/WEB-INF/log4j.properties </param-value>
</init-param>
<load-on-startup>1 </load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>index.html </welcome-file>
<welcome-file>index.htm </welcome-file>
<welcome-file>index.jsp </welcome-file>
<welcome-file>default.html </welcome-file>
<welcome-file>default.htm </welcome-file>
<welcome-file>default.jsp </welcome-file>
</welcome-file-list>
<filter>
<filter-name>strut2 </filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher </filter-class>
</filter>
<filter-mapping>
<filter-name>strut2 </filter-name>
<url-pattern>/* </url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
</web-app>
调用的地方
package test;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class LogintableDao extends HibernateDaoSupport {
//插入数据
public void insertUser(Logintable user) {
HibernateTemplate template = this.getHibernateTemplate();
if(template==null)
{
System.out.print("null");
}
else
{
System.out.print("not null");
template.save(user);
}
}
}