Spring Boot | 使用IntelliJ IDEA 2020从零开始搭建一个springboot项目(详细+坑点)

简介

本文写给第一次使用IDEA搭建springboot项目的萌新们。

网上相关的教程也很多了,但自己在实际跟着操作的过程中还是碰到了一些问题,因此记录下来,希望能帮助到大家~

最后的Hello, world界面如下:

在这里插入图片描述

搭建环境

  • win 10
  • IntelliJ IDEA Ultimate 2020.2
  • jdk 1.8

操作步骤

前面基础的步骤都可以参考这篇博文(如果遇到什么问题再来看我的吧,可能可以帮助到你):

使用idea快速搭建一个springboot项目(遇到的小坑)

下面上自己傻瓜式详细步骤。

1.创建项目

打开我们的IDEA,新建项目:

在这里插入图片描述
在这里插入图片描述
项目配置基本默认的来就行,这里jdk选1.8即可:

在这里插入图片描述
注意这里的java选8:

在这里插入图片描述
选择Web里的Spring Web,其他的配置按自己需求来~

在这里插入图片描述
最后finish:

在这里插入图片描述
不出意外的话,之后建立好就是这样的亚子:

在这里插入图片描述

2.配置configuration

我很纳闷的是只有我新建好项目之后,这里没有自动给我配置好的吗(还是做教程的大佬们觉得没必要说qwq,直接就Run了)

在这里插入图片描述
所以我 要手动来操作一下。

打开文件目录,可以看到这里是不太吉祥的红色:

在这里插入图片描述
这个原因是没有设置我们这个demo2文件夹为source root,所以右键设置一下即可:

在这里插入图片描述
哇,这样设了之后咋这么多红色的波浪线啊!

在这里插入图片描述
不要慌,我们先run一下康康是啥情况~

在这个DemoApplication上右键Run一下:

在这里插入图片描述
顺便提一句,可以看到,Run了之后,上面的configuration就自动配好了(我怎么告诉你我还傻乎乎的自己配,没配出来qwq):

在这里插入图片描述

但是底下有报错:

在这里插入图片描述
那么就说明现在的项目还缺少依赖包,那么我们就来装一波~

3.下载Maven的Sources

右键pom.xml->Maven->Download Sources

在这里插入图片描述
底下就开始下载了:

在这里插入图片描述
也可以把Maven的工具窗口点出来,方便操作:

在这里插入图片描述
可以看到侧边栏:

在这里插入图片描述
就等它慢慢下载吧~~下载好之后可以看到多了一坨东东:

在这里插入图片描述

4.数据库配置

那么怀着激动的心情,我们再Run一下:

好像很正常的亚子?

在这里插入图片描述
不对,定睛一看:

在这里插入图片描述
OMG又有报错!!不要慌,我们先看下这个报错是啥:


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

其实这里的重点是DataSource!!

直接上这篇博客,写的非常清楚Resolving “Failed to Configure a DataSource” Error

By design, Spring Boot auto-configuration tries to configure the beans automatically based on the dependencies added to the classpath.
And, since we have the JPA dependency on our classpath, Spring Boot tries to automatically configure a JPA DataSource. The problem is, we haven’t given Spring the information it needs to perform the auto-configuration.
For example, we haven’t defined any JDBC connection properties, and we’ll need to do so when working with external databases like MySQL and MSSQL. On the other hand, we won’t face this issue with in-memory databases like H2 since they can create a data source without all this information.

记得之前创建项目的时候我们选了个MySQL Driver吗!Spring Boot喜欢自动把我们需要的环境都配置好,但是我们现在没有给它配置数据库的信息,所以出现了这个报错。

解决方案如下:

方法一:配置数据库信息

application.properties 中配置:

spring.datasource.url=jdbc:mysql://localhost:3306/myDb
spring.datasource.username=user1
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

或者在 application.yml 中配置:

spring:
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/myDb
    username: user1
    password: pass

方法二:取消自动配置数据库的功能

application.properties 中使用 spring.autoconfigure.exclude 属性取消 auto-configuration。

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

我就是用的这个方法(我是想着数据库配置啥的之后再说~)

在这里插入图片描述
在这里插入图片描述
或者在 application.yml 中写:

spring:
  autoconfigure:
    exclude:
    - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

以上方法任意选一种就可以。前面的博文里还有别的方法,我就没有列出来啦~

这样弄好了之后我们再Run一下看看:

在这里插入图片描述
感觉好像很正常的样子!

5.写个Hello, world网页

建立一个controller的文件夹:

在这里插入图片描述
建个java class,命名DemoController:

在这里插入图片描述
代码如下:

package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
public class DemoController {
    
    

    @RequestMapping("/index")
    public String index(){
    
    
        return "Hello, world!";
    }

}

那么最后再来Run一下:

在这里插入图片描述
这里圈出来的就是端口号~

访问网址 http://localhost:8080/demo/index,看到下面的页面就说明你成功啦~~)

在这里插入图片描述

一起秃头吧qwq

猜你喜欢

转载自blog.csdn.net/Encore47/article/details/109377497