每一年都奔走在自己热爱里 没有人是一座孤岛,总有谁爱着你

岁
万
月
12

项目创建及启动

项目地址

https://gitee.com/libeimaningg/beizhilib



一:项目创建

1.相关版本

1.1 springboot版本:<version>3.4.3</version>


1.2 JDK版本:17(使用1.8会报错)


1.3 父工程打包方式:需添加<packaging>pom</packaging>,父工程的打包方式得是pom
image

扩展:以下是几种常见的<packaging>类型及其用途:

(1)jar:这是默认的打包类型,适用于普通的Java类库。构建后将生成一个.jar文件,其中包含了编译后的.class文件和资源文件。

(2)war:用于Web应用程序。构建后将生成一个.war文件,该文件可以部署到Servlet容器(如Tomcat、Jetty)中。WAR包中除了包含Java类和资源外,还必须包含WEB-INF目录,其中包括web.xml(如果需要)和其他配置文件。

(3)pom:当项目仅作为其他项目的父POM或聚合POM时使用。这种类型的项目不会产生任何实际的可执行代码或库,而是用来定义一组共享的配置、依赖关系和插件设置。它的子模块会继承这些配置。

(4)ear:用于企业级Java应用程序。EAR(Enterprise Archive)文件是一个包含多个模块(如EJB JARs, WARs, RARs等)的归档文件,适合复杂的多层应用架构。构建后将生成一个.ear文件。

(5)bundle:与OSGi(Open Service Gateway initiative)兼容的JAR包。这类包遵循OSGi规范,并且通常带有额外的元数据,以支持模块化和服务动态加载。

(6)maven-plugin:用于创建Maven插件。这些插件可以在其他Maven项目中被引用并执行自定义任务。

(7)ejb:用于企业JavaBean组件。EJB是一种服务器端组件模型,主要用于构建分布式业务应用。构建后将生成一个.jar文件,但其内容和普通JAR有所不同,因为它们可能包含EJB特有的描述符文件

2.使用<dependencyManagement>集中管理依赖版本


2.1 <dependencyManagement>标签的作用
(1)集中管理依赖版本
在父项目中定义依赖的版本号,所有子项目可以直接使用这些依赖,无需在每个子项目中重复定义版本。这样一来,当需要更新依赖版本时,只需在父项目中修改一次,所有子项目都会自动继承新的版本。

(2)避免版本冲突
通过父项目集中管理版本,可以有效避免多个子项目依赖同一库时因版本不一致而引发的冲突,确保不同模块之间的兼容性。

(3)简化子项目的配置
子项目只需要声明依赖的坐标(groupId 和 artifactId),而无需指定版本号。这使得子项目的 pom.xml 配置更加简洁,避免了冗余的版本定义。

(4)管理传递性依赖
对于传递性依赖,可以在父项目中显式声明期望的版本,避免因传递性依赖版本不一致而导致的兼容性问题。

2.2 自定义模块依赖
子模块需要用到其他子模块时,也需要将子模块添加到父工程里,并声明版本号,然后子工程使用时也不需要定义版本:

//父工程:
<properties>
     <beizhilib.version>0.0.1-SNAPSHOT</beizhilib.version>
</properties>
<dependency>
     <groupId>org.beizhilib</groupId>
     <artifactId>beizhilib-common</artifactId>
     <version>${beizhilib.version}</version>
</dependency>
//子工程:
<parent>
     <groupId>org.beizhilib</groupId>
     <artifactId>beizhilib</artifactId>
     <version>0.0.1-SNAPSHOT</version>
</parent>
<dependency>
     <groupId>org.beizhilib</groupId>
     <artifactId>beizhilib-common</artifactId>
</dependency>

2.3 其他用法
(1)如果子项目需要使用与父项目不同的依赖版本,可以在子项目中显式指定版本号,从而覆盖父项目中的版本。
(2)如果子项目不需要父项目中定义的某个依赖,可以使用 exclusions 排除该依赖。如:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.unwanted</groupId>
            <artifactId>unwanted-library</artifactId>
            <version>1.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.unwanted</groupId>
                    <artifactId>unwanted-library</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</dependencyManagement>



二:启动错误及解决办法


1.spring-boot-starter-web依赖缺失

