1、导包
struts-2.3.15
asm-3.3.jar、asm-commons-3.3.jar、asm-tree-3.3.jar、commons-fileupload-1.3.jar、commons-io-2.0.1.jar、commons-lang3-3.1.jar、commons-logging-1.1.3.jar、freemarker-2.3.19.jar
javassist-3.11.0.GA.jar、log4j-1.2.17.jar、ognl-3.0.6.jar、struts2-core-2.3.15.jar、xwork-core-2.3.15.jar
struts与spring整合包:struts2-spring-plugin-2.3.15.jar
hibernate-distribution-3.6.10.Final
hibernate3.jar
lib\required\*
lib\jpa\*
spring-framework-3.2.0.RELEASE
IOC:spring-beans-3.2.0.RELEASE.jar、spring-context-3.2.0.RELEASE.jar、spring-core-3.2.0.RELEASE.jar、spring-expression-3.2.0.RELEASE.jar
AOP:spring-aop-3.2.0.RELEASE.jar、spring-aspects-3.2.0.RELEASE.jar、com.springsource.org.aopalliance-1.0.0.jar、com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
整合包:spring-jdbc-3.2.0.RELEASE.jar、spring-orm-3.2.0.RELEASE.jar、spring-test-3.2.0.RELEASE.jar、spring-tx-3.2.0.RELEASE.jar、spring-web-3.2.0.RELEASE.jar
日志包:com.springsource.org.apache.commons.logging-1.1.1.jar、com.springsource.org.apache.log4j-1.2.15.jar、slf4j-log4j12-1.7.12.jar
数据库包:c3p0-0.9.1.jar、mysql-connector-java-5.1.38-bin.jar
因为我使用的是c3p0连接池,所以后面配置的数据源就是c3p0的。
2、配置文件
web.xml
applicationContext.xml
struts.xml
我的Action是交给spring来创建的,所以我的class写的是productAction。当然Action也可以不交给spring来管理,就像普通的struts.xml配置就好了,applicationContext.xml中也就不用配置
Action了。
3、acton、service、dao代码以及pojo中的hbm.xml
dao层继承了HibernateDaoSupport这个类就可以直接使用this.getHibernateTemplate()来操作,并不用再把HibernateTemplate注入进来。
这是HibernateTemplate中的方法,这也是我为什么要把sessionFactory注入到dao层。
public final void setSessionFactory(SessionFactory sessionFactory) {
if (this.hibernateTemplate == null || sessionFactory != this.hibernateTemplate.getSessionFactory()) {
this.hibernateTemplate = createHibernateTemplate(sessionFactory);
}
}
struts-2.3.15
asm-3.3.jar、asm-commons-3.3.jar、asm-tree-3.3.jar、commons-fileupload-1.3.jar、commons-io-2.0.1.jar、commons-lang3-3.1.jar、commons-logging-1.1.3.jar、freemarker-2.3.19.jar
javassist-3.11.0.GA.jar、log4j-1.2.17.jar、ognl-3.0.6.jar、struts2-core-2.3.15.jar、xwork-core-2.3.15.jar
struts与spring整合包:struts2-spring-plugin-2.3.15.jar
hibernate-distribution-3.6.10.Final
hibernate3.jar
lib\required\*
lib\jpa\*
spring-framework-3.2.0.RELEASE
IOC:spring-beans-3.2.0.RELEASE.jar、spring-context-3.2.0.RELEASE.jar、spring-core-3.2.0.RELEASE.jar、spring-expression-3.2.0.RELEASE.jar
AOP:spring-aop-3.2.0.RELEASE.jar、spring-aspects-3.2.0.RELEASE.jar、com.springsource.org.aopalliance-1.0.0.jar、com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
整合包:spring-jdbc-3.2.0.RELEASE.jar、spring-orm-3.2.0.RELEASE.jar、spring-test-3.2.0.RELEASE.jar、spring-tx-3.2.0.RELEASE.jar、spring-web-3.2.0.RELEASE.jar
日志包:com.springsource.org.apache.commons.logging-1.1.1.jar、com.springsource.org.apache.log4j-1.2.15.jar、slf4j-log4j12-1.7.12.jar
数据库包:c3p0-0.9.1.jar、mysql-connector-java-5.1.38-bin.jar
因为我使用的是c3p0连接池,所以后面配置的数据源就是c3p0的。
2、配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- spring的核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置struts核心过滤器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 加载数据库配置 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 链接池的配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置hibernate相关属性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- hibernate映射文件 -->
<property name="mappingResources">
<list>
<value>com/imooc_ssh/pojo/Product.hbm.xml</value>
</list>
</property>
</bean>
<!-- dao层的配置 -->
<bean id="productDao" class="com.imooc_ssh.dao.ProductDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- service层的配置 -->
<bean id="productService" class="com.imooc_ssh.service.ProductService">
<property name="productDao" ref="productDao">
</property>
</bean>
<!-- action层的配置 -->
<bean id="productAction" class="com.imooc_ssh.action.ProductAction" scope="prototype">
<!-- 手动注入Service -->
<property name="productService" ref="productService">
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="ssh" namespace="/" extends="struts-default">
<action name="product_*" class="productAction" method="{1}">
</action>
</package>
</struts>
我的Action是交给spring来创建的,所以我的class写的是productAction。当然Action也可以不交给spring来管理,就像普通的struts.xml配置就好了,applicationContext.xml中也就不用配置
Action了。
3、acton、service、dao代码以及pojo中的hbm.xml
package com.imooc_ssh.action;
import com.imooc_ssh.pojo.Product;
import com.imooc_ssh.service.ProductService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class ProductAction extends ActionSupport implements ModelDriven<Product>{
private static final long serialVersionUID = 1L;
private Product product=new Product();
private ProductService productService;
@Override
public Product getModel() {
// TODO Auto-generated method stub
return product;
}
public void setProductService(ProductService productService) {
this.productService = productService;
}
public String save(){
System.out.println("action开始执行save方法..");
productService.save(product);
return NONE;
}
}
package com.imooc_ssh.service;
import org.springframework.transaction.annotation.Transactional;
import com.imooc_ssh.dao.ProductDao;
import com.imooc_ssh.pojo.Product;
@Transactional
public class ProductService {
private ProductDao productDao;
public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}
public void save(Product product) {
System.out.println("service中的save方法开始执行..");
productDao.save(product);
}
}
package com.imooc_ssh.dao;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.imooc_ssh.pojo.Product;
public class ProductDao extends HibernateDaoSupport{
public void save(Product product) {
System.out.println("dao中save方法开支执行..");
this.getHibernateTemplate().save(product);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.imooc_ssh.pojo.Product" table="t_product">
<id name="pid" column="id">
<generator class="native"></generator>
</id>
<property name="pname" column="name" length="8" type="java.lang.String"></property>
<property name="price" column="price"></property>
</class>
</hibernate-mapping>
dao层继承了HibernateDaoSupport这个类就可以直接使用this.getHibernateTemplate()来操作,并不用再把HibernateTemplate注入进来。
这是HibernateTemplate中的方法,这也是我为什么要把sessionFactory注入到dao层。
public final void setSessionFactory(SessionFactory sessionFactory) {
if (this.hibernateTemplate == null || sessionFactory != this.hibernateTemplate.getSessionFactory()) {
this.hibernateTemplate = createHibernateTemplate(sessionFactory);
}
}