今天闲来无事,想着从头开始搭建个SpringBoot的web项目,由于长时间没从头到尾搭建过,还是踩了不少的坑,下面来看看具体流程。
创建项目
懒省事儿,直接用spring官方提供的搭建网站如下
下载完毕以后犯了几个错误,总结如下:
1. 下载的时候选择了mybatis,但是实际使用的时候并未使用mybatis导致项目报错。
2. 因为需要搭建的是web项目,所以直接去中央仓库随便找了个SpringMVC的包,结果一直无法启动(SpringMVC的包怎么可能跟SpringBoot的包兼容呢,尴尬)
3. 项目启动时,发现端口是随机的,且无法修改,也是因为自己加错包了。
下面附上自己写错的XML
<dependencies>
<!-- <dependency>-->
<!-- <groupId>org.mybatis.spring.boot</groupId>-->
<!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!-- <version>2.1.0</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter</artifactId>-->
<!-- <version>2.1.7.RELEASE</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<!-- <dependency>-->
<!-- <groupId>org.springframework</groupId>-->
<!-- <artifactId>spring-webmvc</artifactId>-->
<!-- <version>5.1.9.RELEASE</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
</dependencies>
从上面注释的代码可以看出来我犯错的操作步骤,最初只是个mybatis的项目启动,之后发现缺少web组件,就随手加入了SpringMVC,结果发现还是无法启动,才又很认真的去找了SpringBoot的web启动。
spring-boot-starter-web中集成的有web组件以及内置了Tomcat,所以我们最终才能以一个jar包的方式来运行。
这个项目搭建下来,虽然没花费多久,可是犯错却不少,最主要的还是自己不够细心了,也希望大家引以为戒。
共勉,共同进步。