学习内容:基础入门HelloWorld
1.下载maven
2.修改maven-settings.xml文件
-
修改镜像文件、仓库位置(自己设置和创建)
3.参考官方文档
-
Developing Your First Spring Boot Application
-
Creating the POM
4.创建一个项目maven用idea
-
setting for New Projects
-
Build, Execution,Peloyment
-
Build Tools
-
Maven查看配置
-
maven home directory(用自己的地址)
-
user usettings file(自己文件的位置)
-
用自己的仓库位置
5.创建好后点击Enable Auto-Import,目的:使idea自动导入
6.在pom.xml文件中把父工程parent导入文件里面(固定写法)
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <!-- <version>3.2.2</version> --> <version>2.1.8.RELEASE</version> </parent>
-
注意依赖包的版本不要过高,如果配置的版本过低会报错
7.在pom.xml文件中添加系统依赖来开发web场景
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
-
Writing the Code
-
在 MainApplication.java 文件中编写主程序
-
主程序类@SpringBootApplication 目的表明这是一个SpringBoot应用
-
-
SpringApplication.run(MainApplication.class,args);//固定写法
-
新建包Contoller,新建文件HelloContoller
-
标注一个注解@Contoller
-
@RequestMapping("/hello")用来映射hello请求
-
@ResponseBody表明返回的话以字符串方式写给浏览器
-
@ResponseBody写在类上代表这个类的每一个方法它的数据直接写给浏览器而不是跳转某一个画面
-
@RestController是 @ResponseBody 和@Controller的合体
-
@RestController public class HelloContoller { @RequestMapping("/hello") public String handle01(){ return "Hello,Spring Boot 2!"; } }
9.运行程序:只需要在主类中运行main方法
10.在浏览器访问localhost:8080/hello
11.简化配置
-
在resources下新建application.properties文件
-
server.port=8888 目的修改服务器端口号
12.简化部署
在pom.xml文件中,添加jar包插件
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
-
运用插件使用打包把SpringBoot应用中的clean、package打包
-
target右键点击show in exporler 在路径后输入cmd回车
-
注意事项:
-
取消cmd的快速编辑模式
-
配置好网络,刷新
-