搭建第一个SpringBoot项目
1.准备工作
- 1.新建 Spring Initializr 项目
- 2.选择 Spring Initializr
- 3.选择 SDK,点击【new】这里使用 JAVA SDK 要求版本 1.8+,选择你JDK安装的路径
- 4.然后选择默认的 url-> Next
- 5.填写一下项目的信息
- 6.根据需求勾选依赖
- 7.选择好项目的位置,->Finish
- 8.如果是第一次配置 Spring Boot 的话,需要等待一会儿 IDEA 下载相应的依赖包,默认创建好的项目结构如下:
SpringBoot项目,少了很多配置文件。默认生成的有:
SpringbootApplication: 一个带有 main() 方法的类,用于启动应用程序
SpringbootApplicationTests:一个空的 Junit 测试了,它加载了一个使用 Spring Boot 字典配置功能的 Spring 应用程序上下文
application.properties:一个空的 properties 文件,可以根据需要添加配置属性
pom.xml: Maven 构建说明文件
2.真正项目(HelloController)
- 1.在 main/java/com.sxy.springboot包下新建一个类HelloController
- 2.写简单的类内容
- 3.利用 IDEA 启动 Spring Boot(会提示maven导包,点击
Import Changes
) - 4.运行提示信息
我们之所以在上面的项目中没有手动的去配置 Tomcat 服务器,是因为 Spring Boot 内置了 Tomcat
附:
出错原因见另一篇文章运行springboot找不到数据源的问题
运行后我的demo出现了问题,提示如下:(没有遇到这个问题的宝宝直接跳过这一步)
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-09-27 09:14:13.402 ERROR 13440 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Process finished with exit code 1
- 5.运行结果(在浏览器的地址栏输入访问路径)