SSH(Struts2+Spring+Hibernate)框架搭建流程

由於最近的學習使用到SSH框架,爲了更加的瞭解SSH框架的搭建流程,我就具體的描述一下java

我使用的是eclipse4.六、Tomcat八、JDK1.8,數據庫MySQL5.6mysql

我下載的是Struts-2.3.30、Spring-4.2.二、Hibernate5.2.2web

我已經給你們整理好所用到的包Struts-2.3.30.rarSpring-4.2.2.rarHibernate5.2.2.rar(單擊便可下載)spring

下載完成後,開始搭建sql

1、Struts2(MVC)的搭建:數據庫

相關描述apache

Ⅰ.本質上至關於一個Servlet服務器

Ⅱ.不須要手動獲取傳遞參數 (只須要定義聲明屬性名和設置get、set的方法)、也不須要手動跳轉(只須要struts.xml配置相關的路徑)session

Ⅲ.對項目的分包(例如:dao、service、entity等等),實現MVC模式的開發app

Ⅳ.MVC: Action屬於Model,而JSP是View頁面的展現,其中過濾器起到了Controller的做用

實現流程

1.首先新建一個項目

2.把下載好的Struts-2.3.30解壓後,找到struts-2.3.30\apps\ struts2-blank.war \WEB-INF\lib的jar包,複製到項目的WebContent\WEB-INF\lib目錄下

3.打開WebContent\WEB-INF\lib\web.xml,定義過濾器,編輯以下

