我是Querydsl的新手,并尝试在一个简单的测试项目中使用它。 我按照官方教程配置我的pom.xml,然后mvn clean install能够在target / generated-sources / java下生成Q类。 但是我得到了以下错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project spring-jqgrid-tutorial: Compilation failure
[ERROR] /D:/Project/spring-jqgrid-tutorial/src/main/java/org/krams/controller/UserController.java:[92,62] package QUser.user does not exist
我认为根本原因是生成的Q类源文件没有自动编译成二进制类文件。我确认我的项目目录下没有QUser.class。 我还尝试使用build-helper-maven-plugin将target / generated-sources / java添加为源文件夹,并在apt-maven-plugin配置中指定target / generated-sources / java作为附加源根。但我没有运气。
这是我的pom.xml
<properties>
<querydsl.version>3.3.2</querydsl.version>
<maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
<maven.apt.plugin.version>1.1.1</maven.apt.plugin.version
<maven.build.helper.plugin.version>1.8</maven.build.helper.plugin.version>
<properties>
<dependencies>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<scope>provided</scope>
</dependency>
<dependencies>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${maven.build.helper.plugin.version}</version>
<executions>
<execution>
<id>add-source</id>
<phase>process-classes</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>${maven.apt.plugin.version}</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
<additionalSourceRoots>
<additionalSourceRoot>target/generated-sources/java</additionalSourceRoot>
</additionalSourceRoots>
</configuration>
</execution>
</executions>
</plugin>
<plugins>
这是生成的QUser.java
package org.krams.domain;
import static com.mysema.query.types.PathMetadataFactory.*;
import com.mysema.query.types.path.*;
import com.mysema.query.types.PathMetadata;
import javax.annotation.Generated;
import com.mysema.query.types.Path;
import com.mysema.query.types.path.PathInits;
/**
* QUser is a Querydsl query type for User
*/
@Generated("com.mysema.query.codegen.EntitySerializer")
public class QUser extends EntityPathBase<User> {
private static final long serialVersionUID = -1712499619L;
private static final PathInits INITS = PathInits.DIRECT2;
public static final QUser user = new QUser("user");
public final NumberPath<Integer> age = createNumber("age", Integer.class);
public final StringPath firstName = createString("firstName");
public final NumberPath<Long> id = createNumber("id", Long.class);
public final StringPath lastName = createString("lastName");
public final StringPath password = createString("password");
public final QRole role;
public final StringPath username = createString("username");
public QUser(String variable) {
this(User.class, forVariable(variable), INITS);
}
public QUser(Path<? extends User> path) {
this(path.getType(), path.getMetadata(), path.getMetadata().isRoot() ? INITS : PathInits.DEFAULT);
}
public QUser(PathMetadata<?> metadata) {
this(metadata, metadata.isRoot() ? INITS : PathInits.DEFAULT);
}
public QUser(PathMetadata<?> metadata, PathInits inits) {
this(User.class, metadata, inits);
}
public QUser(Class<? extends User> type, PathMetadata<?> metadata, PathInits inits) {
super(type, metadata, inits);
this.role = inits.isInitialized("role") ? new QRole(forProperty("role"), inits.get("role")) : null;
}
}
以下是UserController.java中对QUser的唯一引用。 repository是UserRepository存储库的一个实例,它扩展了QueryDslPredicateExecutor。
package org.krams.controller;
import java.util.List;
import org.krams.domain.QUser;
import org.krams.domain.Role;
import org.krams.domain.User;
import org.krams.repository.UserRepository;
import org.krams.response.JqgridResponse;
import org.krams.response.StatusResponse;
import org.krams.response.UserDto;
import org.krams.service.UserService;
import org.krams.util.JqgridFilter;
import org.krams.util.JqgridObjectMapper;
import org.krams.util.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
...
if (qUsername != null)
users = repository.findAll(QUser.user.username.like(qUsername), pageRequest);
感谢您的帮助和评论。如果需要,我可以提供更多信息。
答案 0 :(得分:1)
我再次从零开始构建了一切,最后Querydsl正在工作。 我认为这个问题是由弹簧数据jpa和querydsl的不兼容版本引起的。 现在我使用的是spring data jpa 1.3.2.RELEASE和querydsl 2.8.0。,一切都很完美。 附:我删除了additionalSourceRoots和build-helper-maven-plugin配置。它们是不必要的。
答案 1 :(得分:0)
不确定,但在我当前的项目中(由其他人设置)我有两个额外的文件夹,它们与生成源阶段相关联。
希望这有帮助。
<!-- MYSEMA generates a lot of query classes for the model in a pre-process step -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${version.org.codehaus.mojo.build-helper-maven-plugin}</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/apt/</source>
<source>${project.build.directory}/generated-sources/annotations/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>${version.com.mysema.maven.apt-maven-plugin}</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/apt</outputDirectory>
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
答案 2 :(得分:0)
这对我有用
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>add-resource</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>src/main/sources</outputDirectory>
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>3.4.3</version>
</dependency>
</dependencies>
</plugin>