1:建立資料庫並把表建立好
2:開啟MyEclipse,利用MyEclipse的資料庫瀏覽建立一個資料庫連接,串連目標是在第一步建立的資料庫
3:建立考試的web工程
4:添加struts支援,同時刪除struts的支援庫
5:添加spring支援(不勾選支援庫),開啟applicationContext.xml,刪除所有代碼替換裡面的內容如下:
代碼
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
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-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
">
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" autowire="byName"/>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
6:添加hibernate支援(不勾選支援庫),資料庫選擇我們第二步建立的名字
7:開啟/WEB-INF/struts-config.xml,在<action-mappings />後面一行加入一行代碼
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
8:開啟/WEB-INF/web.xml,加入如下代碼(注意,如果你用的是GBK編碼格式則將裡面的UTF-8修改為GBK既可,其他地方不用變):
代碼
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>flashMode</param-name>
<param-value>COMMIT</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
9:將SSH的包匯入工程或者將裡面的jar檔案拷貝到/WEB-INF/lib目錄下,兩者選其一。
10:反轉資料庫表為實體類(列實體類名稱為Mods),(如果不清楚可以如此操作:開啟Myeclipse的資料庫瀏覽器視窗,開啟第二步建立的串連,開啟後進入到dbo目錄下的table目錄,右擊建立好的表選擇Hibernate reverse engining)
11:建立實體類的DAO介面,並包含如下四個方法
void add(Mods mods);
void del(Integer id);
void update(Mods mods);
Mods get(Integer id);
List loadAll();
12:實現Dao介面,這個實作類別必須繼承HibernateDaoSupport並在spring設定檔中註冊此類
13:建立業務介面並實現,類上面加入一個註記@Transactional(rollbackFor={Exception.class})
14:建立Action,此Action繼承DispatchAction,並在spring註冊此類
15:編寫頁面並修改action
測試並完成!
記得:dao類和biz類 private後要set
dao:
public class GoodsDaoImpl extends HibernateDaoSupport implements GoodsDao {
繼承HibernateDaoSupport 源碼包: /Files/Simcoder/ssh_framework.rar
視頻教程:完整案例 http://u.115.com/file/f61835a6ce
SSH.exe