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_4_0.xsd"
version="4.0">
<!-- 搭建ss框架,我们搭建springmvc框架就相当于搭建spring框架,因为springmvc的框架实在spring的基础上-->
<!-- 2.配置一个监听器,在启动服务器的同时,就启动spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- 1.搭建springmvc框架,配置springmvc的核心入口-->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springmvc文件如果不设置初始化参数,那么默认就在web->web-inf->servlet-name+名字 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring.xml中的内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 告诉spring容器,底下这些类可能是要交给spring容器框架来管理的-->
<context:component-scan base-package=" com.duyi.controller,com.duyi.service,com.duyi.dao"></context:component-scan>
<!-- IOC AOP-->
<!-- sm整合就是将mybatis的数据源,工厂,事务交给spring管理-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<!-- 数据库的连接信息-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/testssm?characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
<!-- 数据库连接池信息-->
<property name="maxTotal" value="100"></property>
<property name="minIdle" value="5"></property>
<property name="maxWaitMillis" value="2000"></property>
</bean>
<!-- 配置工厂-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务信息-->
<bean id="manager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务的管理策略-->
<tx:advice id="txAdvice" transaction-manager="manager">
<tx:attributes>
<tx:method name="save*" isolation="READ_COMMITTED" propagation="REQUIRED"/>
<tx:method name="delete*" isolation="READ_COMMITTED" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
<tx:method name="update*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
</tx:attributes>
<!-- isolation 事务的隔离级别,propogation事务的传播行为-->
</tx:advice>
<!-- 事务管理时间点(切入点)-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution (* com.duyi.service.*.*(..))"/>
</aop:config>
</beans>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.dao.controller"></context:component-scan>
<!-- 开启请求与相应的注解编程,让他认识RequestMappering注解啥的-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>