从需求代码实现 HQL 构建器
Implement HQL builder from requirements code
我在构建交易时有这段代码:
public BooleanBuilder findAllTransactionsByUserId(Long userId, DateTime startDate, DateTime endDate,
String firstName, String lastName, String email) {
QTransaction transaction = QTransaction.transaction;
BooleanBuilder where = getTransactionWhereClause(startDate, endDate, firstName, lastName, email);
where.and(transaction.userId.eq(userId));
return where;
}
public BooleanBuilder getTransactionWhereClause(DateTime startDate, DateTime endDate,
String firstName, String lastName, String email) {
BooleanBuilder where = new BooleanBuilder();
QTransaction transaction = QTransaction.transaction;
if (startDate != null && endDate != null) {
where.and(transaction.transactionDate.between(startDate, endDate));
}
if (firstName != null) {
where.and(transaction.firstName.eq(firstName));
}
if (lastName != null) {
where.and(transaction.lastName.eq(lastName));
}
if (email != null) {
where.and(transaction.email.eq(email));
}
return where;
}
我缺少 class QTransaction
的代码,我无法弄清楚 Java class 的内容,所以我可以手动创建。
你知道我如何实现缺失的代码吗?
我试过这个 gradle 配置:
plugins {
id 'org.springframework.boot' version '2.6.7'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'test'
version = '0.0.1'
sourceCompatibility = '17'
ext {
set('springCloudVersion', "2021.0.2")
queryDslVersion = '5.0.0'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.hibernate.validator:hibernate-validator'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'joda-time:joda-time:2.10.14'
implementation 'org.springframework.boot:spring-boot-starter-hateoas:2.6.7'
implementation 'org.postgresql:postgresql'
implementation 'org.jadira.usertype:usertype.core:7.0.0.CR1'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap:3.1.2'
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'com.google.code.gson:gson:2.9.0'
// QueryDSL
implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}:jpa"
testImplementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
testAnnotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}:jpa"
// Lombok
compileOnly 'org.projectlombok:lombok:1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'
testCompileOnly 'org.projectlombok:lombok:1.18.24'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'
// Swagger
implementation 'org.springdoc:springdoc-openapi-ui:1.6.8'
implementation 'org.liquibase:liquibase-core'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
tasks.named('test') {
useJUnitPlatform()
}
但是 JPA classes 没有生成。我得到异常:
import com.test.domain.QTransaction;
^
symbol: class QTransaction
location: package com.test.domain
你知道实现这个的正确方法是什么吗?
Q
classes 通常由 QueryDSL 和 QueryDSL 注释处理器工具生成。
我对 Gradle 的熟悉不如对 Maven 的熟悉,但是 Gradle 配置对我来说看起来不错。为了以防万一,请参阅 this or 相关的 SO 问题。
当与 JPA 一起使用时,作为第一步,如您所料,您必须创建一个代表您的 table 的 class,您必须创建一个包含所需信息的 @Entity
.在您的情况下,它看起来类似于此代码:
@Entity
public class Transaction {
@Id
@GeneratedValue
private Long id;
private Long userId;
private String firstName;
private String lastName;
private String email;
private DateTime transactionDate;
// getters and setters
}
请注意,我们将 class 注释为 @Entity
。
编译您的 classes 后,您会看到 Gradle 应该生成了一个 class,名称为 QTransaction
。
除其他信息外,它将生成 QTransaction
class:
的 public static final
实例
public static final QTransaction transaction = new QTransaction("transaction");
这个 class 是您在构建查询时通常会使用的查询,如您的示例中所示。
This article,虽然 Maven 专注,但可能会有所帮助。
我在构建交易时有这段代码:
public BooleanBuilder findAllTransactionsByUserId(Long userId, DateTime startDate, DateTime endDate,
String firstName, String lastName, String email) {
QTransaction transaction = QTransaction.transaction;
BooleanBuilder where = getTransactionWhereClause(startDate, endDate, firstName, lastName, email);
where.and(transaction.userId.eq(userId));
return where;
}
public BooleanBuilder getTransactionWhereClause(DateTime startDate, DateTime endDate,
String firstName, String lastName, String email) {
BooleanBuilder where = new BooleanBuilder();
QTransaction transaction = QTransaction.transaction;
if (startDate != null && endDate != null) {
where.and(transaction.transactionDate.between(startDate, endDate));
}
if (firstName != null) {
where.and(transaction.firstName.eq(firstName));
}
if (lastName != null) {
where.and(transaction.lastName.eq(lastName));
}
if (email != null) {
where.and(transaction.email.eq(email));
}
return where;
}
我缺少 class QTransaction
的代码,我无法弄清楚 Java class 的内容,所以我可以手动创建。
你知道我如何实现缺失的代码吗?
我试过这个 gradle 配置:
plugins {
id 'org.springframework.boot' version '2.6.7'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'test'
version = '0.0.1'
sourceCompatibility = '17'
ext {
set('springCloudVersion', "2021.0.2")
queryDslVersion = '5.0.0'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.hibernate.validator:hibernate-validator'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'joda-time:joda-time:2.10.14'
implementation 'org.springframework.boot:spring-boot-starter-hateoas:2.6.7'
implementation 'org.postgresql:postgresql'
implementation 'org.jadira.usertype:usertype.core:7.0.0.CR1'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap:3.1.2'
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'com.google.code.gson:gson:2.9.0'
// QueryDSL
implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}:jpa"
testImplementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
testAnnotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}:jpa"
// Lombok
compileOnly 'org.projectlombok:lombok:1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'
testCompileOnly 'org.projectlombok:lombok:1.18.24'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'
// Swagger
implementation 'org.springdoc:springdoc-openapi-ui:1.6.8'
implementation 'org.liquibase:liquibase-core'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
tasks.named('test') {
useJUnitPlatform()
}
但是 JPA classes 没有生成。我得到异常:
import com.test.domain.QTransaction;
^
symbol: class QTransaction
location: package com.test.domain
你知道实现这个的正确方法是什么吗?
Q
classes 通常由 QueryDSL 和 QueryDSL 注释处理器工具生成。
我对 Gradle 的熟悉不如对 Maven 的熟悉,但是 Gradle 配置对我来说看起来不错。为了以防万一,请参阅 this or
当与 JPA 一起使用时,作为第一步,如您所料,您必须创建一个代表您的 table 的 class,您必须创建一个包含所需信息的 @Entity
.在您的情况下,它看起来类似于此代码:
@Entity
public class Transaction {
@Id
@GeneratedValue
private Long id;
private Long userId;
private String firstName;
private String lastName;
private String email;
private DateTime transactionDate;
// getters and setters
}
请注意,我们将 class 注释为 @Entity
。
编译您的 classes 后,您会看到 Gradle 应该生成了一个 class,名称为 QTransaction
。
除其他信息外,它将生成 QTransaction
class:
public static final
实例
public static final QTransaction transaction = new QTransaction("transaction");
这个 class 是您在构建查询时通常会使用的查询,如您的示例中所示。
This article,虽然 Maven 专注,但可能会有所帮助。