【搭建spirng spring-mvc mybaits】一、搭建spring-mvc

一、介紹

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章