1.下面为spring-core.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
">
<!-- 导入database -->
<context:property-placeholder location="classpath:database.properties"/>
<!-- 配置基础数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
<property name="url" value="${url}"/>
</bean>
<!-- 配置SqlSessionFactoryBean -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 导入mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 导入基础数据源 -->
<property name="dataSource" ref="dataSource" />
<!--配置mybatis 的mapper关联 -->
<property name="mapperLocations">
<list>
<value>classpath:cn/zy/dao/*.xml</value>
</list>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置增强类 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="REQUIRES_NEW"/>
</tx:attributes>
</tx:advice>
<!-- 切面aop -->
<aop:config>
<aop:pointcut expression="execution(* cn.zy.dao.*.*(..)))" id="pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
</beans>
2.下面为spring-dao.xml
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置mapper 接口所在的包 -->
<property name="basePackage" value="cn.zy.dao" />
<property name="sqlSessionFactoryBeanName" value="sessionFactory" />
</bean>
3.下面为spring-service.xml
<!-- 注解注入service -->
<context:component-scan base-package="cn.zy.service"/>
4.以下是spring-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
">
<!-- 扫描类 -->
<context:component-scan base-package="cn.zy.control" />
<!-- 扫描mvc -->
<!-- <mvc:annotation-driven /> -->
<mvc:annotation-driven>
<mvc:message-converters>
<!-- 原生ajax 中文编码处理 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8</value>
</list>
</property>
</bean>
<!-- fastjson 中文编码处理 -->
<bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8</value>
<value>application/json;charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:resources location="/resoource/" mapping="/resoource/**" />
<!-- 创建视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/resoource/**"/>
<mvc:exclude-mapping path="/WEB-INF/jsp/login.jsp"/>
<mvc:exclude-mapping path="/user/login.html"/>
<bean class="cn.zy.util.UserLoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>
下面为applicationContext.xml
<!--组合拆分文件-->
<import resource="spring-core.xml"/>
<import resource="spring-dao.xml"/>
<import resource="spring-service.xml"/>
下面为web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<display-name></display-name>
<!-- 启动spring 配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-core.xml,
classpath:spring-dao.xml,
classpath:spring-service.xml
</param-value>
</context-param>
<!-- 配置spring 监听器,让spring容器之间可以互相访问 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置mvc 拦截 -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
</welcome-file-list>
<!-- form 表单post 中文乱码 -->
<filter>
<filter-name>formPostData</filter-name>
<!-- CharacterEncodingFilter 过滤 -->
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 设置字符集 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!-- 强制启用以上配置的字符集设置 -->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>formPostData</filter-name>
<!-- 拦截所有页面请求(图片等不拦截) -->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>