<一>SSI框架
1、框架簡(jiǎn)介
? ? ? MVC對(duì)于我們來(lái)說(shuō),已經(jīng)不陌生了,它起源于20世紀(jì)80年代針對(duì)smalltalk語(yǔ)言的一種軟件設(shè)計(jì)模式。現(xiàn)在已經(jīng)被廣泛應(yīng)用。近年來(lái),隨著java的盛行,MVC的低耦合性、高重用性、可維護(hù)性、軟件工程的可管理性等諸多優(yōu)點(diǎn)使其在java平臺(tái)很受歡迎,期間,也誕生了許多優(yōu)秀的MVC框架,如專注于控制層的struts、webwork、struts2、JSF等框架;專注于業(yè)務(wù)邏輯方面的Spring框架;專注于持久層的Hibernate、iBatis、Castor、JORM等框架。
下面對(duì)SSI框架:struts2+spring+iBatis的三個(gè)開(kāi)源的MVC框架進(jìn)行說(shuō)明
Struts2主要來(lái)源于webwork框架,與Struts1相比:
Spring功能非常的強(qiáng)大:
iBatis則是一種輕量級(jí)的OR Mapping框架,與Hibernate相比,iBatis提供了半自動(dòng)化對(duì)象關(guān)系映射的實(shí)現(xiàn),開(kāi)發(fā)人員需要編寫(xiě)具體的sql語(yǔ)句,為系統(tǒng)設(shè)計(jì)提供了更大的自由空間,為sql語(yǔ)句優(yōu)化提供了便利。
2、框架結(jié)構(gòu)
下面這張圖就是我們所用到的這三種框架的結(jié)合體,下面對(duì)其做以簡(jiǎn)單的介紹。
(1)控制層
在控制層,利用Strtus2標(biāo)簽功能,在Action中直接與jsp頁(yè)面上的數(shù)據(jù)進(jìn)行交互。在調(diào)用業(yè)務(wù)邏輯層應(yīng)用時(shí),Struts2提供了對(duì)Spring的支持。開(kāi)發(fā)人員需要完成對(duì)struts.xml的配置工作和各個(gè)Action類的編寫(xiě)。
總的struts.xml配置文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"> <struts><package name="struts-init" extends="struts-default"><action name="index" class="anonymousAction" method="execute"><result name="success">province_level/shanxi/login.jsp</result></action><action name="nologinAct" class="nologinAct"><result name="success">/common/nologin.jsp</result></action><!-- 插件下載 --><action name="downLoadFileLogin" class="anonymousAction" method="getDownLoadFile"><result name="success" type="stream"><!--文件類型,當(dāng)前是所有的類型 --><param name="contentType">application/octet-stream</param><!--第一個(gè)參數(shù)是指打開(kāi)下載框默認(rèn)是inline瀏覽器能打開(kāi)的就不去下載直接打開(kāi) --><param name="contentDisposition">attachment;fileName="${fileName}"</param><param name="inputName">inputStream</param> <!--方法名字 --><param name="bufferSize">1024</param> <!--文件大小 --></result></action><interceptors><!-- 聲明攔截器 --><interceptor name="checkPrivilege"class="com.highland.criminal.business.sysman.Interceptor.CheckAuthorityInterceptor"></interceptor><interceptor name="json" class="org.apache.struts2.json.JSONInterceptor" /><!-- 自定義攔截器 --><interceptor name="exceptionInterceptor" class="com.highland.framework.web.interceptor.ExceptionInterceptor"></interceptor><!-- 聲明一個(gè)新的攔截器棧(先檢查權(quán)限) --><interceptor-stack name="myDefaultStack"><interceptor-ref name="defaultStack" /><interceptor-ref name="checkPrivilege" /><interceptor-ref name="exceptionInterceptor" /><interceptor-ref name="json" /></interceptor-stack></interceptors><!-- 默認(rèn)使用的攔截器(攔截器棧) --><default-interceptor-ref name="myDefaultStack" /><!-- 全局的Result配置 --><global-results><result name="error">/common/error.jsp</result><result name="login" type="redirect">nologinAct.action</result><result name="noPrivilegeError">/xtba/common/noPrivilegeError.jsp</result></global-results><global-exception-mappings><exception-mapping result="error"exception="com.highland.framework.exception.BaseException"></exception-mapping></global-exception-mappings><!-- 為兼容ecside增加的錯(cuò)誤action --><action name="errorAction"class="com.highland.criminal.business.sysman.action.ExceptionAction"><result>/common/error.jsp</result></action></package><include file="struts/struts-hdzhfx.xml"></include><!-- 綜合分析--></struts>由于項(xiàng)目中可能涉及多個(gè)功能模塊,所以在總的struts.xml中通過(guò)<include>標(biāo)簽引入各個(gè)功能模塊的xml配置。這里以綜合分析模塊為例,對(duì)struts-hdzhfx.xml配置文件進(jìn)行分析
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 綜合分析報(bào)告--><package name="hdzhfx" namespace="/" extends="struts-init"><!-- 這種方式配置返回值鏈接:返回簡(jiǎn)單json字符串參數(shù)total和rows --><action name="glhdfx_new" class="hdzhfxAction" method="glhdfx_new"><result name="success" type="json"><param name="includeProperties">total,rows</param></result></action><!-- 這種方式配置簡(jiǎn)單的查詢方法鏈接 --><action name="zhfx_thqfx" class="hdzhfxAction" method="thqfx_Query"></action><!-- 這種方式配置返回值鏈接:返回list數(shù)據(jù) --><action name="listhdkxx" class="hdzhfxAction" method="listhdxx" ><result name="success">/xzzxyw/hdfx/queryHdxxList.jsp</result><result name = "success_hdxx" type = "json"><param name = "root">bkxxs</param></result></action></package> </struts>(2)業(yè)務(wù)邏輯層
在業(yè)務(wù)邏輯層,利用Spring框架的依賴注入實(shí)現(xiàn)對(duì)業(yè)務(wù)邏輯類和Dao類的實(shí)例進(jìn)行托管;
在事務(wù)處理方面,利用Spring提供的面向切面的事務(wù)處理功能,使對(duì)數(shù)據(jù)的事務(wù)控制脫離于數(shù)據(jù)訪問(wèn)接口實(shí)現(xiàn);
在對(duì)象關(guān)系映射方面,利用Spring對(duì)數(shù)據(jù)庫(kù)連接池的托管和對(duì)iBatis框架的支持。
開(kāi)發(fā)人員需要完成對(duì)數(shù)據(jù)源的配置、對(duì)不同模塊所對(duì)應(yīng)的application*.xml文件的配置,以及對(duì)業(yè)務(wù)邏輯接口的定義和業(yè)務(wù)邏輯實(shí)現(xiàn)的編寫(xiě)。
①?spring-->ibatis-->applicationContext-ibatis.xml:這里加載數(shù)據(jù)源配置及事務(wù)機(jī)制
<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><!-- 引入項(xiàng)目的jdbc配置文件 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:/jdbc.properties</value></list></property></bean><!-- 配置數(shù)據(jù)源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass"><value>${jdbc.driverClassName}</value></property><property name="jdbcUrl"><value>${jdbc.url}</value></property><property name="user"><value>${jdbc.username}</value></property><property name="password"><value>${jdbc.password}</value></property><property name="initialPoolSize"><value>50</value></property><property name="minPoolSize"><value>50</value></property><property name="maxPoolSize"><value>300</value></property><property name="checkoutTimeout"><value>5000</value></property><property name="maxIdleTime"><value>1800</value></property><property name="idleConnectionTestPeriod"><value>3000</value></property><property name="acquireIncrement"><value>5</value></property><property name="maxStatements" value="0"/><property name="testConnectionOnCheckout" value="false"/> </bean><!-- 定義事務(wù)管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource"><ref bean="dataSource"/></property></bean><!-- Spring的IBatis模板 --><!-- 加入對(duì)大數(shù)據(jù)類型操作的支持 --><bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/><bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" ><property name="configLocations"><!-- 重點(diǎn):這里將spring中的ibatis層與ibatis連接起來(lái)了 --><value>classpath*:/ibatis/app-sqlmap-config.xml</value></property><property name="dataSource"><ref bean="dataSource" /></property><property name="lobHandler" ref="lobHandler"/> <!-- 加入對(duì)大數(shù)據(jù)類型操作的支持 --> </bean><bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"><property name="sqlMapClient" ref="sqlMapClient"></property> </bean><!--開(kāi)啟事務(wù) --><tx:annotation-driven transaction-manager="transactionManager" /> </beans>spring-->ibatis-->app-ibatis-hdzhfx.xml?(這個(gè)文件與applicationContext-ibatis.xml是通過(guò)<ref bean="sqlMapClient">關(guān)聯(lián)到一起的)
這里配置Dao的bean值,是為下一層spring-->service-->app-service-hdzhfx.xml中的bean的返回值<ref bean="tbryhdDao" />做的配置。
<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="tbryhdDao" class="com.highland.criminal.business.dao.stdao.TbRyHdDao"><property name="sqlMapClient"><ref bean="sqlMapClient" /></property></bean><bean id="tbryhdtxlbdDao" class="com.highland.criminal.business.dao.stdao.TbRyHdTxlBdDao"><property name="sqlMapClient"><ref bean="sqlMapClient" /></property></bean> </beans>② spring-->service-->app-service-hdzhfx.xml承上啟下的作用。
承上:<ref bean="tbryhdDao" />這里的ref中bean的值對(duì)應(yīng)的是spring-->ibatis-->app-ibatis-hdzhfx.xml中配置的bean的id? ??
啟下:這里的bean 的id值z(mì)hHdfxService對(duì)應(yīng)的是spring-->struts-->app-struts-hdzhfx.xml中需要的service的名字 <ref bean="zhHdfxService" />。
<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- 配置bean:注意這里的bean的id對(duì)應(yīng)的是spring中app-struts-hdzhfx.xml中的ref對(duì)應(yīng)的bean值--><bean id="zhHdfxService" parent="baseTransactionProxy"><property name="target"><bean class="com.highland.criminal.business.xzqbyp.hdfx.service.imp.HdZhfxServiceImpl"><!-- 這里的ref中bean的值對(duì)應(yīng)的是spring中的ibatis下app-ibatis-hdzhfx.xml中配置的bean的id --> <property name="tbryhdDao"><ref bean="tbryhdDao" /></property><property name="tbRyhdtxlDao"><ref bean="tbryhdtxlbdDao" /></property></bean></property></bean></beans>③ spring -->struts-->app-struts-hdzhfx.xml:
?<ref bean="zhHdfxService" />這里的bean的值對(duì)應(yīng)的是spring-->service-->app-service-hdzhfx.xml中配置的bean的id
這里bean的id值hdzhfxAction對(duì)應(yīng)的是配置struts-hdzhfx.xml中需要的action的class類值
<?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:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><!-- 配置bean:注意這里的bean的id對(duì)應(yīng)的是spring中struts-hdzhfx.xml中的action的class --><bean id="hdzhfxAction" class="com.highland.criminal.business.xzqbyp.hdfx.action.HdZhfxAction" scope="prototype"><property name="zhHdfxService"><ref bean="zhHdfxService" /><!-- 注意:這里的bean的值對(duì)應(yīng)的是spring中service中的app-service-hdzhfx.xml中配置的bean的id --></property></bean></beans>(3) 持久層
在持久層,利用iBatis提供的半自動(dòng)化對(duì)象關(guān)系映射實(shí)現(xiàn)。開(kāi)發(fā)人員需要編寫(xiě)具體的sql語(yǔ)句,為系統(tǒng)設(shè)計(jì)提供了更大的自由空間。另外,開(kāi)發(fā)人員需要完成對(duì)ibatis的配置文件app-sqlmap-config.xml和*-sqlmap.xml的配置,以及對(duì)DAO接口的定義和DAO接口的實(shí)現(xiàn)。
app-sqlmap-config.xml:這里配置了多個(gè)sql文件的引用
與spring層之間的關(guān)聯(lián):在spring-->ibatis-->applicationContext-ibatis.xml中配置了這:
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
? ? ? ? <property name="sqlMapClient" ref="sqlMapClient"></property> ?
? ? </bean>
hdzhfx-sqlmap.xml:這個(gè)文件里寫(xiě)sql語(yǔ)句
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "sql-map-2.dtd"> <sqlMap namespace="hdfxnew"><!-- 映射實(shí)體 --><typeAlias alias="hdfxxx" type = "com.highland.framework.hdanalysis.domain.hdfxdomain" /><!-- 根據(jù)id查詢信息 --><select id="queryHdInfo" parameterClass="hdfxxx" resultClass="hdfxxx">select BJGSD,CTLX,MYZT,THDD,THJZ,JZDQ,JZXQ,IMEI,IMSI,JD,WD,DFTHDD,DFTHJZ,DFJZDM,DFIMSI,DFGSD,DFIMEI,DFHMJZXM,DFCH,DFJZQW,DFJD,DFWD,HOLDING_TIME,BJHM,DFHM,CALL_TYPE,START_TIME,END_TIME from HD_CALL_INFO where 1=1 and scbz=0<isNotEmpty property="BJHM"> and BJHM= #BJHM#</isNotEmpty><isNotEmpty property="DFHM"> and DFHM= #DFHM#</isNotEmpty><isNotEmpty property="CALL_TYPE"> and CALL_TYPE= #CALL_TYPE#</isNotEmpty><isNotEmpty property="START_TIME"> and to_date(START_TIME,'yyyy-MM-dd hh24miss') >= to_date(#START_TIME#,'yyyy-MM-dd')</isNotEmpty><isNotEmpty property="END_TIME"> and to_date(END_TIME,'yyyy-MM-dd hh24miss') <![CDATA[<=]]> to_date(#END_TIME#,'yyyy-MM-dd')</isNotEmpty><isNotEmpty property="THDD"> and THDD= #THDD#</isNotEmpty></select><!-- 新增記錄 --><insert id="savedrrzdata" parameterClass="hdfxxx">insert into HD_CALL_LOG (xxzjbh,jzhm,ckrxm,asjbh,ckrsfzh,drzs,drms,drsj)values (#xxzjbh#,#jzhm#,#ckrxm#,#asjbh#,#ckrsfzh#,#drzs#,#drms#,#drsj#)<selectKey resultClass="java.lang.String" keyProperty="xxzjbh">select #xxzjbh# as xxzjbh from dual</selectKey></insert><!-- 查詢所有的信息 --><select id="queryHdlogInfo" parameterClass="hdfxxx" resultClass="hdfxxx">select xxzjbh,ckrsfzh,jzhm,ckrxm,asjbh,drzs,drms,drsj from HD_CALL_LOG where 1=1 and scbz=0</select> </sqlMap>(4)小結(jié)
????????在各層之間進(jìn)行交換的過(guò)程中,利用數(shù)據(jù)傳輸類進(jìn)行數(shù)據(jù)的傳遞和交互。其中數(shù)據(jù)傳輸類與數(shù)據(jù)庫(kù)表一一對(duì)應(yīng)。
SSI框架能夠降低代碼的耦合度,增強(qiáng)了代碼的健壯性和可重用性,加快了開(kāi)發(fā)速度,但是也有一些不足之處,比如由于三種框架的配置文件較多,也會(huì)帶來(lái)一些不便,特別是對(duì)于較小的應(yīng)用來(lái)說(shuō)更是如此。
總結(jié)
- 上一篇: 2022-2028全球自动车道警告系统行
- 下一篇: ROS学习笔记之——amcl源码的解读