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")
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值