Spring+hibernate+Struts2整合
1、配置要求
1. 加入 Spring
1). 加入 jar 包
2). 配置 web.xml 文件:启动SpringIOC的listener
3). 加入 Spring 的配置文件.
2. 加入 Hibernate
1). 同时建立持久化类, 和其对应的 .hbm.xml 文件, 生成对应的数据表
2). Spring 整合 Hibernate
3). 步骤:
①. 加入 jar 包
②. 在类路径下加入 hibernate.cfg.xml 文件, 在其中配置 hibernate 的基本属性
③. 建立持久化类, 和其对应的 .hbm.xml 文件
④. 和 Spring 进行整合
i. 加入 c3p0 和 MySQL 的驱动
ii. 在 Spring 的配置文件中配置: 数据源, SessionFactory, 声明式事务
⑤. 启动项目, 会看到生成对应的数据表
3. 加入 Struts2
1). 加入 jar 包: 若有重复的 jar 包, 则需要删除版本较低的. javassist-3.11.0.GA.jar
2). 在 web.xml 文件中配置 Struts2 的 Filter
3). 加入 Struts2 的配置文件
4). 整合 Spring
①. 加入 Struts2 的 Spring 插件的 jar 包
②. 在 Spring 的配置文件中正常配置 Action, 注意 Action 的 scope 为 prototype
③. 在 Struts2 的配置文件中配置 Action 时, class 属性指向该 Action 在 IOC 中的 id
4. 完成功能.
1). 获取所有的员工信息: 若在 Dao 中只查询 Employee 的信息, 而且 Employee 和 Department 还是使用的懒加载. 页面上还需要显示
员工信息, 此时会出现懒加载异常, 代理对象不能被初始化: 2.org.hibernate.LazyInitializationException: could not initialize proxy - no Session
解决:
①. 打开懒加载: 不推荐使用
②. 获取 Employee 时使用 迫切左外连接同时初始化其关联的 Department 对象.
③. 使用 OpenSessionInViewFilter: 后面再提.
2). 删除员工信息:
①. 正常删除, 返回值需要是 redirect 类型, 而且重定向到 emp-list
②. 确定要删除吗? 的提示使用 jQuery 完成
③. Ajax 的使用参见 struts-2.3.15.3-all/struts-2.3.15.3/docs/WW/docs/ajax.html
3). 添加员工:
①. 显示表单页面: 需要先查询所有的部门信息
②. 使用 Struts2 的 ModelDriven 和 Preparable 拦截器
③. 时间是一个字符串, 需要转为一个 Date 类型的对象
1、配置Spring
1、IOC容器的创建
-
创建IOC容器对象
-
将IOC容器的对象放入到Application中就是ServletContext
<?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"> <display-name>SHH-2</display-name> <!-- 创建IOC容器对应的对象,放入Application 对应的域中--> <context-param> <param-name>contextConfigLocation</param-name> <!-- 配置文件的位置 --> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- Bootstraps the root web application context before servlet initialization --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
2、Spring整合hibernate的配置
- 加入jar包
- hibernate.cfg.xml的基本属性的配置
- 建立持久化类和其对应的.hbm.xml 文件
- 和Spring进行整合
- 启动项目会生成对应的数据表
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">
<hibernate-configuration>
<session-factory>
<!-- 配置 hibernate 的基本属性 -->
<!-- 1. 数据源需配置到 IOC 容器中, 所以在此处不再需要配置数据源 -->
<!-- 2. 关联的 .hbm.xml 也在 IOC 容器配置 SessionFactory 实例时在进行配置 -->
<!-- 3. 配置 hibernate 的基本属性: 方言, SQL 显示及格式化, 生成数据表的策略以及二级缓存等. -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
Spring中配置Hibernate 的事物和SessionFactory
<?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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置 C3P0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></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>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/wu/ssh/beans/*.hbm.xml"></property>
</bean>
<!-- 配置 Spring 的声明式事务 -->
<!-- 1. 配置 hibernate 的事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<!--在Spring配置事物管理的配置的是数据源dataSource
<property name="dataSource" ref="dataSource"></property>
在hibernate中配置了sessionFactory可以直接引用
-->
<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="lastNameIsValid" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 3. 配置事务切入点, 再把事务属性和事务切入点关联起来 -->
<aop:config>
<aop:pointcut expression="execution(* com.wu.ssh.service.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
Spring配置Struts2
- 加入jar包
- 在web.xml 文件中配置Strust2 的Filter
- 加入Struts2的配置文件
- 整合Spring
- 导入插件的加入包Spring可以直接使用Struts2 class的jar包
- 在Spring配置文件的action,action的scope为prototype
- 在Struts2的配置文件中配置Action时,class属性指向该Action在 IOC中的id
配置web.xml
<!-- 配置 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>
在Spring的applicationContext.xml文件中配置bean,可以直接去引用bean的ID
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="employeeDao" class="com.atguigu.ssh.dao.EmployeeDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="departmentDao" class="com.atguigu.ssh.dao.DepartmentDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="employeeService" class="com.atguigu.ssh.service.EmployeeService">
<property name="employeeDao" ref="employeeDao"></property>
</bean>
<bean id="departmentService" class="com.atguigu.ssh.service.DepartmentService">
<property name="departmentDao" ref="departmentDao"></property>
</bean>
<bean id="employeeAction" class="com.atguigu.ssh.actions.EmployeeAction"
scope="prototype">
<property name="employeeService" ref="employeeService"></property>
<property name="departmentService" ref="departmentService"></property>
</bean>
</beans>