步骤一:
使用eclipse建立一个动态的java web项目,导入spring依赖的jar包,配置web.xml
1)<!-- 配置ContextLoaderListener 监听器 -->
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 作用:ContextLoaderListener的作用就是启动Web容器时,自动装配applicationContext的配置信息. 因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法 -->
2)<!-- 声明与注册一个servlet(类型为DispatcherServlet作为前端控制器,并且初始化路径) -->
<servlet>
<!--配置SpringMVC的前端控制器 -->
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springmvc的核心配置文件所在位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<!-- 服务器启动的时候最先加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 为DispatcherServlet建立映射 -->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3) <!-- 注册一个过滤器filter(编码过滤器),解决乱码问题 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!-- 查找资源jar:spring-web-4.2.0.RELEASE.jar/倒数第10个目录 -->
<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>
步骤二:配置applicationContex
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
">
<!-- 使用注解来配置管理的类 -->
<context:annotation-config/>
<!-- 指定扫描的包 -->
<context:component-scan base-package="com.wxg" />
<!-- 配置数据源,配置了数据库连接池,则可以删除mybatis-config.xml-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/superdelivery" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>
<!-- 让Spring容器帮我们创建SqlSessionFactory对象 ,配置它则可以删除我们自己编写SessionUtils类 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource" p:typeAliasesPackage="com.wxg.pojo" >
</bean>
<!-- 去扫描映射文件 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wxg.mapper" /> <!-- mapper映射文件所在的包 -->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 配置JDBC事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
</beans>
步骤三:配置springmvc-config.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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
">
<!-- 指定Controller所在的包 -->
<context:component-scan base-package="com.wxg.controller" />
<!-- 声明使用注解来配置Controller -->
<mvc:annotation-driven />
<!-- 设置默认的Servlet响应静态的资源 -->
<mvc:default-servlet-handler />
<mvc:resources location="/WebCotent/css/" mapping="/css/**" />
<mvc:resources location="/WebCotent/js/" mapping="/js/**" />
<mvc:resources location="/WebCotent/img/" mapping="/img/**" />
<!-- 配置文件上传的处理器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10000000000" />
</bean>
</beans>