sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1796 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

maven搭建多模块springboot项目微服务

目录

1.模块化概念

2.搭建模块

2.1 创建父项目

2.1.1 创建新项目

2.2 添加子模块

2.3 修改父项目pom依赖

2.3.1 父项目pom依赖说明

2.4 修改子项目pom依赖

 2.4.1 子模块pom依赖说明

3. 功能测试

3.1 简单的接口测试

3.1.1 控制层

3.1.2 服务层 

3.1.3 持久层 

3.1.4 mapper文件sql语句

3.1.5 表数据

3.1.6 查询不存在的数据

3.1.7 查询存在的数据 

3.遇到的问题


1.模块化概念

2.搭建模块

2.1 创建父项目

由于我是测试写完后写的这篇文章,所以这里面有些模板已经创建好了,可以参考下项目结构

2.1.1 创建新项目

 

 选择Maven项目

 修改信息后直接点击Finish

 目录结构可以只保留pom.xml文件

2.2 添加子模块

在父项目上面右键,参照下图选择 

这里可以有两种选择,我一般选择Spring Initializr方式,然后修改信息后删除不用的目录和文件,尝试过Maven方式但是会出问题,可能是我IDEA的问题

2.3 修改父项目pom依赖

父项目pom.xml文件更改如下

2.3.1 父项目pom依赖说明

        1.packaging: 指定这是一个pom项目

        2.module: 定义子项目

完整父项目pom.xml文件如下,可参考

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 
    <packaging>pom</packaging>
 
    <modules>
        <module>web</module>
        <module>service</module>
        <module>dao</module>
        <module>model</module>
        <module>utils</module>
    </modules>
 
    <groupId>com.huachun</groupId>
    <artifactId>demo-projects</artifactId>
    <version>0.0.1</version>
 
    <properties>
        <java.version>1.8</java.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.6.1</version>
            </plugin>
        </plugins>
    </build>
 
</project>

2.4 修改子项目pom依赖

以web模块为例

 2.4.1 子模块pom依赖说明

        1.parent: 指定依赖的父项目

        2.packaging: 指定打包类型为jar

        3.指定需要导入service模块

完整pom可参考

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.huachun</groupId>
        <artifactId>demo-projects</artifactId>
        <version>0.0.1</version>
    </parent>
    <packaging>jar</packaging>
 
    <groupId>com.huachun</groupId>
    <artifactId>web</artifactId>
    <version>0.0.1</version>
 
    <dependencies>
 
        <dependency>
            <groupId>com.huachun</groupId>
            <artifactId>service</artifactId>
            <version>0.0.1</version>
        </dependency>
 
        <dependency>
            <groupId>com.huachun</groupId>
            <artifactId>model</artifactId>
            <version>0.0.1</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
</project>

service模块pom依赖示例

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.huachun</groupId>
        <artifactId>demo-projects</artifactId>
        <version>0.0.1</version>
    </parent>
 
    <packaging>jar</packaging>
 
    <groupId>com.huachun</groupId>
    <artifactId>service</artifactId>
    <version>0.0.1</version>
 
    <dependencies>
 
        <dependency>
            <groupId>com.huachun</groupId>
            <artifactId>dao</artifactId>
            <version>0.0.1</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
</project>

其他模块都是大致一样,只是引入的依赖不同,在不同模块根据业务功能去引入相应依赖

        dao模块依赖model模块

        service模块依赖dao模块

        web模块依赖service模块

3. 功能测试

3.1 简单的接口测试

3.1.1 控制层

3.1.2 服务层 

 

3.1.3 持久层 

 

3.1.4 mapper文件sql语句

3.1.5 表数据

3.1.6 查询不存在的数据

 

3.1.7 查询存在的数据 

 

项目参考地址:https://gitee.com/huachun_w/demo-project/tree/maven-init-0.0.1

4.遇到的问题

4.1 找不到实体类

Consider defining a bean of type 'com.hhmt.service.OCPXService' in your conf

RROR] Failed to execute goal on project service: Could not resolve dependencies for project 

问题原因:模块没有很清晰的规划有以下几个包

                com.hhmt.controller        

                        Controller.java

                        Start.java

                com.hhmt.service

                        xxxService.java                        

                com.hhmt.dao

基于这个目录结构发现Start.java最终打包在com.hhmt.controller包里面,而启动类会自动扫描当前包下面的类,所以不能扫描到com.hhmt.service下面的类也是正常,想通了这一点,其实就有两个解决办法。

解决办法:

        1.添加扫描注解,指定扫描同一个包

        @ComponentScan("多模块的情况下指定到父包")

        2.整理好项目模块(推荐这种,这样会让模块更加清晰)

2.no main manifest attribute

解决方法:在pom文件中添加下面插件

<!-- 原文链接:https://blog.csdn.net/sunnyzyq/article/details/89536915 -->
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>
        </plugins>
</build>

添加完之后重新打包运行,正常启动,接口调用正常

2.编译模块提示不能找到父模块

详细:Could not find artifact com.hhmt:delivery:pom:0.0.1-SNAPSHOT

[ERROR] Failed to execute goal on project commons: Could not resolve dependencies for project com.hhmt:commons:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at com.hhmt:model:jar:0.0.1-SNAPSHOT: Failed to read artifact descriptor for com.hhmt:model:jar:0.0.1-SNAPSHOT: Could not find artifact com.hhmt:delivery:pom:0.0.1-SNAPSHOT -> [Help 1]

未完待续...

无法自动注入依赖

模块中可以引入其他模块的类,但是工具提示找不到

@Autowired private TestService testService;

解决方法:在启动类添加扫描包

@SpringBootApplication(scanBasePackages = "com.huachun")
https://blog.csdn.net/hauchun/article/details/121741730
posted on   sunny123456  阅读(1782)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-10-17 C# Lambda表达式select()和where()的区别
2019-10-17 C# join() 连表查询
2019-10-17 C# List.Join()方法 表链接查询
2019-10-17 C# 使用j
2019-10-17 C# Linq技术中SelectMany() 获取list对象的属性 汇总成为一个新的集合
2019-10-17 C# 集合的扩展方法-查询表达式GroupBy()的使用 转
点击右上角即可分享
微信分享提示