【问题标题】:spring boot 2 + entity/jpa as sub modulespring boot 2 + entity/jpa 作为子模块
【发布时间】:2019-01-25 09:33:14
【问题描述】:

带有maven多模块项目的spring boot 2

我们如何将 JPA/entity 创建为单独的模块并将依赖项添加到主项目中??

我已经阅读了很多链接,但仍然没有找到解决方案,请帮助...

我想创建一个如下所示的项目结构...

entity-project - 所有实体和 JPA 相关配置[数据库和所有配置]

主服务[包括实体]和实体项目的存储库将在这里。

请帮忙...

【问题讨论】:

    标签: spring spring-boot maven-3 multi-module


    【解决方案1】:

    这是我下面的项目结构。

    remittance-rs 是主项目模块,包括我们在 pom.xml 文件中定义的所有子模块,如下所示:

    <groupId>org.soyphea.remittance</groupId>
    <artifactId>remittance-rs</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    
    <modules>
      <module>service</module>
      <module>domain</module>
      <module>remmittant-restapi</module>
    </modules>
    

    所以我有 3 个模块,其中 service 用于我们的逻辑,domain 用于我们的 DAO 和 Domain,最后一个 restapi 用于我们的暴露休息端点。

    所以所有子模块都需要包含父构件和组 ID,如下所示,

    <parent>
        <groupId>org.soyphea.remittance</groupId>
        <artifactId>remittance-rs</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    

    在您的子模块中,您将需要每个单独的依赖项。

    示例

    domain 模块 pom.xml 将需要 spring-boot-starter-data-jpa 让我们看看细节。

    <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>
        <parent>
            <groupId>org.soyphea.remittance</groupId>
            <artifactId>remittance-rs</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <artifactId>org.soyphea.domain</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
            </dependency>
    
        </dependencies>
    </project>
    

    这是我在 github 中的示例项目 https://github.com/PheaSoy/spring-boot-remittance-project

    【讨论】:

    • 感谢您的回复,但无论我在哪里使用域模块,我都必须正确设置数据库配置??
    • 您可以使用您的主模块进行配置,例如 web 或 rest-api 模块是可以的。因为我们假设域模块与 DAO 和实体相关,所以那里没有配置。我们的配置应该有一个地方。
    • 但是我有多个主项目将使用实体/dao 项目...那么我们如何在实体/dao 模块中配置数据库配置...请帮助
    • 你能告诉我你的主要项目是什么吗?示例:您应该拥有的支付 API(您的 dao 的域模块,您的逻辑的服务模块,以及您的控制器的 web-mvc 或休息端点)。您的配置应该在休息端点模块中,我称为主模块。
    • 我们正在使用微服务,我们有多个微服务使用相同的实体/dao ....thanx 寻求帮助
    猜你喜欢
    • 2019-03-02
    • 2016-02-02
    • 2017-06-01
    • 2020-07-04
    • 1970-01-01
    • 2019-09-14
    • 2019-05-14
    • 2018-10-19
    • 2015-11-28
    相关资源
    最近更新 更多