我是 Spring Framework 的新手,并嘗試為 sql 資料庫創建動態搜索查詢 - 類似于以下執行緒中描述的內容。
使用 spring-data-jpa 和 spring-mvc 過濾資料庫行
該執行緒提供了一個有用的指南,但是我在生成所描述的元模型檔案時遇到了問題。我知道這是一個常見問題,我已嘗試實施已在線提供的解決方案,但它們沒有奏效。
我在理解 Persistence.xml 檔案的概念以及在哪里可以找到它時遇到了特殊的問題。從我讀過的內容來看,它的功能似乎與我的“application.properties”檔案非常相似。Persistence.xml 檔案顯然是生成元模型所必需的。我找到了有關它的位置的以下答案,但老實說,我也不太明白。
《需要駐留在Java類路徑根目錄下的META-INF檔案夾》《persistence.xml應位于WAR檔案的WEB-INF/classes/META-INF目錄下》
我正在 Eclipse 中使用 Maven 專案,我能找到的唯一 META-INF 目錄位于“Maven Dependencies”中的 jar 檔案中。“persistence.xml”檔案應該自動生成還是我需要自己創建?如果有,在哪里?我添加了一個旨在生成它的插件,但它沒有用。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
我還擔心我可能沒有對專案“屬性”應用正確的更改。我已啟用“注釋處理”并將“生成的測驗源字典”設定為“.apt_生成”的默認值。這可以嗎,還是應該將生成的測驗源字典命名為其他名稱?
我已將推薦的依賴項添加到我的 pom 檔案中-
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.6.2.Final</version>
</dependency>
以及以下插件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/metamodel</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
當我嘗試重建/運行專案時,它像以前一樣運行,控制臺中沒有顯示錯誤,也沒有生成新的元模型類(_files 仍然無法識別)
請看下面我的 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.5.6</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.bron</groupId>
<artifactId>demoJPA</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demoJPA</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Spring JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<!-- Spring starter web -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- E-mail sending capabilities -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<!-- Connect to mySQL database -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Connect to mySQL database -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.5.6</version>
<scope>test</scope>
</dependency>
<!--Reduce boiler plate code -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--Spring security -->
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>org.springframework.security.experimental</groupId>
<artifactId>spring-security-oauth2-authorization-server</artifactId>
<version>0.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.6.2.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<!-- source output directory -->
<outputDirectory>target/metamodel</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/metamodel</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
因為我是 Spring 和復雜框架的新手,所以我發現我讀過的解決方案很難理解和應用到我的專案中。任何幫助將不勝感激!
uj5u.com熱心網友回復:
persistence.xml 應該在
src/main/resources/META-INF/,如果該路徑的任何檔案夾不存在,您必須自己創建它們,還必須使用您想要的配置創建/復制persistence.xml 檔案到該目錄中。元模型生成應該可以作業,但我不熟悉您用來添加源的插件,我可以給您一個我知道它正在作業的 pom 除外:
元模型類應該在使用
mvn install(mvn clean install) 時生成,確保您使用 maven 安裝您的專案以生成類似的東西,使用 IDEs run / build 命令很可能不起作用,因為它對 pom.xml 沒有任何作用。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<arguments>
<argument>--output</argument>
<argument>target/generated-sources/</argument>
</arguments>
</configuration>
</plugin>
- 您正在使用 ,
maven-jar-plugin這讓我有點困惑,因為我認為您想創建一個 Web 應用程式,因此war應該生成一個檔案。你可以通過簡單地<packaging>war</packaging>在你的 pom 下的任何地方添加<project>(例如在你的<artifactId>)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/384400.html
