Java带有任意AND子句的动态Spring Data JPA存储库查询

Java带有任意AND子句的动态Spring Data JPA存储库查询,第1张

Java带有任意AND子句的动态Spring Data JPA存储库查询

请注意,使用新的主要版本的QueryDSL(4.x)和querydsl-jpa可能需要进行一些更改。

在我们的项目之一,我们用

QueryDSL
QueryDslPredicateExecutor<T>

  public Predicate createPredicate(DataEntity dataEntity) {    QDataEntity qDataEntity = QDataEntity.dataEntity;    BooleanBuilder booleanBuilder = new BooleanBuilder();    if (!StringUtils.isEmpty(dataEntity.getCnsiConsumerNo())) {      booleanBuilder        .or(qDataEntity.cnsiConsumerNo.contains(dataEntity.getCnsiConsumerNo()));    }    if (!StringUtils.isEmpty(dataEntity.getCnsiMeterNo())) {      booleanBuilder.or(qDataEntity.cnsiMeterNo.contains(dataEntity.getCnsiMeterNo()));    }    return booleanBuilder.getValue();  }

我们可以在存储库中使用它:

@Repositorypublic interface DataEntityRepository  extends DaoRepository<DataEntity, Long> {哪里DaoRepository是@NoRepositoryBeanpublic interface DaoRepository<T, K extends Serializable>  extends JpaRepository<T, K>,  QueryDslPredicateExecutor<T> {}

因为这样,您可以使用存储库谓词方法。

Iterable<DataEntity> results = dataEntityRepository.findAll(dataEntityPredicateCreator.createPredicate(dataEntity));

要获取QClasses,您需要在pom.xml中指定QueryDSL APT Maven插件

  <build>    <plugins>      <plugin>        <groupId>com.mysema.maven</groupId>        <artifactId>maven-apt-plugin</artifactId>        <version>1.0.4</version>        <executions>          <execution> <phase>generate-sources</phase> <goals>   <goal>process</goal> </goals> <configuration>   <outputDirectory>target/generated-sources</outputDirectory>   <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor> </configuration>          </execution>        </executions>      </plugin>

依赖项是

    <!-- querydsl -->    <dependency>        <groupId>com.mysema.querydsl</groupId>        <artifactId>querydsl-core</artifactId>        <version>${querydsl.version}</version>    </dependency>    <dependency>        <groupId>com.mysema.querydsl</groupId>        <artifactId>querydsl-apt</artifactId>        <version>${querydsl.version}</version>    </dependency>    <dependency>        <groupId>com.mysema.querydsl</groupId>        <artifactId>querydsl-jpa</artifactId>        <version>${querydsl.version}</version>    </dependency>

或对于Gradle:

sourceSets {    generated}sourceSets.generated.java.srcDirs = ['src/main/generated']configurations {    querydslapt}dependencies {    // other deps ....    compile "com.mysema.querydsl:querydsl-jpa:3.6.3"    compile "com.mysema.querydsl:querydsl-apt:3.6.3:jpa"}task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') {    source = sourceSets.main.java    classpath = configurations.compile + configurations.querydslapt    options.compilerArgs = [ "-proc:only", "-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor"    ]    destinationDir = sourceSets.generated.java.srcDirs.iterator().next()}compileJava {    dependsOn generateQueryDSL    source generateQueryDSL.destinationDir}compileGeneratedJava {    dependsOn generateQueryDSL    classpath += sourceSets.main.runtimeClasspath}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5108771.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存