image
2025-02-26T09:32:35.964+08:00 INFO 11120 --- [yaoyao-web] [ main] o.yaoyao.yaoyaoweb.YaoyaoWebApplication : Starting YaoyaoWebApplication using Java 17.0.12 with PID 11120 (E:\Project\yaoyao\yaoyao-web\target\classes started by 92867 in E:\Project\yaoyao)
2025-02-26T09:32:35.970+08:00 INFO 11120 --- [yaoyao-web] [ main] o.yaoyao.yaoyaoweb.YaoyaoWebApplication : No active profile set, falling back to 1 default profile: "default"
2025-02-26T09:32:37.388+08:00 INFO 11120 --- [yaoyao-web] [ main] o.yaoyao.yaoyaoweb.YaoyaoWebApplication : Started YaoyaoWebApplication in 2.373 seconds (process running for 3.335)

Process finished with exit code 0

原因:缺少依赖:在启动类所在模块添加

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.7.18</version>
</dependency>

2.windows powershell不支持部分命令

IDEA在Terminal中输入touch README.md报错:touch : 无法将“touch”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:1 字符: 1 + touch README.md + ~~~~~ + CategoryInfo : ObjectNotFound: (touch:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

原因:windows powershell不支持该命令
解决办法:启用 Unix 工具支持:
(1)下载并安装 Git for Windows。
(2)在 IDEA 中修改终端类型为 Git Bash:File → Settings → Tools → Terminal
(3)将 Shell path 改为 Git Bash 的路径(如 "C:\Program Files\Git\bin\bash.exe")

3.上传gitee仓库

将本地创建好的项目提到仓库(需gitee上创建好一个仓库):

git init 
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/libeimaningg/beizhilib.git
git push -u origin "master"

image

4.github使用https拉取代码时报错

错误:E:\Project>git clone https://github.com/itwanger/paicoding.git
Cloning into 'paicoding'...
fatal: unable to access 'https://github.com/itwanger/paicoding.git/': SSL certificate problem: unable to get local issuer certificate
原因很多,比如证书验证、网络不通啥的,本人是由于开了steam++加速器导致的
解决方法一:
通过无视风险的方式,强行禁止 ssl 证书验证。(在 git 中输入以下命令即可):
git config --global http.sslVerify false
解决方法二:
通过为 git 导入对应的 ssl 证书,从而让 git 能够进行证书验证。
(1)打开 Steam++,找到下面的页面,点击 “打开证书文件夹”
image
(2)在这个文件夹里面,复制 SteamTools.Certificate.cer 文件的路径
image
(3)打开 git,输入下面的命令,路径换成你自己的路径就行(这里需要注意,如果你是复制路径的话,请在粘贴路径后,手动将反斜杠 \ 改为下面的双斜杠 //,否则命令将不起作用)
git config --global http.sslCAInfo D://Software//WattToolkit//AppData//Plugins//Accelerator//SteamTools.Certificate.cer
解决方案三:(强烈推荐)
改为ssh连接方式:
(1)打开git bash,检查本地主机是否已经存在ssh key

cd ~/.ssh
ls
//看是否存在 id_rsa 和 id_rsa.pub文件,如果存在,说明已经有SSH Key

如下图所示,表明已经有,则直接跳到第三步:
image
(2)生成ssh key

ssh-keygen -t rsa -C "xxx@xxx.com"
//执行后一直回车即可,生成完以后再用第一步命令,查看ssh key

(3)获取ssh key公钥内容(id_rsa.pub)

cd ~/.ssh
cat id_rsa.pub

如下图:复制该内容
image

(4)Github账号上添加公钥
点击settings
image
添加ssh key,把刚才复制的内容粘贴上去保存即可
image
(5)验证是否成功

ssh -T git@github.com

显示如下信息则表明成功:
image
注意之后在clone仓库的时候要使用ssh的url,而不是https!

(6)验证连通时可能会出现错误:$ ssh -T git@github.com
ssh: connect to host github.com port 22: Connection refused
原因:22端口不通,改为443端口:
找到config文件(如果没有新建一个):
image
在config文件里添加:

如果没有config 文件就新建一个
Host github.com
Hostname ssh.github.com
Port 443

然后重新测试连接,如果出现以下内容,输入yes即可
The authenticity of host ‘[ssh.github.com]:443 ([20.205.243.160]:443)’ can’t be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
最终显示Hi chengyun-pg! You’ve successfully authenticated, but GitHub does not provide shell access.,可能会比较慢,耐心等待。

5.启动类扫描不到其他依赖

启动报错:Field sysUserService in org.yaoyao.yaoyaoweb.controller.system.SysUserController required a bean of type 'org.yaoyao.yaoyaobiz.service.SysUserService' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
image
web模块需要biz模块,并且需要注入biz模块的SysUserService对象,这时需要在web模块引入biz模块的依赖:

        <dependency>
            <groupId>org.yaoyao</groupId>
            <artifactId>yaoyao-biz</artifactId>
        </dependency>

然后使用@Autowired注入,这时启动就会报错,因为在pom文件中引入需要模块的依赖后,在启动类扫描时扫描不到该依赖。这是因为模块web的@SpringBootApplication注解默认扫描范围为web的启动类所在的包(org.yaoyao.yaoyaoweb)及其子包,所以此时模块web并没有扫描到模块biz的包,那么自然无法在模块web中注入模块biz的Service类。

解决方法:
如果两个模块的包路径相同,只需要在启动类扩大包扫描范围
@SpringBootApplication(scanBasePackages = {"org.yaoyao"})
如果两个模块的包路径不相同,加入引入依赖的包
@SpringBootApplication(scanBasePackages = {"org.yaoyao.yaoyaoweb", "com.yaoyao.yaoyaobiz"})

5.扫描不到mapper


启动报错:Error creating bean with name 'sysUserServiceImpl': Unsatisfied dependency expressed through field 'baseMapper': No qualifying bean of type 'org.yaoyao.yaoyaobiz.mapper.SysUserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
解决方法:在启动类添加添加@MapperScan("org.yaoyao.yaoyaobiz.mapper")
配置文件添加:

mybatis-plus:
  mapperLocations: classpath*:mapper/**/*.xml
  type-aliases-package: org.yaoyao.yaoyaocommon.core.domain.entity

6.springboot与mybatisplus版本不一致


启动报错: Invalid bean definition with name 'sysConfigMapper' defined in file [E:\Project\yaoyao\yaoyao-biz\target\classes\org\yaoyao\yaoyaobiz\mapper\SysConfigMapper.class]: Invalid value type for attribute 'factoryBeanObjectType': java.lang.String


解决方法:
具体对应:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.4.3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
    <version>3.5.6</version>
</dependency>

image

扩展:
另外,mybatis-plus-spring-boot3-starter和mybatis-spring-boot-starter中同时存在了org.mybatis的依赖(至少包含了与MyBatis集成所需的关键依赖)
image
这是一个MybatisPlus相关的依赖冲突问题,主要就是存在重复的mybatis依赖导致的。
错误依赖:

 <!--  mybatis-plus      -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
    <version>3.5.10.1</version>
</dependency>
<!--  mybatis      -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>

正确依赖:

<!--  mybatis-plus      -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
    <version>3.5.10.1</version>
</dependency>
<!--  mybatis      -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.3</version>
        <exclusions>
           <exclusion>
              <groupId>org.mybatis</groupId>
               <artifactId>mybatis</artifactId>
           </exclusion>
         </exclusions>
</dependency>

问题解析:首先控制台很明显的问题就是出现了方法不存在,看到这种类似的错误就应该想到依赖冲突。原因就是在mybatis-plus-spring-boot3-starter和mybatis-spring-boot-starter中同时存在了org.mybatis的依赖,并且mybatis-spring-boot-starter中的org.mybatis版本是要低于mybatis-plus-spring-boot3-starter中的,所以导致在项目启动又时,找不到高版本的方法。引入合理的依赖即可。知道了冲突的依赖,接下来就是在pom文件中排除,一般情况下,高版本都是向下兼容的,所以把低版本的mybatis给排除掉就可以正常启动了!一般直接删除掉mybatis的依赖也行。

7.打包问题
项目打包出现:Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.3.2.RELEASE:repack


原因分析:某些模块是不需要启动的,只是提供给其他模块进行调用,不需要启动意味着没有main启动类,但是父工程的pom文件却引用了 springboot打包插件 spring-boot-maven-plugin,即:

    <!--springboot 打包插件-->
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>

所以在打包的时候mvn会扫描所有的依赖模块,如果发现某个模块下面没有main启动类,就会报错。


解决方法:直接删掉父工程的打包插件 spring-boot-maven-plugin,然后重新导入即可:
image

posted @ 2025-03-03 15:19  果咩那塞  阅读(5)  评论(0)    收藏  举报