1 <filter>
2     <filter-name>struts2</filter-name>
3     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
4   </filter>
5   <filter-mapping>
6     <filter-name>struts2</filter-name>
7     <url-pattern>/*</url-pattern>
8 </filter-mapping>

src目錄下新建一個類,必須繼承ActionSupport父類纔是Action,編輯以下

1 //默認方法,struts自動調用
2 public class IndexAction extends ActionSupport {
3     public String execute() {
4         //默認返回值
5         return "success";
6     }
7 }

4.在項目的src目錄下新建一個struts.xml文件

 編輯以下

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <!-- 上面的頭,注意版本,從樣例裏複製過來 showcase.war\WEB-INF\src\java\struts.xml -->
 6 <struts>
 7     <package name="mypck" extends="struts-default">
 8         <!—name對應的action請求名稱,method(不寫等於默認方法)默認執行方法是 execute,class定義具體的類-->
 9         <action name="Index" class="ssh.action.IndexAction" method="execute">
10             <!-- 接收Action類返回的值,進行跳轉指定的頁面 -->
11             <result name="success">/WEB-INF/jsp/index.jsp</result>
12             <result name="error">/WEB-INF/jsp/error.jsp</result>
13         </action>
14     </package>
15 </struts>

完成以上流程,基本上Struts2的搭建就ok了

2、Spring(注入實例)的使用:

相關描述

Ⅰ.每一層的代碼之間的耦合改成模塊(分離/解耦),代碼之間互不影響

Ⅱ.再也不關注具體的實現類的實例

Ⅲ.更換不一樣的技術(模塊),不須要改動代碼,只須要修改applicationContext.xml的相關配置信息

Ⅳ.主要功能IOC(控制反轉)鬆耦合、AOP (面向切面)內聚性

實現流程

1.首先在Struts2搭建基礎上,再把下載好的Spring-4.2.2解壓後,找到spring-framework-4.2.2.RELEASE\libs的jar包,複製到項目的WebContent\WEB-INF\lib目錄下

 (注意: 包含<Javadoc、sources源碼>的均可以不要)

(注意:導入Spring的包還不夠,還差了Struts-2.3.30包裏的struts-2.3.30-all\struts-2.3.30\apps\struts2-showcase.war\WEB-INF\lib的兩個jar包)

2. 編輯web.xml文件,定義監聽器 ,添加下列代碼

 1 <!-- 不寫默認Spring須要讀取 WebContent\WEB-INF\applicationContext.xml-->
 2   <context-param>
 3     <param-name>contextConfigLocation</param-name>
 4     <!-- 改變默認讀取路徑爲src目錄下的 applicationContext.xml-->
 5     <!-- 在改變的路徑下仍是沒找到,便自動到默認路徑查找 -->
 6     <param-value>classpath:applicationContext.xml</param-value>
 7   </context-param>
 8   <!-- 定義監聽器 -->
 9   <listener>
10     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
11   </listener>

3.在src目錄下新建applicationContext.xml用做編寫Spring

 編寫以下(注意:本例沒有爲Action類注入實例,如下注入只提供參考)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"    
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
 4         xmlns:p="http://www.springframework.org/schema/p"  
 5         xmlns:aop="http://www.springframework.org/schema/aop"   
 6         xmlns:context="http://www.springframework.org/schema/context"  
 7         xmlns:jee="http://www.springframework.org/schema/jee"  
 8         xmlns:tx="http://www.springframework.org/schema/tx"  
 9         xsi:schemaLocation="    
10             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  
11             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
12             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
13             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd  
14             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
15     <!-- 上面的頭,注意版本,從樣例裏複製過來 -->
16     <!-- id定義名字,class具體的類,scope="prototype"定義非單例 -->
17     <bean id="myActionIndex" class="ssh.action.IndexAction" scope="prototype">
18         <!-- 本例沒有 -->
19         <!-- 爲Action類裏的is屬性注入id爲myIndexService的實例 -->
20         <property name="is" ref="myIndexService" />    
21     </bean>
22     <!-- 本例沒有 -->
23         <bean id="myIndexService" class="ssh.service.IndexServiceImpl" scope="prototype">
24     </bean>
25 </beans>
1 //本例沒有
2 public class IndexAction extends ActionSupport {
3     
4     //聲明service,但不給它建立具體的實現類的實例,
5     private IndexService is = null;
6     public void setIs(IndexService is) {
7         this.is = is;
8     }
9 }

4.編輯struts.xml文件,添加下列代碼(注意:新版本能夠不用寫,包括本例)

1 <struts>
2     <!-- 告知Struts2運行時使用Spring來建立對象 -->
3     <constant name="struts.objectFactory" value="spring" />
4 </struts>

改寫action裏的class屬性

1 <!-- class改寫成Spring注入的id定義的名字 -->
2 <action name="Index" class="ssh.action.IndexAction" method="execute">

完成以上流程,基本上Struts2-Spring的搭建就ok了

3、Hibernate(數據層)的搭建:

相關描述

Ⅰ.服務器與數據庫之間的交互

Ⅱ. Hibernate封裝了一系列的JDBC代碼,提供相應的方法咱們使用,使咱們的開發變得簡單快捷,效率大大提升

實現流程

1. 首先在Struts2-spring搭建基礎上,再把下載好的Hibernate5.2.2解壓後,找到hibernate-release-5.2.2.Final\lib\required的jar包,複製到項目的WebContent\WEB-INF\lib目錄下(注意:本例使用是MySQL,記得導入對應的數據庫驅動包)

2.編寫applicationContext.xml文件,添加如下代碼,用做描述具體的數據庫信息(定義1個sessionFactory,用做於注入)

 1 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
 2         <!-- 注入鏈接池,包含了數據庫用戶名,密碼等等信息 -->
 3         <property name="dataSource" ref="myDataSource"></property>
 4         <property name="hibernateProperties">
 5             <props>
 6                 <!-- 數據庫的方言 -->
 7                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
 8                 <!-- 是否顯示打印sql語句-->
 9                 <prop key="hibernate.show_sql">true</prop>
10                 <!-- 是否格式化顯示sql語句-->
11                 <prop key="hibernate.format_sql">true</prop>
12                 <!-- 是否自動提交-->
13                 <prop key="hibernate.connection.autocommit">false</prop>
14                 <!-- 沒表幫你建立表,有表就更新表信息 -->
15                 <prop key="hibernate.hbm2ddl">update</prop>
16             </props>
17         </property>
18         <property name="mappingResources">
19             <list>
20                 <!-- 映射文件路徑(實體類) -->
21                 <value>ssh/entity/BookCard.hbm.xml</value>
22             </list>
23         </property>
24     </bean>

3.爲c3p0數據池導入相應的jar包,在Hibernate5.2.2壓縮包hibernate-release-5.2.2.Final\lib\optional\c3p0的3個jar包, 複製到項目的WebContent\WEB-INF\lib目錄下(提議:也能夠使用..\optional\dbcp的數據池)(區別:c3p0:穩定性、dbcp:速度比較快)

 

4. 繼續編寫applicationContext.xml文件,添加如下代碼(爲sessionFactory注入一個c3p0數據庫)

 1 <!-- 定義c3p0數據池 -->
 2     <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 3         <!-- 數據庫的驅動 -->
 4         <property name="driverClass" value="com.mysql.jdbc.Driver"/>
 5         <!-- 數據庫的路徑 -->
 6         <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/CardDB"/>
 7         <!-- 數據庫的用戶名 -->
 8         <property name="user" value="root"/>
 9         <!-- 數據庫的密碼 -->
10         <property name="password" value="123456"/>
11         <!-- 每300秒檢查全部鏈接池中的空閒鏈接 -->
12         <property name="idleConnectionTestPeriod" value="300"></property>
13         <!-- 最大空閒時間,900秒內未使用則鏈接被丟棄。若爲0則永不丟棄 -->
14         <property name="maxIdleTime" value="900"></property>
15         <!-- 最大鏈接數 -->
16         <property name="maxPoolSize" value="2"></property>
17     </bean>

5.第4步也能夠替換成,如下寫法,先在src目錄下建立一個jdbc.properties的文件(用於記錄保存數據庫相關的信息) 

1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/CardDB
3 jdbc.user=root
4 jdbc.password=123456

而後applicationContext.xml文件就能夠引用該文件內的參數

1 <property name="driverClass" value="${jdbc.driver}"/>
2 <property name="jdbcUrl" value="${jdbc.url}"/>
3 <property name="user" value="${jdbc.user}"/>
4 <property name="password" value="${jdbc.password}"/>

(注意:引用前提必需要在applicationContext.xml文件裏添加如下代碼,聲明一下引用的文件路徑)

 1 <context:property-placeholder location="classpath:jdbc.properties.txt" /> 

6.最後在實體類的目錄下新建一個實體類.hbm.xml的文件,進行如下編輯

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping">
 3       <!-- name是實體類的具體路徑,table是定義建立表的名字 -->
 4     <class name="ssh.entity.BookCard" table="BookCard">
 5         <!-- 定義主鍵的名稱 -->
 6         <id name="cid" column="cid">
 7             <!-- 定義主鍵爲自動增加 -->
 8             <generator class="native"></generator>
 9         </id>
10         <!-- 定義數據庫的其餘的字段 的具體描述-->
11         <property name="name" type="string" length="50" column="name" not-null="true"></property>
12         <property name="sex" type="string" length="2" column="sex"></property>
13         <property name="cardDate" type="date" column="cardDate"></property>
14         <property name="deposit" type="double" column="deposit"></property>
15     </class>
16 </hibernate-mapping>

以上就是一個簡單的SSH搭建的流程

相關文章
相關標籤/搜索