1. 加入Spring
1) 加入jar包
2) 配置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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"
version="2.5">
<!-- 配置Spring配置文件的名称和位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 启动IOC容器的ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3) 加入Spring的配置文件
<?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:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
</beans>
2. 加入Hibernate
1) 同时建立持久化类,和其对应的.hbm.xml文件,生成对应的数据表
2) Spring整合hibernate
3) 步骤:
① 加入jar包
② 在类路径下加入hibernate.cfg.xml文件,在其中配置hibernate的基本属性
<hibernate-configuration>
<session-factory>
<!-- 配置hibernate的基本属性 -->
<!-- 方言 -->
<property name="hibernate.dialect"></property>
<!-- 是否显示及格式化SQL -->
<property name="hibernate.format_sql">true</property>
<property name="hibernate.show_sql">true</property>
<!-- 生成数据表的策略 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 二级缓存相关 -->
<!-- ... -->
</session-factory>
</hibernate-configuration>
③ 建立持久化类,和其对应的.hbm.xml文件
Department.hbm.xml:
<hibernate-mapping>
<class name="com.cf.ssh.entities.Department" table="department" schema="dbo" catalog="ssh">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native"></generator>
</id>
<property name="departmentName" type="java.lang.String">
<column name="DEPARTMENT_NAME" not-null="true" />
</property>
<!-- 映射单向1-n的关联关系 -->
<set name="employees" inverse="true">
<key>
<column name="DEPT_ID" not-null="true" />
</key>
<one-to-many class="com.cf.ssh.entities.Employee" />
</set>
</class>
</hibernate-mapping>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Employee.hbm.xml:
<hibernate-mapping>
<class name="com.cf.ssh.entities.Employee" table="employee" schema="dbo" catalog="ssh">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native"></generator>
</id>
<!-- 映射单向n-1的关联关系 -->
<many-to-one name="department" class="com.cf.ssh.entities.Department">
<column name="DEPT_ID" not-null="true" />
</many-to-one>
<property name="lastName" type="java.lang.String">
<column name="LAST_NAME" not-null="true" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" not-null="true" />
</property>
<property name="birth" type="java.lang.String">
<column name="BIRTH" not-null="true" />
</property>
<property name="createTime" type="java.sql.Timestamp">
<column name="CREATE_TIME" length="23" not-null="true" />
</property>
</class>
</hibernate-mapping>
④ 和Spring进行整合
i. 加入c3p0和sqlserver的驱动
ii. 创建db.properties
user=sa
password=123123
driverclass=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbcurl=jdbc\:sqlserver\://127.0.0.1\:1433;databaseName\=ssh
jdbc.initialPoolSize =5
jdbc.maxPoolSize =10
iii. 在Spring的配置文件中配置:数据源,SessionFactory,声明式事务
<!-- 导入资源文件db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="jdbcUrl" value="${jdbcurl}"></property>
<property name="driverClass" value="${driverclass}"></property>
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 配置数据源属性 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate配置文件的位置及名称 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 配置hibernate映射文件的位置及名称.可以使用通配符 -->
<property name="mappingLocations"
value="classpath:com/cf/ssh/entities/*.hbm.xml"></property>
</bean>
<!-- 配置Spring的声明式事务 -->
<!-- 1. 配置hibernate的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 2. 配置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 3. 配置事务切入点,再把事务属性和事务切入点关联起来 -->
<aop:config>
<aop:pointcut expression="execution(* com.cf.ssh.service.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
3. 加入Struts2
1) 加入jar包:若有重复的jar包,则需要删除版本较低的jar包。
2) 在web.xml文件中配置Struts2的Filter
<!-- 加入Struts2的Filter -->
<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>
3) 加入Struts2的配置文件
<?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>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
</package>
</struts>
4) 整合Spring
① 加入Struts2的Spring插件的jar包:struts2-spring-plugin-2.3.24.jar
② 在Spring的配置文件中正常配置Action,注意Action的scope为prototype
<bean id="employeeAction" class="com.cf.ssh.actions.EmployeeAction"
scope="prototype">
</bean>
③ 在Struts2的配置文件中配置Action时,class属性指向该Action在IOC中的id