JPA查询过于复杂,使用queryDSL配合JPA能够更灵活的使用查询功能。

1.pom配置如下:

2.使用maven的apt-maven-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>target/generated-sources/annotations</outputDirectory>
                    <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

JPAt集成queryDSL,灵活查询 (1.基本配置)

maven编译后,自动生成查询模版

public class QStudent extends EntityPathBase<Student> {

    private static final long serialVersionUID = 2136978263L;

    public static final QStudent student = new QStudent("student");

    public final StringPath age = createString("age");

    public final NumberPath<Integer> host = createNumber("host", Integer.class);

    public final NumberPath<Long> id = createNumber("id", Long.class);

    public final StringPath name = createString("name");

    public final StringPath sex = createString("sex");

    public final DateTimePath<java.util.Date> startDate = createDateTime("startDate", java.util.Date.class);

    public QStudent(String variable) {
        super(Student.class, forVariable(variable));
    }

    public QStudent(Path<? extends Student> path) {
        super(path.getType(), path.getMetadata());
    }

    public QStudent(PathMetadata metadata) {
        super(Student.class, metadata);
    }
JPAt集成queryDSL,灵活查询 (1.基本配置)

3.JPA持久层集成QueryDslPredicteExecutor借口

import com.five.fiveeducation.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;


public interface EducationDao extends JpaRepository<Student,Long>,QueryDslPredicateExecutor<Student> {
}

完成JPA集成queryDSL的初步配置


相关文章:

  • 2022-12-23
  • 2021-08-05
  • 2021-07-06
  • 2022-12-23
  • 2021-05-03
  • 2022-12-23
  • 2021-06-25
猜你喜欢
  • 2021-11-10
  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2022-02-19
  • 2021-12-21
  • 2021-11-11
相关资源
相似解决方案