搭建spring+mybatis+struts2环境的配置文件

1、web.xml配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>mgr</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   
13    
14    <context-param>
15   <param-name>contextConfigLocation</param-name>
16   <param-value>
17    classpath:config/spring_config.xml<!-- spring整合配置 -->
18    classpath:config/spring_quartz.xml<!-- 配置定时任务时使用 -->
19   </param-value>
20  </context-param>
21  <!-- 设置由Sprng载入的Log4j配置文件位置,默认为src下,如果修改配置文件路径则需要修改 -->
22 <!--  <context-param>
23   <param-name>log4jConfigLocation</param-name>
24   <param-value>classpath:config/log4j.properties</param-value>
25  </context-param>
26  Spring刷新Log4j配置文件变动的间隔,单位为毫秒
27  <context-param>
28   <param-name>log4jRefreshInterval</param-name>
29   <param-value>10000</param-value>
30  </context-param>
31  <listener>
32   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
33  </listener> -->
34  
35  <!-- Spring监听器 -->
36  <listener>
37   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
38  </listener>
39  <listener>
40   <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
41  </listener>
42  
43   
44  <!-- Struts2 -->
45  <filter>
46   <filter-name>struts2</filter-name>
47   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
48   <!-- 默认strut.xml放在src目录下,如需修改,需要增加init配置 -->
49  </filter>
50  <filter-mapping>
51   <filter-name>struts2</filter-name>
52   <url-pattern>/*</url-pattern>
53  </filter-mapping>
54  
55  <session-config>
56   <session-timeout>30</session-timeout>
57  </session-config>
58  
59 </web-app>

2、spring_config.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
    
    <!-- 加载配置文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config/jdbc.properties</value>
                <value>classpath:config/mail.properties</value>
            </list>
        </property>
    </bean>
    
    
    <!-- 1、配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

        <!-- 配置基本属性 -->
        <property name="driverClassName" value="${jdbc.mysql.driverclassname}" />
        <property name="url" value="${jdbc.mysql.url}" />
        <property name="username" value="${jdbc.mysql.username}" />
        <property name="password" value="${jdbc.mysql.password}" />

        <!-- 配置连接初始化大小、最小、最大 -->
        <property name="initialSize" value="5" />
        <property name="minIdle" value="5" />
        <property name="maxActive" value="20" />

        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="60000" />

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="3600000" />

        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="3600000" />
        
        <!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量-->
        <property name="numTestsPerEvictionRun" value="3" />

        <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
        <property name="poolPreparedStatements" value="true" />

        <!-- 用来检测连接是否有效的sql,要求是一个查询语句。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用 -->
        <property name="validationQuery" value="SELECT 1" />

        <!-- 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效 -->
        <property name="testWhileIdle" value="true" />

        <!-- 申请连接时执行validationQuery检测连接是否有效,设置为false,默认是true,value值为true时候配置会降低性能7-10倍 -->
        <property name="testOnBorrow" value="false" />

        <!-- 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 -->
        <property name="testOnReturn" value="false" />
        
        <!-- 连接泄漏回收参数,当可用连接数少于3个时才执行 -->
        <property name="removeAbandoned" value="true" />
        
        <!-- 连接泄漏回收参数,180秒,泄露的连接可以被删除的超时值 -->
        <property name="removeAbandonedTimeout" value="180" />
        

        <!-- 配置监控统计拦截的filters -->
<!--         <property name="filters" value="slf4j" /> -->
    </bean>
    
    
    <!-- 2、配置sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" scope="prototype" >
        <!-- 加载数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 加载MyBatis配置文件 -->
        <property name="configLocation" value="classpath:config/mybatis_config.xml"></property>
        <!-- 加载mapper,存放sql的xml的路径-->
        <property name="mapperLocations" value="classpath*:com/develop/**/dao/mapper/*Mapper.xml" /><!-- Mapper.xml文件中的namespace的值是dao接口的全路径类  -->
        
        <property name="plugins">
            <array>
                <bean class="com.develop.core.page.github.pagehelper.PageHelper">
                    <property name="properties">
                        <value>
                            dialect=mysql
                            reasonable=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
    
    
    <!-- 3、通过模板定制mybatis的行为 -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
        <!--更新采用单个模式 -->
        <constructor-arg index="1" value="SIMPLE"/>
        <!--更新采用批量模式 -->
        <!-- <constructor-arg index="1" value="BATCH"/> -->
    </bean>

    <!-- 4、采用自动扫描方式创建mapper(dao) bean -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.develop.*.*.dao" />
        <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate" />
    </bean> 

    
    <!-- 5、加载Service组件 ,需要在Service的实现类Impl上方添加注解@Service,否则会报异常-->
    <context:component-scan  base-package="com.develop.*.*.service.impl"></context:component-scan>
    
    <!-- 6、配置事务管理器 -->
    <!-- MyBatis默认使用Spring JDBC的事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 7、拦截器方式配置事物 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED"  rollback-for="Exception" />
            <tx:method name="update*" propagation="REQUIRED"  rollback-for="Exception" />
            <tx:method name="delete*" propagation="REQUIRED"  rollback-for="Exception" />
            <tx:method name="save*" propagation="REQUIRED"  rollback-for="Exception" />
            <tx:method name="add*" propagation="REQUIRED"  rollback-for="Exception" />
            <tx:method name="append*" propagation="REQUIRED"  rollback-for="Exception" />
            <tx:method name="modify*" propagation="REQUIRED"  rollback-for="Exception" />
            <tx:method name="edit*" propagation="REQUIRED"  rollback-for="Exception" />
            <tx:method name="remove*" propagation="REQUIRED"  rollback-for="Exception" />
            <tx:method name="init" propagation="REQUIRED"  rollback-for="Exception" />
            <tx:method name="delAndInit" propagation="REQUIRED"  rollback-for="Exception" />

            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="load*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="search*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="datagrid*" propagation="SUPPORTS" read-only="true" />

            <tx:method name="*" propagation="REQUIRED"  rollback-for="Exception" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="transactionPointcut" expression="execution(* com.develop.*.*.service..*.*(..))" />
        <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
    </aop:config>
    
    <!-- 邮件 -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.host}" />
        <property name="defaultEncoding" value="UTF-8" />
        <!-- Uncomment if you need to authenticate with your SMTP Server -->
        <property name="username" value="${mail.username}" />
        <property name="password" value="${mail.password}" />
        <property name="javaMailProperties">
            <value>
                mail.smtp.auth=true
                mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
                mail.smtp.socketFactory.fallback=false
                mail.smtp.port=465
                mail.smtp.socketFactory.port=465
            </value>
        </property>
    </bean>
    
    <!-- Configure Velocity for sending e-mail -->
    <bean id="velocityEngine"
        class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="velocityProperties">
            <props>
                <prop key="resource.loader">class</prop>
                
                <prop key="class.resource.loader.class">
                    org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
                </prop>
                <prop key="velocimacro.library"></prop>
                <prop key="velocimacro.library.autoreload">true</prop>
                <prop key="file.resource.loader.cache">false</prop>
    

                  <prop key="input.encoding">UTF-8</prop>
                  <prop key="output.encoding">UTF-8</prop>
                  <prop key="default.contentType">text/html; charset=UTF-8</prop>                      
            </props>
        </property>
    </bean>
    
    <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage"
        scope="prototype">
        <property name="from" value="${mail.default.from}" />
    </bean>
    
    <bean id="mailEngine" class="com.develop.core.controller.MailEngine">
        <property name="mailSender" ref="mailSender" />
        <property name="velocityEngine" ref="velocityEngine" />
        <property name="from" value="${mail.default.from}" />
    </bean>
    
    <!-- SimpleUrlHandlerMapping -->
    <bean id="urlMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <value>
                /baseadmin/flushCache.html=filenameController
                /mainMenu.html=filenameController
                /index.html=filenameController
            </value>
        </property>
        <property name="order" value="0" />
    </bean>

    <!-- /baseadmin/planeModel.html=planeModelController /baseadmin/flightCourse.html=flightCourseController -->



    <!-- BeanNameUrlHandlerMapping -->
    <bean id="handlerMapping"
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
    </bean>
    <!-- 一个Controller多个方法请求映射配置 -->
    <bean name="methodNameResolver"
        class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="paramName">
            <value>method</value>
        </property>
    </bean>

    <bean
        class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
        <property name="order" value="1" />
    </bean>

    <!-- View Resolver for JSPs -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="requestContextAttribute" value="rc" />
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="useCodeAsDefaultMessage" value="true" />
    </bean>

    <bean id="filenameController"
        class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
    <!-- Add additional controller beans here -->

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="20971520" />
    </bean>
    
    <!-- 异常处理模块 -->
    <bean id="exceptionHandler" class="com.develop.core.controller.ApplicationExceptionHandler" />

</beans>

3、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>        

    

    <!-- 全局的参数配置,如开启二级缓存等 -->    

    <settings>

        <!-- 配置延迟加载,按需加载对象属性 -->

        <setting name="lazyLoadingEnabled" value="true"/>  

        <setting name="aggressiveLazyLoading" value="false"/> 

    </settings>

    

    <!-- 使用别名 -->

    <typeAliases>

        <!-- 为包下的所有文件设置别名,别名为类名,不分大小写 -->

        <package name="com.develop.**.model"/>

    </typeAliases>

    

</configuration>

4、struts.xml配置,默认放在src下,如果修改需要修改web.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>
<!--  <constant name="struts.action.extension" value="action" />
 <constant name="struts.i18n.encoding" value="UTF-8" />
 <constant name="struts.devMode" value="true" />
 <constant name="struts.multipart.maxSize" value="52428800" />
 <constant name="struts.enable.SlashesInActionNames" value="true" />
 <constant name="struts.enable.DynamicMethodInvocation" value="false" />
 <constant name="struts.custom.i18n.resources" value="resource/message_zh_CN" /> -->
 
 <!-- 请求格式:namespace/action
  package:包
   * name:包名,唯一的,必选项
   * namespace:命名空间,唯一的,相当于房间号。可选项,省略情况下是"/"。页面中请求连接的前半部分
   * extends:继承
   * extends="struts-default":struts2框架底层提供的核心包struts2-core-2.3.3.jar下的struts-default.xml文件
   * 为什么要继承这个struts-default.xml文件?
   * 因为struts2框架底层提供的struts-default.xml声明了所有的拦截器和拦截器栈,
    知道在struts2框架运行时执行struts-default.xml文件中的拦截器栈。
   * 如果不继承struts-default.xml文件,就没有办法使用struts2框架提供的所有拦截器
  -->
 <package name="core" namespace="/core" extends="struts-default">
<!--   <interceptors>
   <interceptor name="trimInterceptor" class="com.ym.crm.interceptor.TrimInterceptor" />
   <interceptor name="applicationInterceptor" class="com.ym.crm.interceptor.ApplicationInterceptor" />
   <interceptor name="loginInterceptor" class="com.ym.crm.interceptor.LoginInterceptor" />
   <interceptor-stack name="baseStack">
    <interceptor-ref name="defaultStack" />
    <interceptor-ref name="trimInterceptor" />
   </interceptor-stack>
  </interceptors> -->
<!--   <global-results>
   <result name="root" type="redirectAction">/common/error.action</result>
   <result name="rootRuntime" type="redirectAction">/common/error.action</result>
  </global-results> -->
<!--   <action name="*/*" method="{1}" class="com.develop.core.{2}Action">
   <exception-mapping result="root" exception="java.lang.Exception" />
   <exception-mapping result="rootRuntime" exception="java.lang.RuntimeException" />
   <interceptor-ref name="baseStack"></interceptor-ref>
   <result name="success" >/WEB-INF/core/{1}.jsp</result>
   <result name="error" >/WEB-INF/error.jsp</result>
  </action> -->
  
  <action name="*_*" class="com.develop.core.action.{1}Action" method="{2}">
   <result name="success" >/WEB-INF/page/core/{1}_{2}.jsp</result>
   <result name="error" >/WEB-INF/page/common/error.jsp</result>
  </action>
  
  
 </package>
</struts>

 

posted @ 2017-08-20 23:32  向前爬的蜗牛  阅读(473)  评论(0编辑  收藏  举报