struts2.3所需包在上回已經提過,請看上篇構建struts2
此時整合架構還需要struts2-spring-plugin-2.3.8.jar
Spring3.2所需jar
spring-aop-3.2.1.RELEASE.jar
spring-aspects-3.2.1.RELEASE.jar
spring-beans-3.2.1.RELEASE.jar
spring-context-3.2.1.RELEASE.jar
spring-context-support-3.2.1.RELEASE.jar
spring-core-3.2.1.RELEASE.jar
spring-expression-3.2.1.RELEASE.jar
spring-jdbc-3.2.1.RELEASE.jar
spring-test-3.2.1.RELEASE.jar
spring-tx-3.2.1.RELEASE.jar
myBatis所需Jar
mybatis-3.2.0.jar
mybatis-spring-1.2.0.jar
以及其他資料庫關聯jaroracle資料庫)
ojdbc6-1.0.jar
將以上包匯入工程中。
在conf中建立spring的設定檔applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- 配置DataSource資料來源 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="initialSize" value="${db.initialSize}" />
<property name="maxActive" value="${db.maxActive}" />
<property name="validationQuery" value="${db.validationQuery}" />
<!--<property name="defaultAutoCommit" value="${db.defaultAutoCommit}"></property>
--></bean>
<!-- 配置交易管理員,注意這裡的dataSource和SqlSessionFactoryBean的dataSource要一致,不然事務就沒有作用了 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 建立SqlSessionFactory,同時指定資料來源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
-->
</bean>
<!-- 配置事務的傳播特性 -->
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="edit*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="cancel*">PROPAGATION_REQUIRED</prop>
<prop key="*">readOnly</prop>
</props>
</property>
</bean>
</beans>
建立jdbc.properties
db.driver=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:@localhost:1521:orcl
db.username=
db.password=
db.alias=OraclePool
db.minIdle=5
db.maxIdle=10
db.maxWait=5
db.maxActive=20
db.initialSize=10
db.logWriter=
db.validationQuery=SELECT 1 FROM DUAL
建立mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 別名 -->
<typeAliases>
</typeAliases>
</configuration>
至此所有配置都完成,下面接著就是後台代碼如何整合
本文出自 “Java” 部落格,轉載請與作者聯絡!