这两天搭建了个spring+mybatis+struts2的环境,也碰到了很多问题,不过都解决掉了,最后测试没有问题了,希望能帮到用到的朋友。
1、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>mgr</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/spring_config.xml<!-- spring整合配置 -->
classpath:config/spring_quartz.xml<!-- 配置定时任务时使用 -->
</param-value>
</context-param>
<!-- 设置由Sprng载入的Log4j配置文件位置,默认为src下,如果修改配置文件路径则需要修改 -->
<!-- <context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:config/log4j.properties</param-value>
</context-param>
Spring刷新Log4j配置文件变动的间隔,单位为毫秒
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>10000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener> -->
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- Struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- 默认strut.xml放在src目录下,如需修改,需要增加init配置 -->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</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="3" />
<property name="minIdle" value="3" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="false" />
<!-- 用来检测连接是否有效的sql,要求是一个查询语句。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用 -->
<property name="validationQuery" value="SELECT 1" />
<!-- 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效 -->
<property name="testWhileIdle" value="true" />
<!-- 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 -->
<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接口的全路径类 -->
</bean>
<!-- 3.1、通过模板定制mybatis的行为 ,单个-->
<bean id="sqlSessionTemplateSimple" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
<!--更新采用单个模式 -->
<constructor-arg index="1" value="SIMPLE"/>
</bean>
<!-- 3.2、通过模板定制mybatis的行为,批量 -->
<bean id="sqlSessionTemplateBatch" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
<!--更新采用批量模式 -->
<constructor-arg index="1" value="BATCH"/>
</bean>
<!-- 4.1、采用自动扫描方式创建mapper(dao) bean(单个更新模式) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.develop.**.dao" />
<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplateSimple" />
<!-- <property name="markerInterface" value="com.xxx.dao.SimpleDao" /> 指定单个dao和basePackage都写的话取并集-->
</bean>
<!-- 4.2、采用自动扫描方式创建mapper(dao) bean(批量更新模式) -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.develop.**.dao" />
<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplateBatch" />
<!-- <property name="markerInterface" value="com.xxx.dao.BatchDao" /> 指定单个dao和basePackage都写的话取并集-->
</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="add*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="append*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="update*" 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="delete*" 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="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.*..*.*(..))" />
<!-- 第一个 * —— 通配 任意返回值类型 -->
<!-- 第二个 ** —— 通配 包com.develop包下的任意包 -->
<!-- 第四个 * —— 通配 包service包下的任意包 -->
<!-- 第五个 * —— 通配 包com.develop.**.service.*下的任意class -->
<!-- 第六个 * —— 通配 包com.develop.**.service.*下的任意class的任意方法 -->
<!-- (..)中.. —— 通配 方法可以有0个或多个参数 -->
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>
<!-- 本项目的Action交给struts2处理,暂不配置 -->
</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>
配置好测试通过,未出现异常。