S2SH的整合(Hibernate(JPA) + Struts2 + Spring)
---------------------------------
1.什么是S2SH的整合
Struts2+Spring+Hibernate
各个框架在S2SH中的作用:
Spring: 负责管理Struts2和Hibernate的生命周期。
Hibernate(JPA):主要对DAO层进行封装
Struts2:负责接收表单数据、调用业务层、负责页面的
转发。(WEB的控制器)
2.为什么要使用S2SH整合?
(1)提供一个中、大型项目中一个通用的MVC解决方案。
节约编程人员的时间,可以更多时间关注业务。
(2)S2SH框架降低了各层之间的依赖程度。
(3)S2SH框架使程序有更好的扩展性。
3.S2SH的使用范围?
主要应用于企业级的中、大型项目。
4.S2SH的整合实现:
(1)加入Hibernate、Spring、Struts2的依赖文件。
注意:
a.Hibernate3.6对应使用Spring3.0
b.Struts2与Spring整合一定要加入
struts2-spring-plugin-2.2.1.jar 插件包
(2)在SRC下新建META-INF目录,在此目录下
加入persistence.xml。JPA的配置文件
注意:
在文件中没有配置数据库的连接
(3)在SRC目录下加入applicationContext.xml和
jdbc.properties(数据库连接)文件
applicationContext.xml中配置
<!-- Spring注解要用到的类扫描器 -->
<context:annotation-config />
<context:component-scan base-package="com..kingqueen" />
<!-- Spring连接数据库的配置 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据源dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!-- 配置Spring管理JPA中的EntityManager对象 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="dataSource" ref="dataSource" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<!-- 配置Spring中的事物管理对象 -->
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<!-- 把事物 可以注解配置 -->
<tx:annotation-driven transaction-manager="txManager" />
总结:
Spring与JPA的整合实质:
(1)将数据库连接交给Spring管理
(2)将JPA的Entitymanager对象交给Spring管理
(3)将JPA的事务交给Spring管理
(4)将DAO层和业务层都交给Spring管理
(4)编写BaseDAO接口和实现类
@Repository
使用注解将DAO层注入到Spring容器中
使用注解控制事物
(5)编写业务层
@Service
使用注解将业务层注入到Spring容器中
(6)编写测试用例测试业务层
Spring整合Struts2部分:
(1)在web.xml文件中加入Struts2的过滤器
<!-- 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>/*</url-pattern>
</filter-mapping>
(2)在web.xml文件中配置Spring容器的监听器
<!-- Spring监听器启动Spring容器时要读取的配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!-- Spring容器的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
(3)在SRC下加入struts.xml文件(Struts2的Action的配置)
(4)编写Struts2的Action
a.将Action交给Spring管理
通过@Controller实现。
b.将Action的配置作用域配置成原型
@Scope(value="prototype")
c.配置业务层与Action的依赖关系@Resource
@Resource
ICustomerService cusService;
public void setCusService(ICustomerService cusService) {
this.cusService = cusService;
}
(5)编写Action中业务逻辑
(6)在struts-customer.xml文件中配置Action
(7)在struts.xml文件中加载对应的模块
<include file="com/kingqueen/customer/action/struts-customer.xml"></include>
实现模块的可拔插
---------------------------------
1.什么是S2SH的整合
Struts2+Spring+Hibernate
各个框架在S2SH中的作用:
Spring: 负责管理Struts2和Hibernate的生命周期。
Hibernate(JPA):主要对DAO层进行封装
Struts2:负责接收表单数据、调用业务层、负责页面的
转发。(WEB的控制器)
2.为什么要使用S2SH整合?
(1)提供一个中、大型项目中一个通用的MVC解决方案。
节约编程人员的时间,可以更多时间关注业务。
(2)S2SH框架降低了各层之间的依赖程度。
(3)S2SH框架使程序有更好的扩展性。
3.S2SH的使用范围?
主要应用于企业级的中、大型项目。
4.S2SH的整合实现:
(1)加入Hibernate、Spring、Struts2的依赖文件。
注意:
a.Hibernate3.6对应使用Spring3.0
b.Struts2与Spring整合一定要加入
struts2-spring-plugin-2.2.1.jar 插件包
(2)在SRC下新建META-INF目录,在此目录下
加入persistence.xml。JPA的配置文件
注意:
在文件中没有配置数据库的连接
(3)在SRC目录下加入applicationContext.xml和
jdbc.properties(数据库连接)文件
applicationContext.xml中配置
<!-- Spring注解要用到的类扫描器 -->
<context:annotation-config />
<context:component-scan base-package="com..kingqueen" />
<!-- Spring连接数据库的配置 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据源dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!-- 配置Spring管理JPA中的EntityManager对象 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="dataSource" ref="dataSource" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<!-- 配置Spring中的事物管理对象 -->
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<!-- 把事物 可以注解配置 -->
<tx:annotation-driven transaction-manager="txManager" />
总结:
Spring与JPA的整合实质:
(1)将数据库连接交给Spring管理
(2)将JPA的Entitymanager对象交给Spring管理
(3)将JPA的事务交给Spring管理
(4)将DAO层和业务层都交给Spring管理
(4)编写BaseDAO接口和实现类
@Repository
使用注解将DAO层注入到Spring容器中
使用注解控制事物
(5)编写业务层
@Service
使用注解将业务层注入到Spring容器中
(6)编写测试用例测试业务层
Spring整合Struts2部分:
(1)在web.xml文件中加入Struts2的过滤器
<!-- 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>/*</url-pattern>
</filter-mapping>
(2)在web.xml文件中配置Spring容器的监听器
<!-- Spring监听器启动Spring容器时要读取的配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!-- Spring容器的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
(3)在SRC下加入struts.xml文件(Struts2的Action的配置)
(4)编写Struts2的Action
a.将Action交给Spring管理
通过@Controller实现。
b.将Action的配置作用域配置成原型
@Scope(value="prototype")
c.配置业务层与Action的依赖关系@Resource
@Resource
ICustomerService cusService;
public void setCusService(ICustomerService cusService) {
this.cusService = cusService;
}
(5)编写Action中业务逻辑
(6)在struts-customer.xml文件中配置Action
(7)在struts.xml文件中加载对应的模块
<include file="com/kingqueen/customer/action/struts-customer.xml"></include>
实现模块的可拔插