Lets you search text among all fields pretty.
Add dependency to your pom.xml
.
<dependency>
<groupId>com.github.scfj</groupId>
<artifactId>jpava</artifactId>
<version>2.0.0</version>
</dependency>
Add a method(s) accepting specification object to your repository inherited from JpaRepository.
@Repository
interface PostRepository extends JpaRepository<Post, Long> {
List<Post> findAll(Specification<Post> spec);
// OR
Page<Post> findAll(Specification<Post> spec, Pageable pageable);
}
Import single method into your controller/service which uses repository.
import static com.github.scfj.jpava.TextSpecifications.forClass;
// OR
import static com.github.scfj.jpava.TextSpecifications.inAnyColumnOf;
Now you can perform complex search operation with ease!
@Controller
class PostController {
/*
* Configuration is very simple
*/
Specification<Post> posts =
forClass(Post.class) // can be forClass, inAnyColumnOf (= forClass) or inAllColumnsOf
.except("id", "createdAt") // skip these fields when searching database
.ignoreCase(); // can be ignoreCase (default) or matchCase
public List<Post> findExamples() {
/*
* It will find all posts that has substring "hello"
* in any of post's fields (title, preview, content etc)
*/
return postRepository.findAll(posts.withText("hello"));
}
}
To deploy run
mvn clean deploy -P ossrh
See demo here (version <= 1.3): scfj/jpava-demo.