一、介绍

spirng + spring-mvc + mybaits 几乎已经成为了java后端后端开发的最基本的组合。虽然现在大家都用springb-boot,但是spring-boot封装过度,新手都不知道怎么跑起来的,比如像我这样的新手:(!

二、搭建项目

打开IDEA -> Create New Project

clipboard.png
点击next填好信息

clipboard.png

clipboard.png
添加spring-web-mvc依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>top.taoeer</groupId>
    <artifactId>spring-mvc-mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>
</project>

只添加一个spring-webmvc就行了,其它需要依赖spring-webmvc的依赖表里有,会自动导入的。
好了,目前为止开发web的依赖已经可以了。接下来我们先弄个hello world试一下。
为了让项目跑起来,我们还需要tomcat,为了简单起见,我这里采用tomcat7-maven-plugin这个tomcat maven的插件。tomcat7-maven-plugin需要将package设置为war。下面是当前的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>top.taoeer</groupId>
    <artifactId>spring-mvc-mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- tomcat7-maven-plugin需要将packaging设置为war -->
    <packaging>war</packaging>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <!-- 将tomcat的context设置为 / , 默认为当前项目名称 -->
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

接下来配置spring-mvc

先给项目添加web功能

clipboard.png

clipboard.png

点击设置web所在目录

clipboard.png

目录结构如下

clipboard.png
web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
          http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

在webapp/WEB-INF/下创建spring-mvc配置文件spring-servlet.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: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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <context:component-scan base-package="top.taoeer" />

    <mvc:annotation-driven />
</beans>

创建HomeController:

package top.taoeer.controllers;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {
    @GetMapping("/")
    public String home() {
        return "home page";
    }
}

目录结构如下:

clipboard.png

一个spring-mvc项目就已经搭建完成了,下面跑起来测试一下。

clipboard.png
打开 http://localhost:8080

页面显示表示我们的第一个spring-mvc项目已经搭建成功
clipboard.png


c1生不换
95 声望0 粉丝

The best way to build large things is to stitch small things together.