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

准备工作

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

在官网快速创建SpringBoot项目

下面开始进入正题:


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






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

使用 Maven 导入 Spring Boot 项目

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

出现下面这个直接 fix 掉,未出现不用理睬

Maven切换国内源

你会发现他那个依赖下载的特别慢
下一步,关掉它

然后重新打开
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";
}
}

项目结构如下



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

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





上图成功代表项目可以访问了,你成功的迈向 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

最新文章

  1. Electron使用与学习--(基本使用与菜单操作)
  2. RunTimeException包括:
  3. iOS应用之间跳转
  4. iOS 关于僵尸对象和僵尸指针的那些事儿
  5. NOI剑客决斗
  6. eclipse 项目修改和更新项目,回退版本,解决分支的冲突的办法
  7. OCJP考试介绍
  8. C#学习笔记之线程 - 通知Signal
  9. CI 笔记,使用 json的参考文档(废弃)
  10. PLSQL常用时间函数
  11. 打包mysql、tomcat、jdk为一个软件
  12. C#中ISpostback
  13. javascript 延时执行函数
  14. HDU 5963 博弈
  15. Unity Shader入门精要读书笔记(一)序章
  16. The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files
  17. 猴子分桃—Python
  18. Eclipse 无输出,但不报错
  19. NopCommerce 更改发票字体
  20. div锚点链接跳转

热门文章

  1. Autofac的切面编程实现
  2. windows服务程序的编写
  3. 蘑菇街CEO陈琪上市致辞:科技是生产力 美丽也是生产力
  4. 【10月新版】Aspose.Pdf 10月新版V17.10发布 | 附下载
  5. Top 命令数据分析
  6. Springboot-WebFlux实现http重定向到https
  7. 题目分享P
  8. Android 开发技术周报 Issue#278
  9. Java——运算符那些事
  10. K. Road Widening