修改 web.xml 文件
配置字符编码拦截器
<filter>
<filter-name>characterEncodingFilter</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>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
添加spring配置文件位置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
添加spring的监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
配置SpringMVC文件位置
<!-- SprigMVC 配置 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化
默认情况下: /WEB-INF/<servlet-name>-servlet.xml
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- SpringMVC配置文件路径 -->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 启动时自动加载配置文件 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!-- 全匹配 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
配置 Sessin 过期时间
<!-- 配置 session 超时时间,单位分钟 -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
配置SpringMVC配置文件
注解扫描包
<!--SpringMVC只是控制网站跳转逻辑 -->
<!-- 只扫描控制器 com.zyxx 下的目录都会被扫描 -->
<context:component-scan base-package="com.zyxx"use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
添加注解
<!-- 注册注解配置 -->
<mvc:default-servlet-handler/>
<!--取消拦截静态页码 -->
<mvc:annotation-driven/>
配置视图解析方式
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
配置 springmvc 文件上传
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 200m -->
<property name="maxUploadSize" value="209715200"></property>
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 是否启动延迟加载提高性能 -->
<property name="resolveLazily" value="true"></property>
</bean>
修改spring的核心配置文件
1. 引入数据库配置文件的位置
<context:property-placeholder location="classpath:jdbc.properties" />
配置文件扫描
<context:component-scan base-package="com.lxit.dianshang.controller">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<context:component-scan base-package="com.lxit.dianshang.web.services.impl"></context:component-scan>
2.配置数据源
2.1 c3p0连接方式
<!-- 配置 c3p0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 队列中的最小连接数 -->
<property name="minPoolSize" value="15"></property>
<!-- 队列中的最大连接数 -->
<property name="maxPoolSize" value="25"></property>
<!-- 当连接耗尽时创建的连接数 -->
<property name="acquireIncrement" value="15"></property>
<!-- 等待时间 -->
<property name="checkoutTimeout" value="10000"></property>
<!-- 初始化连接数 -->
<property name="initialPoolSize" value="20"></property>
<!-- 最大空闲时间,超出时间连接将被丢弃 -->
<property name="maxIdleTime" value="${jdbc.maxIdleTime}"></property>
<!-- 每隔 60 秒检测空闲连接 -->
<property name="idleConnectionTestPeriod" value="60000"></property>
</bean>
2.1 dbcp连接方式
<!-- 配置 dbcp 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 队列中的最小等待数 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
<!-- 队列中的最大等待数 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
</bean>
2.1 Druid Data Source连接方式
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close">
<property name="url" value="jdbc:mysql://127.0.0.1:3306/ssi" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
3. 配置SessionFactory
<!-- Spring 整合 MyBatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描 Mapper 文件 -->
<property name="mapperLocations" value="classpath*:com/zyxx/activiti/portal/**/*Mapper.xml" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置 dao 接口路径 -->
<property name="basePackage" value="com.zyxx.activiti.portal.*.dao" />
</bean>
4. 配置事务
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
5. 配置事务通知
<!-- 事务的通知方式 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="search*" propagation="REQUIRED" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
6.切面
<!-- AOP 切面拦截事务 -->
<aop:config>
<!-- 事务入口( Service 的包路径) -->
<aop:pointcut id="transactionPointcut"
expression="execution(* com.zyxx.activiti.portal..service.*Service*.*(..))" />
<!-- 将事务通知与切入点组合 -->
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="txAdvice" />
</aop:config>
7.spring 注解扫描
<!-- 配置文件扫描 -->
<!-- 不包含 @Controller 注入 -->
<context:component-scan base-package="com.zyxx">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
8.配置各类bean
注意:Action的bean需要修改作用域,默认是单例模式,需要修改为每次请求进来重新创建Bean对象
scope="prototype"
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="cacheEnabled" value="true" />
<!-- 打印sql日志 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
</configuration>