A extension of Querydsl for searching pojo via custom query like lucene query. e.g. b.name=hello AND date<2017-12-29T08:29:55.907Z
基于querydesl和spring-data的自定义搜索, 快速搭建搜索语法
Quickstart
Add below dependencies to your pom
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
<dependency>
<groupId>io.github.jianzhichun</groupId>
<artifactId>qsearch-querydsl</artifactId>
<version>0.0.1</version>
</dependency>
And plugin for generating source
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>maven-apt-plugin</artifactId>
<version>1.0.4</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>
Add interface IQSearch<T, QT extends EntityPathBase> to your repository assigned to CrudRepository
@Repository
public interface ARepository extends JpaRepository<A, Integer>, IQSearch<A, QA> {};
Add QSearchHandlerMethodArgumentResolver to your List<HandlerMethodArgumentResolver> in spring context
public class QsearchQuerydslJpaSampleApplication extends WebMvcConfigurerAdapter {
@Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new QSearchHandlerMethodArgumentResolver());
}
Add restful function with parameter QSearchPredicator with annotation @QSearchParam(T.class)
@GetMapping
public Page<A> qsearch(@ApiIgnore @QSearchParam(A.class) QSearchPredicator predicator, Pageable pageable){
return repository.findAll(predicator.predicate(), pageable);
}
Then you can query pojo via qsearch pattern
Pattern:
private final static Pattern PATTERN = Pattern
.compile("\\s*(?<logicalOperator>AND|OR|,)?\\s+(?<key>[\\w|.]+?)(?<operation>~|=|<|>|<=|>=|!=)(?<value>[^=|^>|^<|^!|^~|^,|^\\s]+)");
- logicalOperator: OR means or query, AND | , means and query
- key: Name of pojo's filed, supported dot path
- operation:
- ~: containsIgnoreCase in String query
- =: equals
- < | > | <= | >= | !=: compare
- value: Values to be querying
e.g. http://localhost:8080/api/A?q=b.name~tri, date>=2017-12-29T08:29:55.907Z
Examples
qsearch-querydsl example projects
License
This project is licensed under Apache License 2.0.