1. 配置springmvc.xml
(1)扫描controller包
<?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:mvc="http://www.springframework.org/schema/mvc"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 包扫描 -->
<context:component-scan base-package="org.hgsf.controller"></context:component-scan>
<!-- 注解驱动 -->
<mvc:annotation-driven/>
<!--(3)配置视图解析器(可省略) -->
<!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
<bean id="defaultViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="3">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="contentType" value="text/html" />
<property name="prefix" value="/" /> //前缀
<property name="suffix" value=".jsp" /> //后缀
</bean>
</beans>
2.配置mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!—配置bean路径的扫描,org.hgsf.bean是路径-->
<typeAliases>
<package name="org.hgsf.bean"/>
</typeAliases>
</configuration>
3.配置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:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.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">
<!-- 扫描service -->
<context:component-scan base-package="org.hgsf.buss"></context:component-scan>
<!-- 配置c3p0数据库连接 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/student" />
<property name="user" value="root" />
<property name="password" value="123" />
</bean>
<!-- 配置bean工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:org/hgsf/mapper/*.xml"></property>
<property name="configLocation" value="classpath:mybatis_config.xml"></property>
</bean>
<!-- 扫描dao-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.hgsf.dao"></property>
</bean>
<!-- 需要文件上传功能时,启用以下配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxInMemorySize">
<value>1638400</value>
</property>
</bean>
</beans>
4.配置web.xml
(1)导入springmvc的配置文件,项目启动,加载web.xml.然后通过web.xml加载springmvc和spring配置文件
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMvc.xml</param-value> //springmvc配置文件路径
</init-param>
<load-on-startup>1</load-on-startup> //tomcat加载顺序
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name> // servlet-name上下保持一致
<url-pattern>*.do</url-pattern> //拦截的后缀
</servlet-mapping>
(2)导入spring的配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value> //spring配置文件名称
</context-param>
<!—监听器配置-->
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!—设置编码-->
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!—
设置过滤器-->
<filter>
<filter-name>userpower</filter-name>
<filter-class>org.hgsf.fiter.UserPower</filter-class>
</filter>
<filter-mapping>
<filter-name>userpower</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
5.建package
6.导入lib中的jar包