SSH框架整合-慕課課程
學習時間:2018年12月3日
慕課鏈接:基於SSH實現員工管理系統之框架整合篇
內容:Struts2+Hibernate+Spring框架整合,分為Struts2整合Spring和Spring整合Hibernate兩步進行
源碼:Github:SSH_Base
一、開發環境搭建
1. jar 包導入
2. 引入相關配置文件
-
web.xml-->Struts2 過濾器配置
<!-- Struts2 過濾器的配置--> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> -
struts2.xml -->src
>struts-2.3.36-all.zip\struts-2.3.36\apps\struts2-blank-2.3.36.war\WEB-INF\classes\struts2.xml -
web.xml -->Spring 核心監聽器
> ContextLoaderListener--> ContextLoader --> 105:contextConfigLocation<!-- Spring 核心監聽器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> - applicationContext.xml
3. 創建包結構和實體
- 包結構:
- com.ssh.action
- com.ssh.dao-
- com.ssh.domain - 實體 product
- com.ssh.service
二、Struts2整合Spring
1. 頁面創建
- 使用struts2標籤 佈局頁面
<s:form action="" method="post" namespace="/" theme="simple">
<table border="1" width="400">
<tr>
<td>商品名稱:</td>
<td><s:textfield name="pname"/></td>
</tr>
<tr>
<td>商品價格:</td>
<td><s:textfield name="price"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="添加" /></td>
</tr>
</table>
</s:form>
2. 編寫Action,Service,Dao的類
-
com.ssh.action
ProductAction //商品管理的Action
public class ProductAction extends ActionSupport implements ModelDriven<Product>{ //^extends ActionSupport implements ModelDriven<Product> (模型驅動的接口) //模型驅動使用的類 private Product product = new Product(); @Override public Product getModel() { // TODO Auto-generated method stub return product; } /*注入Service struts2-spring-plugin-2.3.36.jar > struts-plugin.xml > struts.objectFactory >struts.objectFactory.spring.autoWire = name 自動裝配*/ /*Struts 和Spring 整合過程中按名字自動注入業務層的類*/ private ProductService productService; public void setProductService(ProductService productService) { this.productService = productService; } } -
com.ssh.dao
ProductDao //商品管理的DAO
-
com.ssh.domain
實體 product
-
com.ssh.service
ProductService //商品管理的業務層
//業務層 注入Dao的類 private ProductDao productDao; public void setProductDao(ProductDao productDao) { this.productDao = productDao; }
3. 配置Action,Service,Dao的類
-
aplicationContext.xml 配置業務層的類 *
<!-- 配置業務層的類 --> <bean id="productService" class="com.epoint.ssh.service.ProductService"> <property name="productDao" ref="productDao"/> </bean> <!-- 配置DAO的類 --> <bean id="productDao" class="com.epoint.ssh.dao.ProductDao"> </bean> -
Struts2和Spring整合的兩種方式
-
(1) Action 類由Struts2 自身創建
-
struts2.xml————>
<s:form action="product_save" method="post" namespace="/" theme="simple">- 在applicationContext.xml中配置Service DAO 兩個<bean>標籤(上一步*)
-
-
(2)actio 類交給Spring創建
<!--配置action類 --> <bean id="productAction" class="com.epoint.ssh.action.ProductAction" scope="prototype"> <!--手動注入service --> <property name="productService" ref="productService"></property> </bean> <package name="ssh" extends="struts-default" namespace="/"> <action name="product_*" class="productAction" method="{1}" > </action> </package>
-
- Action中執行的方法
三、Sprinng整合 Hibernate
1. 創建數據庫
create database myssh
2. 創建映射文件 以及Spring中的配置
-
/ssh/src/com/epoint/ssh/domain/Product.hbm.xml
--> /org/hibernate/hibernate-mapping-3.0.dtd<hibernate-mapping> <class name="com.epoint.ssh.domain.Product" table="product"> <id name="pid" column="pid"> <generator class="native"/><!-- native表示自增 --> </id> <property name="pname" column="pname" length="20"/> <property name="price" column="price"/> </class> </hibernate-mapping> -
applicationContext.xml
<!-- 引入外部配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置連接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/myssh?useSSL=false"/> <property name="user" value="root"/> <property name="password" value="Gepoint"/> </bean> <!-- 配置Hibernate --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!--注入連接池 --> <property name="dataSource" ref="dataSource"></property> <!--配置Hibernate屬性 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!--方言 --> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!--加載Hibernate配置文件 --> <property name="mappingResources"> <list> <value>com/epoint/ssh/domain/Product.hbm.xml</value> </list> </property> </bean>
3. 編寫DAO的代碼
-
注入SessionFactory:
- 在 Dao 上extends HibernateDaoSupport
- 配置applicationContext中的Dao
<bean id="productDao" class="com.epoint.ssh.dao.ProductDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> -
在Dao中調用模板完成保存操作
this.getHibernateTemplate().save(product)
4. 添加事務管理
<!-- 配置事務管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 開啓註解事務 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
5.問題
-
錯誤1:
Causedby:java.lang.ClassNotFoundException:com.mchange.v2.c3p0.ComboPooledDataSource解決:
1.導入c3p0-0.9.2.1.jar和mchange-commons-java-0.2.3.4.jar(有的導入就解決,有的還會報錯,報錯的看第二步)
2.對於<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><bean>中的class不能直接複製粘貼,要通過“提示”敲出來,如果直接複製或者不按提示敲出來可能還會繼續報同樣的錯誤 -
錯誤2:
Loading class 'com.mysql.jdbc.Driver'. This is deprecated.解決:
com.mysql.jdbc.Driver 改為 com.mysql.cj.jdbc.Driver -
錯誤3:
MYSQL:WARN: Establishing SSL connection without server's identity verification is not recommended.解決:
jdbc:mysql://localhost:3306/test?useSSL=false -
錯誤4:
java.sql.SQLException: Access denied for user 'root '@'localhost' (using password: YES)解決:
參考:java.sql.SQLException