整合后文件

配置文件:
1. spring与mybatis配置文件:applicatonContext.xml
- 导入数据库配置文件
- 配置数据源信息
- 配置事务管理器
- 开启事务注解
- 配置 mybatis 的 sqlSessionFactoryBean
- 配置 mapper 扫描器
- 扫描 service 文件

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xmlns:tx="http://www.springframework.org/schema/tx"
7 xsi:schemaLocation="http://www.springframework.org/schema/beans
8 http://www.springframework.org/schema/beans/spring-beans.xsd
9 http://www.springframework.org/schema/aop
10 http://www.springframework.org/schema/aop/spring-aop.xsd
11 http://www.springframework.org/schema/tx
12 http://www.springframework.org/schema/tx/spring-tx.xsd
13 http://www.springframework.org/schema/context
14 http://www.springframework.org/schema/context/spring-context.xsd">
15 <!-- 导入数据库配置文件-->
16 <context:property-placeholder location="classpath:config/db.properties"/>
17 <!-- 配置数据源-->
18 <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
19 <property name="driverClassName" value="${jdbc.driver}"/>
20 <property name="url" value="${jdbc.url}"/>
21 <property name="username" value="${jdbc.username}"/>
22 <property name="password" value="${jdbc.password}"/>
23 <property name="maxTotal" value="${jdbc.maxTotal}"/>
24 <property name="maxIdle" value="${jdbc.maxIdle}"/>
25 <property name="initialSize" value="${jdbc.initialSize}"/>
26 </bean>
27 <!-- 事务管理器-->
28 <bean id="transactionManager"
29 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
30 <property name="dataSource" ref="dataSource"/>
31 </bean>
32 <!-- 开启事务注解-->
33 <tx:annotation-driven/>
34 <!-- 配置 Mybatis 的 sqlSessionFactoryBean-->
35 <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
36 <property name="dataSource" ref="dataSource"/>
37 <property name="configLocation" value="classpath:config/mybatis-config.xml"/>
38 </bean>
39 <!-- 配置 mapper 扫描器-->
40 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
41 <property name="basePackage" value="com.zhh.dao"/>
42 </bean>
43 <!-- 扫描 service-->
44 <context:component-scan base-package="com.zhh.service"/>
45 </beans>
2. springMVC 配置文件:springMVC-config.xml
- 包扫描,扫描 controller 注解
- 加载注解qvdong
- 配置视图扫描器,配置前缀后缀

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/tx 10 http://www.springframework.org/schema/tx/spring-tx.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context.xsd 13 http://www.springframework.org/schema/mvc 14 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 15 <!-- 包扫描器,扫描 controller 注解--> 16 <context:component-scan base-package="com.zhh.controller"/> 17 <!-- 加载注解驱动--> 18 <mvc:annotation-driven/> 19 <!-- 配置视图解析器--> 20 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 21 <property name="prefix" value="/WEB-INF/jsp/"/> 22 <property name="suffix" value=".jsp"/> 23 </bean> 24 </beans>
3. web.xml
- 配置 spring 文件监听器 org.springframework.web.context.ContextLoaderListener
- 添加编码过滤器 org.springframework.web.filter.CharacterEncodingFilter
- 配置springMVC控制器 org.springframework.web.servlet.DispatcherServlet

1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 5 version="4.0"> 6 <!-- 配置 spring 文件监听器--> 7 <context-param> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>classpath:config/applicationContext.xml</param-value> 10 </context-param> 11 <listener> 12 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 13 </listener> 14 <!-- 编码过滤器--> 15 <filter> 16 <filter-name>encoding</filter-name> 17 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 18 <init-param> 19 <param-name>encoding</param-name> 20 <param-value>UTF-8</param-value> 21 </init-param> 22 </filter> 23 <filter-mapping> 24 <filter-name>encoding</filter-name> 25 <url-pattern>*.action</url-pattern> 26 </filter-mapping> 27 <!-- 配置 springMVC 前端核心配置器--> 28 <servlet> 29 <servlet-name>springMVC</servlet-name> 30 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 31 <init-param> 32 <param-name>contextConfigLocation</param-name> 33 <param-value>classpath:config/springMVC-config.xml</param-value> 34 </init-param> 35 <!--服务器启动后立刻加载springMVC--> 36 <load-on-startup>1</load-on-startup> 37 </servlet> 38 <servlet-mapping> 39 <servlet-name>springMVC</servlet-name> 40 <url-pattern>/</url-pattern> 41 </servlet-mapping> 42 </web-app>
