What is the best solution for filtering results in

2019-01-20 14:24发布

I have a case here for a products Table, That needs to be filtered by user input like Category, color, size, price range and many more.

I'm using Spring Data JPA and happy with its derived query from method name and when I'm forced to I just use the @query option for more complex queries like joins and ...

But for the Filter method that I need, I'm afraid I have to write something like this

public interface ProductRepository extends JpaRepository<Product, Long> {
   //..... other methods

Page<Product> findByCategoriesContainingAndSalepriceBetween(List<Category> categories, Float minprice, Float maxprice, PageRequest pagerequest);

Page<Product> findByCategoriesContaining(List<Category> categories, PageRequest pagerequest);

Page<Product> findByCategoriesContainingAndSizeIn(List<Category> categories,Int[] sizes, PageRequest pagerequest);

Page<Product> findByCategoriesContainingAndSizeInAndSalepriceBetween(List<Category> categories,Float minprice, Float maxprice, PageRequest pagerequest);

}

it seems adding some other fields will force me to write so many different combinations

so I've looked at QueryDsl and Specification but they seem so much of extra code, could you put me on the right path??

1条回答
甜甜的少女心
2楼-- · 2019-01-20 14:55

I would disagree that QueryDSL results in a lot of extra code. Here's some test code I have for QueryDSL:

Repository Definition:

public interface UserRepository extends PagingAndSortingRepository<User, Long>, QueryDslPredicateExecutor<User> {

    public User findOne(Predicate predicate);

    public List<User> findAll(Predicate predicate);
}

Test Code based on various attributes and combinations thereof:

 @Test
    public void testFindByEmailAddress() {
        User user = repository.findOne(QUser.user.emailAddress.eq("jack@hamilton.net"));
        Assert.assertNotNull(user);
        Assert.assertEquals("Jack", user.getForename());
        Assert.assertEquals("Hamilton", user.getSurname());
    }

    @Test
    public void testFindByGender() {
        List<User> users = repository.findAll(QUser.user.gender.eq(Gender.M));
        Assert.assertEquals(4, users.size());

        users = repository.findAll(QUser.user.gender.eq(Gender.F));
        Assert.assertEquals(2, users.size());
    }

    @Test
    public void testFindByCity() {

        List<User> users = repository.findAll(QUser.user.address.town.eq("Edinburgh"));
        Assert.assertEquals(2, users.size());

        users = repository.findAll(QUser.user.address.town.eq("Stirling"));
        Assert.assertEquals(1, users.size());
    }

    @Test
    public void testFindByGenderAndCity() {
        List<User> users = repository.findAll(QUser.user.address.town.eq("Glasgow").and(QUser.user.gender.eq(Gender.M)));
        Assert.assertEquals(2, users.size());

        users = repository.findAll(QUser.user.address.town.eq("Glasgow").and(QUser.user.gender.eq(Gender.F)));
        Assert.assertEquals(1, users.size());
    }
查看更多
登录 后发表回答