第一步:搭建Spring环境
使用的是spring3.2版本,首先导入整合时需要的jar包:
spring-3.2-core 核心包
spring-3.2-aop aop包
spring-3.2-persistence 持久化包,与hibernate整合需要
spring-3.2-web web包,与Struts2整合需要
c3p0-0.9 c3p0数据包
编写配置文件applicatonContext-public.xml,名称和位置不固定,但位置一般在src下,名称以统一的形式开头,例如:bean-1.xml。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 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 "> <!-- 配置数据库连接信息 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///myshop?useUnicode=true&characterEncoding=UTF-8"></property> <property name="user" value="root"></property> <property name="password" value="mysql"></property> </bean> <!-- 使用Spring中实现了Hibernate的SessionFactory的实现类LocalSessionFactoryBean来初始化SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 依赖dataSource --> <property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate的配置文件地址,Spring在加载后会同时加载hibernate配置--> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <!-- 配置事务管理器,将Hibernate的Session通过Spring事务来管理--> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置事务增强,transaction-manager要与上面的id相同 --> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes> <!-- 设置事务操作方法的匹配规则 --> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <aop:config> <!-- 将事务增强与切入点合并,确定哪些方法需要加入事务管理,且aop:pointcut要放在aop:advisor之前,否则会抛异常 --> <aop:pointcut expression="execution(* cn.it.myshop.service.impl.*.*(..))" id="pointcut"></aop:pointcut> <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/> </aop:config> </beans>
第二步:搭建Hibernate环境
使用的是hibernate3.6版本,导入jar包:
hibernate-3.6
编写配置文件hibernate.cfg.xml,名称位置固定。
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <!-- 与Spring整合后可以将数据库连接信息删掉 <property name="connection.url"> jdbc:mysql://localhost:3306/myshop?useUnicode=true&characterEncoding=UTF-8 </property> <property name="connection.username">root</property> <property name="connection.password">mysql</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="myeclipse.connection.profile">MySql</property> --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <mapping resource="cn/it/myshop/model/Category.hbm.xml" /> </session-factory> </hibernate-configuration>
第三步:搭建Struts环境
使用的是struts-2.3版本,首先导入jar包:
struts-2.3
配置struts.xml配置文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="myshop" extends="struts-default"> <!-- 与Spring整合后,下面的语句中class需要修改Spring中配置的id值 --> <!-- <action name="category_*" class="cn.it.myshop.action.CategoryAction" method="{1}"> --> <action name="category_*" class="categoryAction" method="{1}"> <result name="index">/index.jsp</result> </action> </package> </struts>
第四步:配置项目的web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>myapp</display-name> <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> <!-- 监听器的启动优先级高于过滤器,Tomcat服务器启动时会加载spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<!-- 配置struts2的过滤器--> <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>*.action</url-pattern> </filter-mapping> </web-app>
第五步:编写测试类
启动Tomcat服务器,测试测试类。