Merge pull request #122 from Doha2012/master

modify spring query
This commit is contained in:
Eugen 2015-01-28 18:10:02 +02:00
commit 2b1243e6aa
5 changed files with 76 additions and 85 deletions

View File

@ -8,7 +8,21 @@ import org.baeldung.web.util.SearchCriteria;
import com.mysema.query.types.expr.BooleanExpression; import com.mysema.query.types.expr.BooleanExpression;
public class MyUserPredicatesBuilder { public class MyUserPredicatesBuilder {
public static BooleanExpression buildUserPredicates(final List<SearchCriteria> params) { private List<SearchCriteria> params;
public MyUserPredicatesBuilder() {
params = new ArrayList<SearchCriteria>();
}
public MyUserPredicatesBuilder with(final String key, final String operation, final Object value) {
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
final List<SearchCriteria> newParams = new ArrayList<SearchCriteria>(params);
newParams.add(new SearchCriteria(key, operation, value));
builder.params = newParams;
return builder;
}
public BooleanExpression build() {
if (params.size() == 0) if (params.size() == 0)
return null; return null;

View File

@ -10,7 +10,21 @@ import org.springframework.data.jpa.domain.Specifications;
public class UserSpecificationsBuilder { public class UserSpecificationsBuilder {
public static Specification<User> buildUserSpecs(final List<SearchCriteria> params) { private List<SearchCriteria> params;
public UserSpecificationsBuilder() {
params = new ArrayList<SearchCriteria>();
}
public UserSpecificationsBuilder with(final String key, final String operation, final Object value) {
final UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
final List<SearchCriteria> newParams = new ArrayList<SearchCriteria>(params);
newParams.add(new SearchCriteria(key, operation, value));
builder.params = newParams;
return builder;
}
public Specification<User> build() {
if (params.size() == 0) if (params.size() == 0)
return null; return null;

View File

@ -1,42 +0,0 @@
package org.baeldung.persistence.service.impl;
import java.util.List;
import org.baeldung.persistence.dao.IUserDAO;
import org.baeldung.persistence.dao.UserRepository;
import org.baeldung.persistence.dao.UserSpecificationsBuilder;
import org.baeldung.persistence.model.User;
import org.baeldung.web.util.SearchCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class UserService {
@Autowired
private IUserDAO dao;
@Autowired
private UserRepository repository;
public UserService() {
super();
}
public void saveUser(final User user) {
dao.save(user);
}
public List<User> searchUser(final List<SearchCriteria> params) {
return dao.searchUser(params);
}
public List<User> searchBySpecification(final List<SearchCriteria> params) {
final Specification<User> spec = UserSpecificationsBuilder.buildUserSpecs(params);
if (spec == null)
return repository.findAll();
return repository.findAll(spec);
}
}

View File

@ -5,26 +5,35 @@ import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.baeldung.persistence.dao.IUserDAO;
import org.baeldung.persistence.dao.MyUserPredicatesBuilder;
import org.baeldung.persistence.dao.MyUserRepository;
import org.baeldung.persistence.dao.UserRepository;
import org.baeldung.persistence.dao.UserSpecificationsBuilder;
import org.baeldung.persistence.model.MyUser; import org.baeldung.persistence.model.MyUser;
import org.baeldung.persistence.model.User; import org.baeldung.persistence.model.User;
import org.baeldung.persistence.service.impl.MyUserService;
import org.baeldung.persistence.service.impl.UserService;
import org.baeldung.web.util.SearchCriteria; import org.baeldung.web.util.SearchCriteria;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import com.mysema.query.types.expr.BooleanExpression;
@Controller @Controller
public class UserController { public class UserController {
@Autowired @Autowired
private UserService service; private IUserDAO service;
@Autowired @Autowired
private MyUserService myService; private UserRepository dao;
@Autowired
private MyUserRepository mydao;
public UserController() { public UserController() {
super(); super();
@ -47,32 +56,39 @@ public class UserController {
@RequestMapping(method = RequestMethod.GET, value = "/users/spec") @RequestMapping(method = RequestMethod.GET, value = "/users/spec")
@ResponseBody @ResponseBody
public List<User> findAllBySpecification(@RequestParam(value = "search", required = false) final String search) { public List<User> findAllBySpecification(@RequestParam(value = "search") final String search) {
final List<SearchCriteria> params = new ArrayList<SearchCriteria>(); UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
if (search != null) { if (search != null) {
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),"); final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
final Matcher matcher = pattern.matcher(search + ","); final Matcher matcher = pattern.matcher(search + ",");
while (matcher.find()) { while (matcher.find()) {
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3))); builder = builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
} }
} }
return service.searchBySpecification(params); final Specification<User> spec = builder.build();
if (spec == null)
return dao.findAll();
else
return dao.findAll(spec);
} }
@RequestMapping(method = RequestMethod.GET, value = "/myusers") @RequestMapping(method = RequestMethod.GET, value = "/myusers")
@ResponseBody @ResponseBody
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search", required = false) final String search) { public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") final String search) {
final List<SearchCriteria> params = new ArrayList<SearchCriteria>(); MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
if (search != null) { if (search != null) {
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),"); final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
final Matcher matcher = pattern.matcher(search + ","); final Matcher matcher = pattern.matcher(search + ",");
while (matcher.find()) { while (matcher.find()) {
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3))); builder = builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
} }
} }
return myService.search(params); final BooleanExpression exp = builder.build();
if (exp == null)
return mydao.findAll();
else
return mydao.findAll(exp);
} }
@RequestMapping(method = RequestMethod.GET, value = "/users/new") @RequestMapping(method = RequestMethod.GET, value = "/users/new")
@ -83,7 +99,7 @@ public class UserController {
user.setLastName(last); user.setLastName(last);
user.setEmail("john@doe.com"); user.setEmail("john@doe.com");
user.setAge(age); user.setAge(age);
service.saveUser(user); dao.save(user);
return user.getId(); return user.getId();
} }
@ -95,7 +111,7 @@ public class UserController {
user.setLastName(last); user.setLastName(last);
user.setEmail("john@doe.com"); user.setEmail("john@doe.com");
user.setAge(age); user.setAge(age);
myService.save(user); mydao.save(user);
return user.getId(); return user.getId();
} }
} }

