美文网首页
spring-data的Dsl查询

spring-data的Dsl查询

作者: 仙境999 | 来源:发表于2018-06-23 23:22 被阅读0次
  1. 第一步, 生成查询对象
    在一个jpa项目中加入如下依赖和配置:
<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <scope>provided</scope>
</dependency>
<build>
    <plugins>
        <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>target/generated-sources/java</outputDirectory>
                        <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

如果报找不到Inject类还需要加入:

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>

然后去命令行运行mvn clean compile (IDEA的compile好像不行), 插件会扫描@Entity对象并且按名称生成查询对象, 例如@Entity class Country会生成class QCountry类, 代码在指定的outputDirectory里

将生成的查询对象移动到项目里

repository继承QueryDslPredicateExecutor<T>

public interface CountryRepository extends JpaRepository<Country, String>, QueryDslPredicateExecutor<Country> {
}

下边是一个条件查询的例子, 使用了Pageable

private Page<Country> searchByContinent(String continent, Pageable pageable) {
    BooleanBuilder booleanBuilder = new BooleanBuilder();
    if (hasLength(continent)) {
        booleanBuilder.and(QCountry.country.continent.eq(continent));
    }

    return countryRepository.findAll(booleanBuilder.getValue(), pageable);
}

相关文章

  • spring-data的Dsl查询

    第一步, 生成查询对象在一个jpa项目中加入如下依赖和配置: 如果报找不到Inject类还需要加入: 然后去命令行...

  • DSL查询文档

    1.DSL查询文档 elasticsearch的查询依然是基于JSON风格的DSL来实现的。 1.1.DSL查询分...

  • ElasticSearch查询DLS

    查询和过滤的区别 ES提供基于JSON的完整DSL来定义查询,查询DSL包括两种子句:叶查询子句:在特定的字段上查...

  • ES搜索(二)query查询

    ES可以使用URI或DSL进行查询 URI 由于DSL可以提供更多功能,以及可视化更好,一般都使用DSL进行查询 ...

  • swagger

    spring-data的三种查询 原生态自带查询 常用的方法名查询 常用方法名总结: 自定义JPQL查询

  • spring-data进阶和swagger的使用

    spring-data的三种查询 原生态自带查询 常用的方法名查询 常用方法名总结: 自定义JPQL查询

  • 29.Kibana基础-2

    29.1 按字段过滤 29.2 DSL查询 还可以编辑一个DSL查询语句,用于过滤筛选,例如: 29.3 查看文档...

  • Elasticsearch(入门篇)——Query DSL与查询

    前言 Query DSL结构化查询 Query DSL是一个Java开源框架用于构建类型安全的SQL查询语句。采用...

  • solr教程四(query和queryFilter)

    查询DSL和过滤DSL: filter:范围过滤 query:更倾向准确查找 query filter在性能上...

  • JPA查询

    jpa常用的查询方式 jpa官方文档 https://docs.spring.io/spring-data/jpa...

网友评论

      本文标题:spring-data的Dsl查询

      本文链接:https://www.haomeiwen.com/subject/htadyftx.html