如何用Spring Boot 写出helloworld

Spring Boot Controller

如何用Spring Boot 写出helloworld

效果如下

image-20220711130552554

准备条件: idea2020.0.4 和 jdk8u安装完毕

image-20220711121310688

安装完后打开idea

我的勇士,看到桌面的图标的了吗,双击打开它,

image-20220711121150005

打开之后,也许会出现已经打开的项目,我的勇士,单机File ,新建它,

image-20220711121600855

看到New 点击New

image-20220711121649142

看到New Project,点击 New Project

image-20220711121757157

点击 New Project 后会弹出,新建项目面板 ,让我们选择 我们的英雄(项目)

image-20220711121922943

选择 Spring Initializr

image-20220711122119222

点击 Next 后,选择 Java Version (Java 版本)为 8

image-20220711122209604

选择 我们需要的插件 ,

Developer Tools 里面的Lombok ,

Web 里面的 Spring Web ,

SQL 里面的 MyBatis Framework 和 Mysql Driver

image-20220711122520296

选择完成后 点击 Next

image-20220711122926237

自动帮你的project Name (项目名称改为 demo),我们可以更改为我们自己的项目名称为SpringBootController,完成后,点击 Next

image-20220711123135458

开始了自动化创建,创建完成后,应该是这个样子的

image-20220711123350583

ok,这个样子之后,我们点击三角形启动项目,会发现报错,原因是没有链接数据库

image-20220711123822001

这里为了方便,我们直接下载集成数据库,PHPStudy 来快速链接

这是 PHPstudy安装后打开的样子

image-20220711124042023

我们直接点击WNMP 就能启动,

image-20220711124145086

启动完毕,我们回到我们的idea2020.3.4,在application.properties中添加数据库信息。

spring.datasource.url=jdbc:mysql://localhost:3306/
spring.datasource.username=root
spring.datasource.password=TS1374206028
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

添加完成后,再次点击三角形启动

image-20220711124615756

这是启动成功的界面,端口号是8080,我们访问

http://localhost:8080/

image-20220711124758774

访问后就会发现,有页面

image-20220711125101260

最后 ,我们给它写上controller控制器。打开DemoApplication.java添加两个注解

image-20220711125924761

在类DemoApplication上面添加 @RestController 注解

在main()方法是上面添加index()方法,index()方法上面添加@RequestMapping("/")注解

加了两个注解和一个index()方法后应该是这样的

image-20220711130258532

最后,启动项目,访问8080端口

http://localhost:8080/

这是最后效果

image-20220711130505690

posted @ 2022-07-11 13:47  scwelcome  阅读(60)  评论(0)    收藏  举报