spirng + spring-mvc + mybaits 幾乎已經成爲了java後端後端開發的最基本的組合。雖然如今你們都用springb-boot,可是spring-boot封裝過分,新手都不知道怎麼跑起來的,好比像我這樣的新手:(!java
打開IDEA -> Create New Projectweb
點擊next填好信息spring
添加spring-web-mvc依賴apache
<?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-mvcspring-mvc
先給項目添加web功能tomcat
點擊設置web所在目錄mybatis
目錄結構以下mvc
web.xml:app
<?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"; } }
目錄結構以下:
一個spring-mvc項目就已經搭建完成了,下面跑起來測試一下。
打開 http://localhost:8080
頁面顯示錶示咱們的第一個spring-mvc項目已經搭建成功