View File

@ -6,13 +6,10 @@ import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInA
import static org.hamcrest.collection.IsIterableContainingInOrder.contains; import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.hamcrest.core.IsNot.not; import static org.hamcrest.core.IsNot.not;
import java.util.ArrayList; import org.baeldung.persistence.dao.MyUserPredicatesBuilder;
import java.util.List; import org.baeldung.persistence.dao.MyUserRepository;
import org.baeldung.persistence.model.MyUser; import org.baeldung.persistence.model.MyUser;
import org.baeldung.persistence.service.impl.MyUserService;
import org.baeldung.spring.PersistenceConfig; import org.baeldung.spring.PersistenceConfig;
import org.baeldung.web.util.SearchCriteria;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -29,7 +26,7 @@ import org.springframework.transaction.annotation.Transactional;
public class JPAQuerydslTest { public class JPAQuerydslTest {
@Autowired @Autowired
private MyUserService service; private MyUserRepository repo;
private MyUser userJohn; private MyUser userJohn;
@ -42,32 +39,29 @@ public class JPAQuerydslTest {
userJohn.setLastName("Doe"); userJohn.setLastName("Doe");
userJohn.setEmail("john@doe.com"); userJohn.setEmail("john@doe.com");
userJohn.setAge(22); userJohn.setAge(22);
service.save(userJohn); repo.save(userJohn);
userTom = new MyUser(); userTom = new MyUser();
userTom.setFirstName("Tom"); userTom.setFirstName("Tom");
userTom.setLastName("Doe"); userTom.setLastName("Doe");
userTom.setEmail("tom@doe.com"); userTom.setEmail("tom@doe.com");
userTom.setAge(26); userTom.setAge(26);
service.save(userTom); repo.save(userTom);
} }
@Test @Test
public void givenLast_whenGettingListOfUsers_thenCorrect() { public void givenLast_whenGettingListOfUsers_thenCorrect() {
final List<SearchCriteria> params = new ArrayList<SearchCriteria>(); final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("lastName", ":", "Doe");
params.add(new SearchCriteria("lastName", ":", "Doe"));
final Iterable<MyUser> results = service.search(params); final Iterable<MyUser> results = repo.findAll(builder.build());
assertThat(results, containsInAnyOrder(userJohn, userTom)); assertThat(results, containsInAnyOrder(userJohn, userTom));
} }
@Test @Test
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() { public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
final List<SearchCriteria> params = new ArrayList<SearchCriteria>(); final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "John").with("lastName", ":", "Doe");
params.add(new SearchCriteria("firstName", ":", "John"));
params.add(new SearchCriteria("lastName", ":", "Doe"));
final Iterable<MyUser> results = service.search(params); final Iterable<MyUser> results = repo.findAll(builder.build());
assertThat(results, contains(userJohn)); assertThat(results, contains(userJohn));
assertThat(results, not(contains(userTom))); assertThat(results, not(contains(userTom)));
@ -75,11 +69,9 @@ public class JPAQuerydslTest {
@Test @Test
public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() { public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() {
final List<SearchCriteria> params = new ArrayList<SearchCriteria>(); final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("lastName", ":", "Doe").with("age", ">", "25");
params.add(new SearchCriteria("lastName", ":", "Doe"));
params.add(new SearchCriteria("age", ">", "25"));
final Iterable<MyUser> results = service.search(params); final Iterable<MyUser> results = repo.findAll(builder.build());
assertThat(results, contains(userTom)); assertThat(results, contains(userTom));
assertThat(results, not(contains(userJohn))); assertThat(results, not(contains(userJohn)));
@ -87,20 +79,17 @@ public class JPAQuerydslTest {
@Test @Test
public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() { public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() {
final List<SearchCriteria> params = new ArrayList<SearchCriteria>(); final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "Adam").with("lastName", ":", "Fox");
params.add(new SearchCriteria("firstName", ":", "Adam"));
params.add(new SearchCriteria("lastName", ":", "Fox"));
final Iterable<MyUser> results = service.search(params); final Iterable<MyUser> results = repo.findAll(builder.build());
assertThat(results, emptyIterable()); assertThat(results, emptyIterable());
} }
@Test @Test
public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() { public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() {
final List<SearchCriteria> params = new ArrayList<SearchCriteria>(); final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "jo");
params.add(new SearchCriteria("firstName", ":", "jo"));
final Iterable<MyUser> results = service.search(params); final Iterable<MyUser> results = repo.findAll(builder.build());
assertThat(results, contains(userJohn)); assertThat(results, contains(userJohn));
assertThat(results, not(contains(userTom))); assertThat(results, not(contains(userTom)));