`
hunter090730
  • 浏览: 191857 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Spring2.5.6+Struts2.318+ibatis2的整合

 
阅读更多
根SSH整合相似,在web.xml中加入struts2的Filter以让web container加载struts framework,同时将struts2中用到的Action也一并交由spring container来管理。
struts2ibatis
即struts2的acton生成交由spring container管理(具体org.springframework.web.context.ContextLoaderListener),而ibatis则由spring来生成。
org.springframework.orm.ibatis.SqlMapClientFactoryBean
步骤1:web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><display-name>java_eBooking</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!-- 
1、指定spring的配置文件,默认从webroot根目录寻找配置文件, 
我们可以通过spring提供的classpath:前缀指定从类路径下寻找  contextConfigLocation
--><!--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
--><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><filter><filter-name>struts2</filter-name><filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
步骤2:
src/applicationContext.xml,配置如下

<?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-2.5.xsd
           http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-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"><aspectj-autoproxy></aspectj-autoproxy><bean id="myAspect" class="com.ebooking.aop.BeforeExample"><!-- configure properties of aspect here as normal --></bean><bean id="productService" class="com.ebooking.service.ProductService"><property name="productDao"><ref bean="productDao"></ref></property></bean><bean id="hellowordAction" class="com.ebooking.web.struts.action.HelloWordAction" scope="prototype"><constructor-arg ref="hellowordService"></constructor-arg></bean><bean id="hellowordService" class="com.ebooking.service.HelloWordService"><property name="helloWordDao" ref="helloWordDao"></property></bean><bean id="helloWordDao" class="com.ebooking.dao.impl.HelloWordDaoImpl"></bean><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><value>classpath:conn/jdbc.properties</value></property></bean><bean id="productDao" class="com.ebooking.dao.impl.ibatis.maps.SqlMapProductDao"><property name="sqlMapClient" ref="sqlMapClient"></property></bean><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><value>classpath:conn/jdbc.properties</value></property></bean><bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"><property name="configLocation" value="classpath:sql-map-config.xml"></property><property name="dataSource" ref="dataSource"></property></bean><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"><property name="driverClass" value="${jdbc.driverClassName}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property><property name="minPoolSize" value="1"></property><property name="initialPoolSize" value="2"></property><property name="maxPoolSize" value="20"></property><property name="maxIdleTime" value="1800"></property><property name="acquireIncrement" value="2"></property><property name="maxStatements" value="3"></property><property name="idleConnectionTestPeriod" value="1800"></property><property name="acquireRetryAttempts" value="3"></property></bean><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><config><pointcut id="bussinessService" expression="execution(public * com.ebooking.service.*.*(..))"></pointcut><advisor pointcut-ref="bussinessService" advice-ref="txAdvice"></advisor></config><advice id="txAdvice" transaction-manager="txManager"><attributes><method name="list" read-only="true"></method><method name="*" propagation="REQUIRED"></method></attributes></advice></beans>
另外:src/sql-map-config.xml,配置如下:
<?xml version="1.0" encoding="UTF-8"?><sqlmapconfig><sqlmap resource="com/ebooking/dao/impl/ibatis/maps/Product.xml"></sqlmap></sqlmapconfig>
=====Product.xml========
<?xml version="1.0" encoding="UTF-8"?><sqlmap namespace="Product"><cachemodel id="oneDayProduct" type="MEMORY"><flushinterval hours="24"></flushinterval><property name="reference-type" value="WEAK"></property></cachemodel><resultmap id="result" class="com.ebooking.model.Product"><result property="productId" column="productid" columnindex="1"></result><result property="name" column="name" columnindex="2"></result><result property="description" column="descn" columnindex="3"></result><result property="categoryId" column="category" columnindex="4"></result></resultmap><select id="getProduct" resultmap="result">
    select productid, name, descn, category from product where productid = #value#
  </select><select id="getProductListByCategory" resultmap="result">
    select productid, name, descn, category from product where category = #value#
  </select><select id="searchProductList" resultmap="result">
    select productid, name, descn, category from product
    <dynamic prepend="where"><iterate property="keywordList" open="(" close=")" conjunction="OR">
        lower(name) like #keywordList[]# OR lower(category) like #keywordList[]# OR lower(descn) like #keywordList[]#
      </iterate></dynamic></select></sqlmap>

尔后就是一般的业务调用了,
总体来说:SSI比SSP整合简单,当然在整合的过程中有一个简单的测试方法:
你在作好以上相关配置好,可写一个直接调用action的测试类,如能得到你想要的结果则证明你配置OK,如还在浏览器上运行失败则要向你jar导入出问题了如版本不匹对这方面来考虑了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics