我正在尝试了解在 springboot 中使用 JUnit 进行测试驱动开发。当我添加 queryDSL 查询时,我收到编译时错误,指出 JPA queryfactory 行上有
JPAqueryfactory cannot be resolved to a type
。并且无法运行项目,
我的 pom.xml 文件如下所示,
<properties>
<java.version>17</java.version>
<querydsl.version>5.0.0</querydsl.version>
</properties>
<dependencies>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<scope>provided</scope>
<classifier>jakarta</classifier>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
生成的 Q 类型类在目标目录中如下所示,
我的控制器示例代码如下,
import com.devopsauthorize.model.QUsers;
import com.devopsauthorize.model.Users;
import com.devopsauthorize.services.UserService;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
@RequestMapping("/cicd")
@RestController
public class PipelineController {
@Autowired
UserService userServiceObj;
@PersistenceContext
private EntityManager em;
@GetMapping("/load")
public String loadPipelineMessageMethod(){
//return userServiceObj.retrieveUserRoleDetails("dmoore");
QUsers users = QUsers.users;
JPAQueryFactory queryFactory = new JPAQueryFactory(em);
Users userObj = queryFactory.selectFrom(users)
.where(users.susername.eq("cvo"))
.fetchOne();
System.out.println(userObj.susername);
System.out.println(userObj.spassword);
return userObj.spassword;
// return "hello";
}
}
如果我走错方向,任何人都可以指导我解决此错误或建议任何替代的实施方式吗?
@GetMapping("/load")
public String loadPipelinemessagemethod() {
//return userServiceObj.retrieveUserRoleDetails("dmoore");
QUsers users = QUsers.users;
JPAQueryFactory queryFactory = new JPAQueryFactory(em);
Users userObj = queryFactory.selectFrom(users)
.where(users.susername.eq("cvo"))
.fetchOne();
System.out.println(userObj.susername);
System.out.println(userObj.spassword);
return userObj.spassword;
// return "hello";
}
您的
pom.xml
需要更新如下:
<properties>
<java.version>17</java.version>
<querydsl.version>5.0.0</querydsl.version>
</properties>
<dependencies>
<!-- QueryDSL Core -->
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-core</artifactId>
<version>${querydsl.version}</version>
</dependency>
<!-- QueryDSL JPA -->
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
<!-- QueryDSL Annotation Processor -->
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<classifier>jakarta</classifier>
<scope>provided</scope>
</dependency>
<!-- Spring Boot Starter JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Other Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven Plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- Annotation Processing for QueryDSL -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<classifier>jakarta</classifier>
</path>
</annotationProcessorPaths>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
确保您的
Users
实体带有 @Entity
注释,并且具有 susername
和 spassword
字段。之后重建您的项目。仔细检查您的控制器导入和使用 QUsers
和 JPAQueryFactory
。
如果仍未生成
QUsers
类,请确保您的 IDE 配置为处理注释:
IntelliJ IDEA:
Settings > Build, Execution, Deployment > Compiler > Annotation Processors
。
日食:
Project > Properties > Java Compiler > Annotation Processing
.