关于2024 如何在 IntelliJ IDEA 2023 中开发 Java 8 Springboot 2.x 的项目 home 编辑时间 2024/06/06 ![](/api/file/getImage?fileId=66613b3dda7405001405683d) <br><br> ## 前言 <br> 众所周知 `IntelliJ IDEA 2023.2` 新建 `springboot` 项目时,只能新建 `springboot 3.x`,并且只支持 `JDK17` 以上版本 <br> 虽然我并不是祖宗之法不可变的保守派,但我非常偶尔的时候也会遇到需要开发一个 `JDK8` `Srpingboot 2.x` 的需求,IDEA 这样一刀切,逼得我只能自己手动新建项目了 <br> 其实最早在学Java的时候,新建项目都是普通的本地项目,自己加jar包依赖,自己写配置文件的,后来有了maven,就可以快速引入依赖,并且自动解决依赖链的问题。后来有了Springboot,配置文件也可以不写少写。现在无非就是退回去,新建一个Maven项目,然后手动配置Springboot的部分。 <br><br> ## 折腾 <br> 1. 新建Maven项目 JDK选择1.8 Archetype选择quickstart ![](/api/file/getImage?fileId=66612608da7405001405679c) <br> 2. 根据自己习惯 改变src下的目录结构 如图我删了test目录 新建了resources 并且在Project Structure中配置为Resources类型 ![](/api/file/getImage?fileId=66612db0da740500140567e4) <br> 附上一个好看的banner.txt ```txt .::::::`.::::::::::.. .''``````.::::::::::::::::: .:',ere$ze c ::::::::::::::::::: ,'` e$$$$$$$-K eeeu...`````::::::::: .zd>^leeu^R$%:FJ$$$$$$$$$$e.. ``:::::: .ed$$$$4$$$$$P-u@" ""?????R$$$$$$$hc. ``::: .e$$F"..: P??7loF .:::::::::::.."""?R$$$e. `:. zF".:::::::`"""'.:::::::::::::::::::::.`"?$$e.` .::::::::::::':::::::::::::::::::::::::::::.`"=. .:::::::::::::` `:::::::::::::::::::::::::::::::.. .:::::::::::::` ud$ec. ``:::::::::::::::::::::::::::::. .:::::::::::`` .zd$$$$$$$ec.. ```:::::::::::::::::::::::::: .:::::::::::` "??$$$$$$$$$$$P ``:::::::::::::::::::::: ::::::::::` .. $$*. ^$$$$$$$$$$ .e$**"" =e=.. ``::::::::::::::::. ::::::::: :::. . ^ $$P$$$$$$$$$$F .'$$$N4$$L'::. `::::::::::::::: `::::::` :::::: $ '$$4$$$$$$$$% - : $$$F$$$$u`::::::. `:::::::::::. :::::: :::::: .^m.-.e.$'$$$$$$$P. -)z$?Cd$$$$$u `:::::::. `::::::::: `::::::::::: J$buFh52d4$$$$$$$LcccccCz$$$$$$$$": `::::::::..::::::::: `::::::::: $$$$$$$$PJ$$$$$$$$$$$$$$$$$$$$$F.d$$. `::::::::::::::::` `:::::: ?$$$$$$$F$$$$$$$$$$$$$$$$$$$$P x$$$$$$L `:::::::::::::: ``:: dN ?$$$$$$N2$$?3$$$$$$$$$$$$$$P dP\$$$$$$$u `:::::::::::: `:'" $.`R$$P???$$P??????"$$$$$$$$ 9".d$$$$$$$$b.`:::::::::: :: R$$. ?$$r `..;; . $$$$$$$F"'$'$J?$$$$$$$f :::::::: `:.^""""""."?$`niiiodfu$$$$$F"z$$ ^""~""""`..:::: :::::: `::::::::::`?beCCbe$$$$""cd$$$$i .`::::::::::::. `:::` ``:::::::::`?$$$P"',cd$$$$$$$$r?k :::::::::::`.:::` ```::::::::: 4$$$$$$$$$$$$$"d$ ``::::::::::::` ``::: . $$$$$$$$$$$$fJ$F4$e.``:::::::` dR `$$$$$$$$$$"x$$ d$$$$eu,.``` .e$ $E b?$$$$$$$".d$$ d$$$$$$$$$$$e.. ..ee$$$$$ $$k`$$$$$$".d$$".$$$$$$$$$$$$$$$$$$hec. .ze$$$$$$$$$$$b"$$heeeeeud$R" e$$$$$$$$$$$$$$$$$$$$$$$$$e. z$$$$$$$$$$$$$$$$$$h`?c""""J$R z$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ .ee$$$$$$$$$$$$$$$$$$$$$$hc"xJ>=".zd$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ .d$$$$$$$$$$$$$$$$$$$$$$$$$$$$he$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ``` <br> 3. 配置一下maven 这里加了 springboot redis 开发环境中的tomcat mysql mybatisplus lombok apache工具箱等基础依赖 底部加了阿里云镜像maven加快速度 整体打war包,生产环境需要通过tomcat执行 版本选择截止今天最后一个springboot 2的版本 2.7.18 <br> `pom.xml` ```xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zzzmh</groupId> <artifactId>spring-boot-demo</artifactId> <packaging>war</packaging> <version>1.0.0</version> <name>spring-boot-demo</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.18</version> </parent> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>8.4.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.15</version> </dependency> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.33</version> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> <!-- 阿里云maven仓库 --> <repositories> <repository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>public</id> <name>aliyun nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project> ``` <br> 改完以后刷新一下maven依赖 <br> 4. 新建启动类 参考前文,我这里选择war包,所以启动类是按照war方式建 删除原本的 `App.java` 在同级新建2个启动类文件 <br> `SpringBootDemoApplication.java` ```java package com.zzzmh; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDemoApplication.class, args); } } ``` <br> `ServletInitializer.java` ```java package com.zzzmh; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringBootDemoApplication.class); } } ``` <br> 如果到这一步都配置正确,已经可以执行 `SpringBootDemoApplication` 的 `main` 方法启动试试看了,因为springboot 有默认配置,无配置文件也可以启动 <br> 启动后看到这样的控制台输出就说明启动成功了 ```java 2024-06-06 12:03:29.902 INFO 17412 --- [ main] com.zzzmh.SpringBootDemoApplication : Starting SpringBootDemoApplication using Java 1.8.0_345 on ROG-G14 with PID 17412 (C:\mywork\idea-2023-workspace\spring-boot-demo\target\classes started by Administrator in C:\mywork\idea-2023-workspace\spring-boot-demo) 2024-06-06 12:03:29.905 INFO 17412 --- [ main] com.zzzmh.SpringBootDemoApplication : The following 1 profile is active: "light-dev" 2024-06-06 12:03:30.557 INFO 17412 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode 2024-06-06 12:03:30.562 INFO 17412 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode. 2024-06-06 12:03:30.580 INFO 17412 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 6 ms. Found 0 Redis repository interfaces. 2024-06-06 12:03:30.683 WARN 17412 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.zzzmh]' package. Please check your configuration. 2024-06-06 12:03:31.096 INFO 17412 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2024-06-06 12:03:31.102 INFO 17412 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2024-06-06 12:03:31.103 INFO 17412 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.83] 2024-06-06 12:03:31.269 INFO 17412 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2024-06-06 12:03:31.269 INFO 17412 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1323 ms 2024-06-06 12:03:31.542 WARN 17412 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class 2024-06-06 12:03:31.545 INFO 17412 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2024-06-06 12:03:31.557 INFO 17412 --- [ main] ConditionEvaluationReportLoggingListener : ``` <br> 后面有一个这样的报错是正常的,因为还没配置JDBC的参数 ```java Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2024-06-06 12:03:31.575 ERROR 17412 --- [ 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 (the profiles light-dev are currently active). ``` <br> 相信能看到这里的人,应该都能自己写springboot的参数配置文件 这里只列举一个最简单基础的配置吧 开发环境一切从简就行,把数据库 用户名密码 还有 service目录改成你自己的 就OK了 <br> `application.yml` ```yml # 通用配置 spring: profiles: active: dev mvc: pathmatch: matching-strategy: ant_path_matcher ``` <br> `application-dev.yml` ```yml # 开发环境 server: port: 8080 servlet: context-path: /demo spring: redis: host: 127.0.0.1 port: 6379 servlet: multipart: max-file-size: 100MB max-request-size: 100MB datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/xxx?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&tinyInt1isBit=false&allowPublicKeyRetrieval=true username: xxxxxx password: xxxxxx mybatis-plus: mapper-locations: classpath*:/mapper/*.xml type-aliases-package: com.zzzmh.service global-config: db-config: id-type: auto logic-delete-value: 1 logic-not-delete-value: 0 insert-strategy: not_null update-strategy: not_null banner: false configuration: map-underscore-to-camel-case: true cache-enabled: true call-setters-on-nulls: true logging: level: org.springframework.web: trace org.mybatis: trace ``` <br><br> ## END 大功告成 收摊! 送人玫瑰,手留余香 赞赏 Wechat Pay Alipay 解决 SpringBoot 升级 3.3.0 MybatisPlus报错 Invalid value type for attribute 'factoryBeanObjectType': java.lang.String 2024 Java STMP 发送邮件工具类代码 支持腾讯/网易企业邮箱