在网上也是搜索很多资料一点点学到现在。东西整合在一起,希望对各位有所帮助
一、准备工作:
1.先看下目录结构
2.需要的jar包
二、配置文件解读
配置文件有这么几个 web.xml,springMvc的配置文件(spring-mvc.xml,daoContext.xml),数据库属性文件mysql.properties,MyBatis的配置文件MyBatis-config.xml,MyBatis与数据库映射文件userMapper.xml。下面逐一讲解
1.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<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>
<welcome-file-list>
<welcome-file>/WEB-INF/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<init-param>
<description>加载spring-mvc文件夹下xml的配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc/*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<filter>是为了防止乱码加的
<servlet>中<init-param>是在加载文件夹spring-mvc文件夹下关于springMvc 配置的xml的配置文件
另外需要注意的是:servlet-name是随意的。但是如果没有<init-param>中加载xml这句话时,spring会自动找到test-servlet.xml文件。这时就需要spring配置文件名与其一致,否则会报错
2.spring-mvc有两个配置文件:spring-mvc.xml和daoContext.xml
(1)spring-mvc.xml 顾名思义是springMvc的配置文件
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 启用 spring mvc 注解 -->
<mvc:annotation-driven/>
<!-- 对com.yue下所有包下的类的注解进行扫描,并自动创建bean实例和装配bean -->
<context:component-scan base-package="com.yue"></context:component-scan>
<!-- 视图解析器,定义跳转的文件前后缀 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
<pre name="code" class="html">启用注解:当url中有deleteUser.do,则去文件中找@RequestMapping(value="deleteUser.do")这句话,并对应执行他的方法
视图解析器:java方法中return "main";则会去找“/WEB-INF/jsp/main.jsp”的文件。对应起来就是一个完整的请求返回过程
<pre name="code" class="html">(2)daoContext.xml中是一些与数据库相关的连接配置文件</p><p></p><pre name="code" class="html">
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframe