创建第一个spring-boot项目
第一种(需要了解项目的):
创建一个maven项目
在pom中继承
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.12.RELEASE</version><relativePath/> </parent>
在pom中导入依赖
<!-- 声明依赖--><dependencies><!--web应用启动器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--测试启动器工具:集成所需依赖--> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies>
在导入插件
<build><plugins> <!-- 插件:打包成可执行的jar包--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
编写主程序
作用:启动springBoot应用
在man>java创建Application
@SpringBootApplication public class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);} }
编写表示层(controller)
创建HelloController
//表示层 @RestController @RequestMapping("/hello") public class HelloController {@RequestMapping("/hi")public String hi(){return "hello boot2";} }
在Application启动类启动访问:localhost:8080/hello/hi
继续还有就是配置文件这里以(yml格式)来说指定端口
在resouce下创建application.yml文件(严格要求空格,可重启应用8080将不能用)
server:port: 8081
在讲打包jar的插件在有和无的区别
无(使用maven带的打包,打包后默认位置在当前目录下):
输出一个:boot-hello-1.0.jar文件(内含源码,并不能运行,不过它可以被当作工具使用,即在别的地方需要用到该文件中某个功能时,将它导入当作工具使用)
有:会多生成一个boot-hello-1.0.jar.original的文件
内含:spring相关文件和在lib文件夹内集成所有jar包和依赖包括内置容器tomcat,jar包需要运行则在MANIFEST.MF文件中必须要有Main-Class
运行方法:
第二种(极速版):
在创建springboot项目勾选springweb模块就行
依赖、插件、主程序全部自动写好
将controllerCP过来跑起项目即可