所以在pom中。xml我放了这些依赖项和插件:
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-core</artifactId>
<version>3.7.4</version>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-sql</artifactId>
<version>3.7.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
....
<plugins>
<plugin>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-maven-plugin</artifactId>
<version>3.7.4</version>
<executions>
<execution>
<goals>
<goal>export</goal>
</goals>
</execution>
</executions>
<configuration>
<jdbc.url>jdbc:mysql://localhost:8889/chebuoni</jdbc.url>
<jdbc.driver>com.mysql.cj.jdbc.Driver</jdbc.driver>
<jdbcUser>root</jdbcUser>
<jdbcPassword>root</jdbcPassword>
<packageName>it.group.myproject</packageName>
<targetFolder>/target/generated-sources/java</targetFolder>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.13.1.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
只有当我退出时,Maven才能毫无错误地完成构建
我还将我的类Person映射为包中的注释@Entity。组我的项目
根据@Nikolas的提示,我终于能够生成Q类,并在pom中添加了这个插件:
<plugin>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-maven-plugin</artifactId>
<version>4.1.0</version>
<configuration>
<jdbc.url>jdbc:mysql://localhost:8889/myDB</jdbc.url>
<jdbc.driver>com.mysql.cj.jdbc.Driver</jdbc.driver>
<jdbc.user>root</jdbc.user>
<jdbc.password>root</jdbc.password>
<packageName>myPackage</packageName>
<targetFolder>/target/generated-sources/java</targetFolder>
<namePrefix>S</namePrefix>
<imports>
<import>java.io</import>
<import>java.io.File</import>
</imports>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.13.1.1</version>
</dependency>
</dependencies>
</plugin>
但是,我仍然不知道如何配置entityManager的一部分:
JPAQuery<?> queryFactory = new JPAQuery<Void>(entityManager);
我阅读了本教程-->
https://examples.javacodegeeks.com/enterprise-java/jpa/jpa-entitymanager-example/
似乎我需要一种坚持。xml文件,以便我可以执行以下操作:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jcg-JPA");
EntityManager em = emf.createEntityManager();
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jcg-JPA" />
</bean>
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
版本=“2.0”>
<persistence-unit name="jcg-JPA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:8889/myDB" />
</properties>
</persistence-unit>
但服务器在启动时失败:
org.apache.catalina.core.StandardContext listenerStart
GRAVE: Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
java.lang.NoSuchMethodError:
org.springframework.beans.factory.annotation.InjectionMetadata: method
<init>()V not found at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:351)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:295)