新版idea创建spring boot项目

目录

前言

汉化教程

项目模板初始化

1.点击新建项目

2.配置初始化信息

3.初始依赖选择

配置Maven

1.打开maven设置

2.重写maven配置文件

3.选择你创建的配置文件

4.重启项目

[spring boot配置并测试](#spring boot配置并测试)

1.修改配置文件后缀

2.启动项目

3.编写测试控制类

4.重启项目测试

保底策略

1.git方式获取

2.下载压缩包方式获取


前言

本教程对新手小白友好。若根据教程创建出现问题导致失败可下载我提供的源码,在文章最后。

本教程较新

本文使用的工具以及搭建的springboot版本都是很新版本:

idea版本如下

spring boot 版本如下:

本教程使用的是汉化版的idea

汉化教程

File->Settings

搜索:plugins

选择插件市场,搜索chinese安装

下载完毕重启即可。

或者:

项目模板初始化

1.点击新建项目

或者

2.配置初始化信息

这里提一嘴的是,在第7步,java版本选择上我的建议:java 8、Java 11、Java 17三个长期支持版

原因是开发商会对其提供长期支持服务,包括修复漏洞、解决问题和提供更新等。

spring boot 2x版本建议使用Java 8、Java 11

spring boot 3x版本最低要求 Java17

我创建的spring boot 3x版本所以选Java17

最后第八步打包方式一定选择jar包。原因是,Spring Boot内置了Tomcat等Web服务器的支持,并提供了嵌入式容器的功能。这意味着你可以将整个应用程序以可执行的JAR文件的形式进行部署和运行,而无需外部的独立Web服务器。

点击下一步

如果你的idea版本较老可能没有我这个3x版本选择,你可以选择2x版本,然后回到上一步,jdk换成8或11。

3.初始依赖选择

选择几个常用初始依赖

选择好初始依赖点击创建,此时会去该spring官网下载初始化模板,稍等即可。

也可以去spring官网初始化模板并下载:Spring Initializrhttps://start.spring.io/

初始化完成如图:

配置Maven

此时需要配置以下maven下载源为国内阿里云镜像,加速依赖下载

1.打开maven设置

展开主菜单->文件->设置->

输入maven搜索

2.重写maven配置文件

这里我不推荐通过maven目录的conf下去直接修改setting.xml方式去切换下载源以及java版本。

我们只需要提前准备好setting.xml即可。

新建一个txt ->打开文件粘贴阿里云镜像源配置内容 ->修改文件名为setting.xml

粘贴如下:

我的java版本是17

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 
  <pluginGroups>
   
  </pluginGroups>

  <proxies>
    
  </proxies>

  
  <servers>
   
  </servers>

  
  <mirrors>
  <mirror>
    <id>alimaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
    
  </mirrors>

  <profiles>
  <profile>     
    <id>JDK-17</id>       
    <activation>       
        <activeByDefault>true</activeByDefault>       
        <jdk>17</jdk>       
    </activation>       
    <properties>       
        <maven.compiler.source>17</maven.compiler.source>       
        <maven.compiler.target>17</maven.compiler.target>       
        <maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>       
    </properties>       
</profile>
   
  </profiles>

</settings>

如果你是Java11:

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 
  <pluginGroups>
   
  </pluginGroups>

  <proxies>
    
  </proxies>

  
  <servers>
   
  </servers>

  <mirrors>
  <mirror>
    <id>alimaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
    
  </mirrors>

  <profiles>
  <profile>     
    <id>JDK-11</id>       
    <activation>       
        <activeByDefault>true</activeByDefault>       
        <jdk>11</jdk>       
    </activation>       
    <properties>       
        <maven.compiler.source>11</maven.compiler.source>       
        <maven.compiler.target>11</maven.compiler.target>       
        <maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>       
    </properties>       
</profile>
   
  </profiles>

  
</settings>

如果你是Java8:

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 
  <pluginGroups>
   
  </pluginGroups>

  <proxies>
    
  </proxies>

  
  <servers>
   
  </servers>

  <mirrors>
  <mirror>
    <id>alimaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
    
  </mirrors>

  <profiles>
  <profile>     
    <id>JDK-8</id>       
    <activation>       
        <activeByDefault>true</activeByDefault>       
        <jdk>8</jdk>       
    </activation>       
    <properties>       
        <maven.compiler.source>8</maven.compiler.source>       
        <maven.compiler.target>8</maven.compiler.target>       
        <maven.compiler.compilerVersion>8</maven.compiler.compilerVersion>       
    </properties>       
</profile>
   
  </profiles>

  
</settings>

粘贴完ctrl+s保存退出。

修改文件名为:setting.xml

3.选择你创建的配置文件

4.重启项目

此时依赖会马上下载好。

spring boot配置并测试

1.修改配置文件后缀

application.properties ->application.yml

此时你的配置文件啥都没写,但是可以直接运行项目,spring boot遵循约定大于配置理念,已经提供好了一组默认配置,你可以按需修改配置。

2.启动项目

这两处都能启动

3.编写测试控制类

新建controller目录下新建TestController类

java 复制代码
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/hello")
    public String  test(){
        return "hello world";
    }
}

4.重启项目测试

重启完成浏览器地址栏输入:localhost:8080/test/hello

成功输出返回响应。

5.简单配置项目端口以及项目名称

application.yml:

java 复制代码
server:
  # 端口号
  port: 8888
spring:
  application:
    # 应用名称
    name: mijiu-app

保底策略

如果你参照该教程遇到问题,导致创建失败

可以自取我已经创建好的

蒾酒/springboot-demo (gitee.com)https://gitee.com/mi9688-wine/springboot-demo

1.git方式获取

代码地址:

java 复制代码
https://gitee.com/mi9688-wine/springboot-demo

克隆后先编译一下在运行

2.下载压缩包方式获取

下载完解压用idea打开,编译,运行即可。

相关推荐
sg_knight17 分钟前
Spring Cloud与RabbitMQ深度集成:从入门到生产级实战
java·spring boot·spring·spring cloud·消息队列·rabbitmq·stream
Chan161 小时前
批处理优化:从稳定性、性能、数据一致性、健壮性、可观测性五大维度,优化批量操作
java·spring boot·后端·性能优化·java-ee·intellij-idea·优化
行者阿毅1 小时前
langchain4j+DashScope (通义千问)文生图
java·ai作画
Bug退退退1231 小时前
Java 网络流式编程
java·服务器·spring·sse
IT机器猫1 小时前
RabbitMQ
java·rabbitmq·java-rabbitmq
小杨的全栈之路1 小时前
冒泡、插入、选择、归并、堆排序:从名字由来到Java实现,一篇讲透
java·排序算法
yinke小琪1 小时前
面试官:谈谈为什么要拆分数据库?有哪些方法?
java·后端·面试
自由的疯1 小时前
java DWG文件转图片
java·后端·架构
小兔崽子去哪了1 小时前
EasyExcel 使用
java·excel
青云交1 小时前
Java 大视界 -- Java 大数据机器学习模型的对抗攻击与防御技术研究
java·机器学习模型·对抗攻击·java 大数据·防御技术·对抗训练·i - fgsm