JPAt整合queryDSL,靈活查詢 (1.基本配置)
阿新 • • 發佈:2019-01-31
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 longserialVersionUID = 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的初步配置