web.xml
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-mybatis.xml</param-value>
- </context-param>
- <servlet>
- <servlet-name>SpringMVC</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring-mvc.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- <async-supported>true</async-supported>
- </servlet>
spring-mybatis.xml
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${driver}" />
- <property name="url" value="${url}" />
- <property name="username" value="${username}" />
- <property name="password" value="${password}" />
- <!-- 初始化连接大小 -->
- <property name="initialSize" value="${initialSize}"></property>
- <!-- 连接池最大数量 -->
- <property name="maxActive" value="${maxActive}"></property>
- <!-- 连接池最大空闲 -->
- <property name="maxIdle" value="${maxIdle}"></property>
- <!-- 连接池最小空闲 -->
- <property name="minIdle" value="${minIdle}"></property>
- <!-- 获取连接最大等待时间 -->
- <property name="maxWait" value="${maxWait}"></property>
- </bean>
- <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <!-- 自动扫描mapping.xml文件 -->
- <property name="mapperLocations" value="classpath:com/cn/hnust/mapping/*.xml"></property>
- </bean>
- <!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->
- <aop:config proxy-target-class="true">
- <aop:pointcut id="serviceMethod"
- expression=" execution(* com.service..*(..))" />
- <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
- </aop:config>
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="*" />
- </tx:attributes>
- </tx:advice>
spring-mvc.xml
主要是自动扫描控制器,视图模式,注解的启动- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
- <!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
- p:viewClass="org.springframework.web.servlet.view.JstlView"
- p:prefix="/WEB-INF/jsp/"
- p:suffix=".jsp" />