1、spring主配置文件
<?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" 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--第一步,掃描service--> <context:component-scan base-package="com.zmh.ssm.service.impl"></context:component-scan> <!--第二步,加載jdbc.properties--> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!--第三步,創建dbcp數據源連接池--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${user}"></property> <property name="password" value="${password}"></property> </bean> <!--第四步,創建mybatis的工廠類對象--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <!--指定數據源--> <property name="dataSource" ref="dataSource"></property> <!--加載mybatis的映射文件 在value中可以使用*號通配符--> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!--第五步,在spring的工廠中生成dao接口的實現類對象--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--指定要掃描哪個包下面所有的dao接口--> <property name="basePackage" value="com.zmh.ssm.dao"></property> </bean> <!--第六步,創建spring的事物管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--第七步,聲明以注解的方式配置聲明式事物--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> </beans>
2、spring-mvc主配置文件
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:mvc="http://www.springframework.org/schema/mvc" 6 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 7 <!--掃描控制器包--> 8 <context:component-scan base-package="com.zmh.ssm.controller"></context:component-scan> 9 <!--聲明以注解的方式配置spring mvc 相當於配置類 10 RequestMappingHandlerMapping 處理器映射器 11 RequestMappingHandlerAdapter 處理器適配器 12 --> 13 <mvc:annotation-driven></mvc:annotation-driven> 14 <!--配置spring mvc默認的jsp視圖解析器--> 15 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 16 <!--配置返回視圖的前綴--> 17 <property name="prefix" value="/WEB-INF/jsp/"></property> 18 <!--配置返回視圖的后綴--> 19 <property name="suffix" value=".jsp"></property> 20 </bean> 21 </beans>
3、web.xml配置文件
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 <listener> 8 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 9 </listener> 10 <!--手動指定spring的主配置文件的位置和名稱--> 11 <context-param> 12 <param-name>contextConfigLocation</param-name> 13 <param-value>classpath:spring.xml</param-value> 14 </context-param> 15 <!--配置spring mvc的前端控制器--> 16 <servlet> 17 <servlet-name>spring-mvc</servlet-name> 18 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 19 <!--指定spring mvc的主配置文件的位置和名稱--> 20 <init-param> 21 <param-name>contextConfigLocation</param-name> 22 <param-value>classpath:spring-mvc.xml</param-value> 23 </init-param> 24 </servlet> 25 26 <servlet-mapping> 27 <servlet-name>spring-mvc</servlet-name> 28 <url-pattern>*.do</url-pattern> 29 </servlet-mapping> 30 <!--配置編碼過濾器,解決post請求中文亂碼問題--> 31 <filter> 32 <filter-name>characterEncodingFilter</filter-name> 33 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 34 <init-param> 35 <param-name>encoding</param-name> 36 <param-value>UTF-8</param-value> 37 </init-param> 38 <init-param> 39 <param-name>forceResponseEncoding</param-name> 40 <param-value>true</param-value> 41 </init-param> 42 </filter> 43 <filter-mapping> 44 <filter-name>characterEncodingFilter</filter-name> 45 <url-pattern>/*</url-pattern> 46 </filter-mapping> 47 </web-app>