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>