一个完整的Struts2.X+Spring3.X+MyBatis3.X配置搭建文档

做程序员很长时间了,但是却很少自己完整的搭建过开发框架,一般在公司做开发都是已经搭建好的框架,自己只是在里面添砖加瓦而已;这是第一次搭建一个SSM框架,仅把主要的配置文件复制出来,由于现在对maven还不是很了解,所以JAR包还是手动引入的,后期逐渐完善,引入现在流行的一些开源组件。第一个版本比较粗糙,只能当做一个可以运行的Demo实例而已。有需要的初学者可以交流下,提供源码。

WEB.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <context-param>
    <!-- 如果不定义webAppRootKey参数,
     那么webAppRootKey就是缺省的"webapp.root"。
     但最好设置,以免项目之间的名称冲突。
     -->
    <param-name>webAppRootKey</param-name>
    <param-value>FrameWork.root</param-value>
  </context-param>

  <!-- Spring 配置文件 -->
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
 
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 编码过滤 配置-->
  <filter>
    <filter-name>encodingFilter</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>
 
  <!-- Struts2 集成配置 -->
  <filter>
   <filter-name>struts2</filter-name>
   <filter-class>
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   </filter-class>
   <init-param>  
   <param-name>config</param-name>  
   <!-- <param-value>struts-default.xml,Struts2提供的默认配置,提供一些默认的拦截器
                struts.xml,
                ./conf/struts/auth-struts.xml,
                ./conf/struts/om-struts.xml,
                ./conf/struts/common-struts.xml,
                ./conf/struts/demo-struts.xml,
   </param-value> -->
   <param-value>
     struts-default.xml,
        struts.xml
   </param-value>     
  </init-param>
  </filter>

 <!-- 过滤器集合,与顺序有关 -->
  <filter-mapping>
   <filter-name>encodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>*.action</url-pattern>
  </filter-mapping>
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>*.jsp</url-pattern>
  </filter-mapping>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
</web-app>

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:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/jdbc
            http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

 <!--使用JNDI DataSource
 <bean id="it_dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
 <property name="jndiName">
   <value>jdbc/demoDB</value>
 </property>
 </bean> -->
 
 <!-- Spring配置DataSource 
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="${dataSource.driverClassName}"></property>
  <property name="url" value="${dataSource.url}"></property>
  <property name="username" value="${dataSource.username}"></property>
  <property name="password" value="${dataSource.password}"></property>
 </bean>-->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
   <property name="driverClassName" value="oracle.jdbc.OracleDriver" />  
   <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orclEOS" />
   <property name="username" value="eos"/>
   <property name="password" value="eos"/>
   <property name="initialSize" value="4"/>
   <property name="maxIdle" value="4"/>   
   <property name="minIdle" value="2"/>   
   <property name="maxActive" value="5"/>   
   <property name="removeAbandoned" value="true"/> 
   <property name="removeAbandonedTimeout" value="180"/> 
   <property name="maxWait" value="1000"/>
 </bean>
  
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="configLocation" value="/WEB-INF/mybatis-config.xml"/>
  <property name="dataSource" ref="dataSource"/>
 </bean>
 
 <!-- 数据连接管理 -->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
      <property name="dataSource" ref="dataSource"/> 
 </bean> 
 
 <!-- 事务控制 -->
 <tx:advice id="transactionControl" transaction-manager="transactionManager" >
  <tx:attributes>   
   <!-- 事务设置 propagation事务传播行为:默认REQUIRED;
       isolation 事务隔离级别  :默认DEFAULT
       timeout  事务超时时间   :默认-1
       read-only 事务是否只读   ;false
       rollback-for 触发回滚的异常:多个异常以逗号分开;Exception(s)
       no-rollback-for   不被触发进行回滚的Exceotion(s),多个异常以逗号隔开 -->
   <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />  
   <tx:method name="query*" read-only="true"/> 
   <tx:method name="select*" read-only="true"/> 
   <tx:method name="set*" read-only="true"/> 
   <tx:method name="get*" read-only="true"/>
  </tx:attributes>
 </tx:advice>
 
 <!-- Spring编程式事物  推荐不使用,产生耦合
 <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
      <property name="transactionManager">
        <ref bean="TransactionManager"/>
       </property>
 </bean>
 <bean id="courseService" class="com.test.CourseService">
    <property name="transactionTemplate">
       <ref bean="transactionTemplate"/>
     </property>
 </bean>-->
 
 <!--  添加子模块得配置
 <import resource="classpath:conf/spring/ac-config.xml"/>
 <import resource="classpath:conf/spring/log-config.xml"/>
 <import resource="classpath:conf/spring/demo.xml"/>
</beans>

mybatis-config.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>
<mappers>
<!-- 引入映射文件
  <mapper resource="QueryAuthInfoMapper.xml"/>-->     
  <mapper resource="conf/demo/demoMapper.xml"/>
</mappers>
</configuration>

 

你可能感兴趣的:(一个完整的Struts2.X+Spring3.X+MyBatis3.X配置搭建文档)