1.tomcat启动,先加载web.xml文件的配置
服务器启动,要加载spring框架容器,springmvc框架容器,mybatis框架由spring整合,所以这里不用加载该框架容器
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- spring容器-->
<!-- 加载全局初始化参数,让服务器启动就加载spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring监听器 监听服务器启动, 即服务器启动创建spring容器-->
<listener>
<listener-class>org.springframework.web.context.ontextLoaderListener</listener-class>
</listener>
<!-- springmvc容器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 这个servlet加载的时候就会加载spring-mvc文件,创建spring-mvc容器-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- 让服务器启动就加载这个servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<!-- 缺省的路径 客户端访问服务器的时候,所有请求会经过这个servlet,由这个servlet将请求分发给其他servlet
当这里配置为 *.do 的时候 不用配置静态资源解析器 *.html *.css *.js *.png
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 字符编码过滤器,此配置只能解决Post请求中文乱码问题,Get请求的乱码问题在pom.xml中配置插件解决-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.spring的配置文件加载
在web.xml中配置了加载spring的配置文件 applicationContext.xml 会被加载,spring整合mybatis框架,可以将mybatis框架中的所有相关配置全部放到spring配置文件中来加载.(也可以单独给mybatis写配置文件)
applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--组件扫描 此处只扫描service层的包即可 因为dao层使用的是mapper接口,后面的配置会加载,扫描service包下的类,将对象创建权交给spring容器控制-->
<context:component-scan base-package = "service包名"/>
<!--配置数据库连接池-->
<!--加载properties配置文件-->
<context:properties-placeholder location="classpath:jdbc.properties">
<!--配置数据源信息-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置平台事务管理器,事务管理是要控制事务的提交与回滚,所以需要数据库连接池的connection对象来操作,所以需要配置一个数据源属性-->
<bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- service 需要事务控制的方法上 加 @Transactional 就可以了-->
<tx:annotaion-driver />
<!--配置sqlSessionFactory工厂对象,这里用的工厂不是mybatis提供的,是由springmvc框架单独提供的-->
<!--该配置目的是把sqlSession对象创建权交给spring,在service层实现类中,直接注入Mapper对象,直接调用方法操作数据库-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="实体类所在包,用来起别名"/>
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<!-- 配置查询时 下划线_驼峰映射 -->
<property name="mapUnderscoreToCamelCase" value="true"/>
</bean>
</property>
<!-- 配置分页插件 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">数据库方言类型(mysql|oracle)</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
<!-- 扫描mapper包,为mapper创建实现类-->
<!-- 原理是加载mapper包下的接口,然后根据接口名字找相同全限定名的xml文件 所以配置文件要和接口的全限定名相同-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.mapper"></property>
</bean>
3.spring-mvc的配置文件加载
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--组件扫描-->
<context:component-scan base-package="controller包名"/>
<!-- 配置mvc注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 内部资源视图解析器-->
<bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 开放静态资源访问权限
静态资源, 以下配置二选一,如果 DispatcherServlet配置的 *.do 那么下面的配置都不需要
-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>