需要三个文件,分别是 web.xml,springmvc的配置文件,spring和mybatis整合的配置文件
!!!为了便于管理这三个配置文件全部存放于Src目录下
①web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--Spring监听器,用于监听Tomcat启动-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--设置spring MVC配置文件位置-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 配置Spring和mybatis整合配置文件位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!--字符编码过滤器-->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
②spring mvc配置文件
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd ">
<!--配置启动spring MVC注解-->
<context:annotation-config />
<!-- 设置使用注解的类所在的包 -->
<context:component-scan base-package="controller"/>
<mvc:annotation-driven/>
<!--配置视图解析器,设置前缀与后缀-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value= ""/>
<property name="suffix" value= ".jsp"/>
</bean>
<!--将处理完请求后返回的对象绑定到响应正文,即将对象序列化为Json-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html; charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
</beans>
③spring和mybatis整合的配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="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/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!--需要从component的文件所在目录-->
<context:component-scan base-package="dao"/>
<!--数据源中的账户密码的properties文件位置-->
<context:property-placeholder location="classpath:database.properties" />
<!--初始化数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 初始化连接数量; -->
<property name="initialSize" value="0" />
<!-- 最大并发连接数 -->
<property name="maxActive" value="20" />
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="20" />
<!-- 最小空闲连接数 -->
<property name="minIdle" value="0" />
<!-- 最大等待时长 -->
<property name="maxWait" value="60000" />
<!-- 超过时间限制是否回收 -->
<property name="removeAbandoned" value="true" />
<!-- 超过时间限制多长; -->
<property name="removeAbandonedTimeout" value="180"/>
<!-- 数据源连接参数配置; -->
<property name="username" value="${db.username}"/>
<property name="url" value="${db.url}"/>
<property name="password" value="${db.password}"/>
<property name="driverClassName" value="${db.driver}"/>
</bean>
<!-- 配置SessionFactory 名字不能更改 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--配置mapper映射文件所在位置-->
<!-- <property name="mapperLocations" value="classpath:mapper/*.xml" /> -->
<!--<property name="mapperLocations">-->
<!--<list>-->
<!--<value>classpath:mapper/*.xml</value>-->
<!--</list>-->
<!--</property>-->
</bean>
<!-- 自动扫描mapper接口,注入sqlSessionFactory 设置mapper接口所在的文件夹-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> -->
<!-- 定义切面 -->
<aop:config>
<aop:pointcut expression="execution(* dao.*.* (..))" id="txPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
<!-- 声明式事务 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="isValid" propagation="REQUIRED" read-only="true"/>
<tx:method name="add" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>