创建一个web项目

添加JAR

commons-dbcp2-2.9.0.jar
commons-logging-1.2.jar
commons-pool2-2.11.1.jar
log4j-1.2.17.jar
log4j-1.2.17-javadoc.jar
mybatis-3.5.8.jar
mybatis-spring-2.0.6.jar
mysql-connector-java-8.0.27.jar
spring-aop-5.3.14.jar
spring-beans-5.3.14.jar
spring-context-5.3.14.jar
spring-context-support-5.3.14.jar
spring-core-5.3.14.jar
spring-expression-5.3.14.jar
spring-jdbc-5.3.14.jar
spring-tx-5.3.14.jar
spring-web-5.3.14.jar

创建相关User表

Mybatis的Conf.xml可以不配置,全部交给Spring去管理

在src中创建applicationContext.xml

创建mapper,并写一个userMapper.xml

web.xml中引入applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
<!-- web项目中引入Spring -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

整合SM

整合SS

加入jar

spring-webmvc-5.3.14.jar

配置web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

<!-- 整合SpringMVC -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-controller.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

编写SpringMVC配置文件

applicationContext-controller.xml

1
2
3
4
5
6
7
8
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>