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>
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);
}
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的初步配置