【发布时间】:2020-04-01 13:16:05
【问题描述】:
我做到了:
- 创建的项目包含一些依赖项。
- 创建了第一个 Maven 模块(网络)。
- 创建了第二个 Maven 模块(模型)。
- 将代码移至 Web 模块:主类 + 控制器。
- 从根目录中删除了src(因为它已经是空的了)
- 在模型模块中创建实体,添加依赖JPA/MySQl驱动到这个模块的pom.xml。
- 在 web 模块中添加了模型模块的依赖项。
项目运行没有错误,web 模块看到 model 依赖。但是实体代码不起作用(不在数据库中创建表)。如果我将课程移至 web 模块 - 它可以工作。完全正确 - 它可以工作,但 JPA/MySQL 的依赖项仍在 model 模块中。
我做错了什么?
我尝试使用包的模块名称更改 groupId 和 artifactId 的名称,将 application.propeties 移至 model 模块 - 没有帮助。
我的项目:
-model/src/main/java 和 resources/com/example/demo/model
pom.xml:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.demo.model</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>module-model</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
-web/src/main/java 和 resources/com/example/demo/web
application.properties 与 MySQL 的数据库连接。
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.demo.web</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>module-web</name>
<description>Web project</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.example.demo.model</groupId>
<artifactId>model</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
主 pom.xml:
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.1.0</version>
<packaging>pom</packaging>
<description>Demo project for Spring Boot</description>
<name>demo</name>
<modules>
<module>web</module>
<module>model</module>
</modules>
【问题讨论】:
-
尝试将 @EntityScan(basePackageClasses = EntityPackage.class) 注释添加到配置类。 “EntityPackage.class”可以是实体包根目录下的接口。
-
接口是什么?或者你的意思只是为了这个注释的空接口?
-
是的,只是空接口,或者您可以使用实体包的路径@EntityScan("com.example.entity")。
标签: java spring spring-boot maven