您现在的位置是:首页 > 文章详情

SpringBoot2.x快速入门指南(一)

日期:2020-05-07点击:594

SpringBoot2.x快速入门指南(一)

准备工作

  • IDE: IntelliJ IDEA 2020.3
  • Java环境 jdk1.8 

在官网快速创建SpringBoot项目


下面开始进入正题:


进入 https://start.spring.io/  生成一个初始项目


image.png


这里会下载一个zip的项目压缩包

使用 Maven 导入 Spring Boot 项目

demo.zip解压之后记得复制下demo文件夹放的路径
在此用的开发工具是IntelliJ IDEA
下面是导入流程:
IDEA里点击File -> Open -> 粘贴刚刚的demo文件夹路径 -> 找到  pom.xml  双击
-> Open as Peoject -> 等待 Maven 加载完就好,看不明白看下图
image.png
出现下面这个直接 fix 掉,未出现不用理睬
image.png


Maven切换国内源

你会发现他那个依赖下载的特别慢
要么你科学上网
下一步,关掉它
image.png
然后重新打开
pom.xml 文件底部增加

<repositories> <!--阿里云主仓库,代理了maven central和jcenter仓库--> <repository> <id>aliyun</id> <name>aliyun</name> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <!--阿里云代理Spring 官方仓库--> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://maven.aliyun.com/repository/spring</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <!--阿里云代理Spring 插件仓库--> <pluginRepository> <id>spring-plugin</id> <name>spring-plugin</name> <url>https://maven.aliyun.com/repository/spring-plugin</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories>

起飞,等待下载完成即可

SpringBoot项目启动

启动前准备
依据下图把 DemoApplication 启动类 移到包最外层
启动类相当于管理项目的负责人,你把他扔到与控制层同级肯定出错不是


目的是打开一个web应用,在 pom.xml 中 <dependencies> 下增加

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>


修改 BootDemoApplication.java 

package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /* * * description : 这里的@RestController相当于 @ResponseBody+@Controller * 使用@RestController 相当于使每个方法都加上了 @ResponseBody 注解 * @author: Robot * @date 0:03 2020/5/8 **/ @RestController @SpringBootApplication public class BootDemoApplication { public static void main(String[] args) { SpringApplication.run(BootDemoApplication.class, args); } /** * 这里的@GetMapping相当于@RequestMapping(value = "/hello", method = RequestMethod.GET) **/ @GetMapping("hello") public String test(){ return "i love java"; } }


项目结构如下


image.png
image.png

推荐调试模式启动,往后我们做项目也是

启动成功之后访问 http://localhost:8080/hello


image.png


上图成功代表项目可以访问了,你成功的迈向 Spring 第一步

配置application.yml


什么是yml?
YML文件格式是YAML (YAML Aint Markup Language)编写的文件格式,YAML是一种直观的能够被电脑识别的的数据数据序列化格式,并且容易被人类阅读,容易和脚本语言交互的,可以被支持YAML库的不同的编程语言程序导入,比如: C/C++, Ruby, Python, Java, Perl, C#, PHP等。


听不懂吧,其实我也看不明白
就是相当于xml,properties的配置文件,看的更直观,上代码吧还是

下述properties

spring.resources.locations= classpath:/templates

改为yml格式之后

spring: resources: static-locations: classpath:/templates


yml需要注意,冒号(:)后面要跟空格,第二级和第一级要在上下行用一个Tab的距离


application.yml

server: port: 8080 # 连接数据库的,需要存在可以使用的数据库,不然报错 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/dovis?characterEncoding=utf-8 username: root password: root

永久更新地址

https://www.yuque.com/ekko/spring/qqt7xd

原文链接:https://yq.aliyun.com/articles/759155
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章