新版idea(2023)创建spring boot3项目

简介: 新版idea(2023)创建spring boot3项目

前言

本教程对新手小白友好。如果你想直接获取代码或遇到问题可下载我提供的源码,在文章最后。

本教程较新

本文使用的工具以及搭建的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。

注意:作者后续spring boot实战系列文章都会用spring boot3x,如果你想跟着博主实战教程建议保持跟博主一致。

3.初始依赖选择

选择几个常用初始依赖

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

也可以不用idea自带的初始化,自行去spring官网初始化模板并下载:

初始化完成如图:

配置Maven

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

1.打开maven设置

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

输入maven搜索

2.重写maven配置文件

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

我们只需要提前准备好setting.xml即可。每次新建项目用到不同java版本只需要换不同配置文件即可。

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

粘贴如下:

我的java版本是17

<?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 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 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类

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:

server:
  # 端口号
  port: 8888
spring:
  application:
    # 应用名称
    name: mijiu-app

代码获取

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

可以自取我已经创建好的,项目根目录已经提供maven配置文件(阿里云镜像源,Java17)

1.git方式获取

代码地址:

https://gitee.com/mi9688-wine/springboot-demo

克隆后先编译一下在运行

2.下载压缩包方式获取

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

目录
相关文章
|
15天前
|
Java 应用服务中间件 程序员
如何利用Idea创建一个Servlet项目(新手向)(下)
如何利用Idea创建一个Servlet项目(新手向)(下)
26 0
|
1月前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
156 0
|
2天前
|
Java Maven Docker
0.07 秒启动一个 SpringBoot 项目!Spring Native 很强!!
0.07 秒启动一个 SpringBoot 项目!Spring Native 很强!!
9 2
|
3天前
|
Java Maven Kotlin
[AIGC] 请你写一遍博客介绍 “使用idea+kotinlin+springboot+maven 结合开发一个简单的接口“,输出markdown格式,用中文回答,请尽可能详细
[AIGC] 请你写一遍博客介绍 “使用idea+kotinlin+springboot+maven 结合开发一个简单的接口“,输出markdown格式,用中文回答,请尽可能详细
|
15天前
|
JSON 前端开发 Java
统一异常处理:让Spring Boot项目异常更优雅
统一异常处理:让Spring Boot项目异常更优雅
25 1
|
1月前
|
JavaScript Java 关系型数据库
实例!使用Idea创建SSM框架的Maven项目
实例!使用Idea创建SSM框架的Maven项目
38 0
|
1月前
|
缓存 NoSQL Java
spring cache整合redis实现springboot项目中的缓存功能
spring cache整合redis实现springboot项目中的缓存功能
46 1
|
1月前
|
Java Shell API
通用Shell脚本执行Spring Boot项目Jar包
通用Shell脚本执行Spring Boot项目Jar包
|
1月前
|
存储 Java 关系型数据库
Spring Batch学习记录及示例项目代码
Spring Batch学习记录及示例项目代码
|
1月前
|
Java 应用服务中间件 Maven
【问题篇】IDEA运行war包项目
【问题篇】IDEA运行war包项目
94 0