首页 > 其他分享 >maven搭建多模块springboot项目微服务

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

时间:2022-10-17 15:58:25浏览次数:84  
标签:springboot boot maven pom 模块 3.1 com

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文件如下,可参考

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.6.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <packaging>pom</packaging>
  12. <modules>
  13. <module>web</module>
  14. <module>service</module>
  15. <module>dao</module>
  16. <module>model</module>
  17. <module>utils</module>
  18. </modules>
  19. <groupId>com.huachun</groupId>
  20. <artifactId>demo-projects</artifactId>
  21. <version>0.0.1</version>
  22. <properties>
  23. <java.version>1.8</java.version>
  24. </properties>
  25. <dependencies>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. </dependency>
  30. </dependencies>
  31. <build>
  32. <plugins>
  33. <plugin>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-maven-plugin</artifactId>
  36. <version>2.6.1</version>
  37. </plugin>
  38. </plugins>
  39. </build>
  40. </project>

2.4 修改子项目pom依赖

以web模块为例

 2.4.1 子模块pom依赖说明

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

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

        3.指定需要导入service模块

完整pom可参考

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>com.huachun</groupId>
  7. <artifactId>demo-projects</artifactId>
  8. <version>0.0.1</version>
  9. </parent>
  10. <packaging>jar</packaging>
  11. <groupId>com.huachun</groupId>
  12. <artifactId>web</artifactId>
  13. <version>0.0.1</version>
  14. <dependencies>
  15. <dependency>
  16. <groupId>com.huachun</groupId>
  17. <artifactId>service</artifactId>
  18. <version>0.0.1</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>com.huachun</groupId>
  22. <artifactId>model</artifactId>
  23. <version>0.0.1</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-test</artifactId>
  32. <scope>test</scope>
  33. </dependency>
  34. </dependencies>
  35. </project>

service模块pom依赖示例

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>com.huachun</groupId>
  7. <artifactId>demo-projects</artifactId>
  8. <version>0.0.1</version>
  9. </parent>
  10. <packaging>jar</packaging>
  11. <groupId>com.huachun</groupId>
  12. <artifactId>service</artifactId>
  13. <version>0.0.1</version>
  14. <dependencies>
  15. <dependency>
  16. <groupId>com.huachun</groupId>
  17. <artifactId>dao</artifactId>
  18. <version>0.0.1</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-test</artifactId>
  27. <scope>test</scope>
  28. </dependency>
  29. </dependencies>
  30. </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文件中添加下面插件

  1. <!-- 原文链接:https://blog.csdn.net/sunnyzyq/article/details/89536915 -->
  2. <build>
  3. <plugins>
  4. <plugin>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-maven-plugin</artifactId>
  7. <configuration>
  8. <executable>true</executable>
  9. </configuration>
  10. </plugin>
  11. </plugins>
  12. </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

标签:springboot,boot,maven,pom,模块,3.1,com
From: https://www.cnblogs.com/sunny3158/p/16799468.html

相关文章

  • SpringBoot异步线程,父子线程数据传递的5种方案
    背景在上一篇《SpringBoot+@Async开启异步,快的飞起(https://blog.51cto.com/u_15339304/5715380)》文章种我们介绍了使用springboot自定义线程池的方式实现多线程的异步......
  • 河北稳控科技智能振弦传感器的核心技术-电子标签模块
    河北稳控科技智能振弦传感器的核心技术-电子标签模块在前面我们讲了《振弦传感器的发展及信息化的核心技术-VM系列振弦采集模块》中提到河北稳控科技研发并批量生产的激励......
  • [idea中配置maven]
    前言:加入项目中,采用idea环境开发,在这个过程中,对maven项目的了解又深入了很多,学习是一个不断深入的过程,在这个过程中,不断成长,本篇博客记录idea配置maven,从环境变量......
  • ERP常用模块顾问行情对比
    ERP系统因为庞大复杂,多业务协同,实施运维顾问会根据模块进行分工协作。每个模块业务内容和需求各不相同,相应的也催生出了模块之间的差异,包括行情、工作量、难易程度以及薪资......
  • maven 无法下载插件
    起因本次文章记录一下前段时间来公司按照Jenkins遇到的一个坑。maven安装完成后无法下载插件。解决这次的问题百度之后,都说是插件下载的链接是不对的,都是重复的解决方式,实在......
  • windows minio服务安装以及springboot集成
    @​​TOC​​​​​​本文只介绍如何将minio做成服务​​1.将minio做成服务将MinioServer.exe放在minio安装目录中同目录下创建MinioServer.xml。特别注意,xml和exe必须同名......
  • 获取springboot中指定目录下 带有指定注解的类
    privateList<String>getTableNameList(){List<String>list=newArrayList<>();PathMatchingResourcePatternResolverresolver=newPathMatchi......
  • QFramework v1.0 使用指南 架构篇:08. 用接口设计模块(依赖倒置原则)
    QFramework本身支持依赖倒置原则,就是所有的模块访问和交互都可以通过接口来完成,代码如下:usingUnityEngine;usingUnityEngine.UI;namespaceQFramework.Example{......
  • Eclipse插件开发MavenArchetype
    介绍用过Maven的童鞋,可能会听说过archetype(骨架)。而在Eclipse中利用m2e插件提供的骨架来新建Maven工程的话,是需要手动来配置的。而如果每次手动添加骨架会很麻烦,如果能像Ecl......
  • [转]SpringBoot项目@Configuration类中使用@Autowired自动注入为null
    原文地址:SpringBoot项目@Configuration类中使用@Autowired自动注入为null_潘子夜个人博客(panziye.com)最近潘老师在搭建SpringBoot项目整合Shiro框架时,在@Configurat......