SSH回顧

1 引入jar包
Struts2的jar包
- D:\Struts2\struts-2.3.35\apps\struts2-blank\WEB-INF\lib 開發基本包
- Struts2有一些包是需要了解的:
- struts2-convention-plugin-2.3.35 注解開發包
- struts2-json-plugin-2.3.35.jar 整合ajax
- struts2-spring-plugin-2.3.35.jar 整合Spring
Hibernate的jar包
- D:\Hibernate\hibernate-release-5.0.7.Final\lib\required 開發必須
- mysql-connector-java-5.1.43-bin mysql驅動
- 日志記錄
- 注意:Struts2和Hibernate都引入了一個相同的javasistjar包(javasist版本不一樣容易沖突),刪掉一個版本低的
- 使用C3P0連接池,還需要引:
Spring的jar包
- IOC的開發
- AOP的開發
- JDBC模版的開發
- 整合web項目的開發
- 整合單元測試
- 整合Hibernate的開發
IOC的6個基本包

AOC的包

JDBC開發,事務管理:

整合單元測試:spring-test-4.2.4.RELEASE.jar
整合web項目:

整合Hibernate的開發:

進行Spring和Struts2的整合:
引入整合的插件包
第二步:引入配置文件
Struts2的配置文件:
- web.xml
- struts.xml

Hibernate的配置文件:
- hibernate.cfg.xml,刪掉與線程綁定的session
- 映射文件
- 日志文件
Spring的配置文件:
- web.xml
- applicationContext.xml
- 日志記錄(一個就行,如果引過) log4j.properties

第三步:創建包結構和類

第四步: 引入相關的頁面
第五步:修改menu.jsp,修改add.jsp

第六步:編寫Action,添加到提交數據到action

第七步:Spring整合Struts2 方式一:Action由Struts2創建(Service由Spring,在Action類中注入Service)
編寫Action:

在action中引入Service
傳統方式:

進行Spring和Struts2的整合:
引入整合的插件包
在插件包中有如下配置:

開啟了一個常量,在Struts2中開啟這個常量,就會引發下面常量生效

讓action按照名稱自動注入service
將service交給Spring管理:

Action中注入Service



第八步:Spring整合Struts2方式二:Action交給Spring創建(推薦,如果用Struts2創建Action不如用Spring工廠配置,因為可以用AOP增強action)
引入插件包
將Action交給Spring

struts.xml配置Action,使用Spring創建的

注意:
1 原來的Action是多例的,現在是單例的,一個Action只有一個值棧,如果你保存,刪除,修改都用一個值棧,在取值的時候就會出現問題。

2 需要手動注入Service

測試:

第九步:Service調用Dao
將Dao交給Spring管理

在Service層注入Dao


第十步:Spring整合Hibernate框架
- 創建數據庫和表
- 編寫實體和映射
Spring和Hibernate整合
在Spring配置文件中引入Hibernate配置信息

在Spring和Hibernate整合后,Spring提供了一個Hibernate的模板類,用來簡化Hibernate開發,改寫Dao繼承HibernateDaoSupport

配置的時候在Dao中直接注入SessionFactory,就會創建Hibernate模版類

在Dao中使用Hibernate的模版實現保存操作

第十步:配置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: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標簽引入jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- <context:annotation-config/> --> <!-- 配置C3P0連接池 --> <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.user}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 配置Action --> <bean id="customerAction" class="com.ssh.web.action.CustomerAction" scope="prototype"> <property name="customerService" ref="customerService"/> </bean> <!-- 注入CustomerService --> <bean id="customerService" class="com.ssh.service.impl.CustomerServiceImpl"> <property name="customerDao" ref="customerDao"/> </bean> <!-- 注入Dao --> <bean id="customerDao" class="com.ssh.dao.impl.CustomerDaoImpl"> </bean> </beans>

