diff --git a/spring-security-rest-full/.classpath b/spring-security-rest-full/.classpath
index a72442544c..7049a8b952 100644
--- a/spring-security-rest-full/.classpath
+++ b/spring-security-rest-full/.classpath
@@ -6,6 +6,7 @@
+
diff --git a/spring-security-rest-full/.settings/org.eclipse.wst.common.component b/spring-security-rest-full/.settings/org.eclipse.wst.common.component
index 174b04e48d..3b942952f2 100644
--- a/spring-security-rest-full/.settings/org.eclipse.wst.common.component
+++ b/spring-security-rest-full/.settings/org.eclipse.wst.common.component
@@ -4,6 +4,7 @@
+
diff --git a/spring-security-rest-full/pom.xml b/spring-security-rest-full/pom.xml
index 67756a1d47..c9a785ee54 100644
--- a/spring-security-rest-full/pom.xml
+++ b/spring-security-rest-full/pom.xml
@@ -77,6 +77,26 @@
${org.springframework.version}
+
+
+
+ com.mysema.querydsl
+ querydsl-core
+ 3.6.0
+
+
+
+ com.mysema.querydsl
+ querydsl-apt
+ 3.6.0
+
+
+
+ com.mysema.querydsl
+ querydsl-jpa
+ 3.6.0
+
+
@@ -307,6 +327,25 @@
+
+
+
+ com.mysema.maven
+ apt-maven-plugin
+ 1.1.3
+
+
+
+ process
+
+
+ target/generated-sources/java
+ com.mysema.query.apt.jpa.JPAAnnotationProcessor
+
+
+
+
+
diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPrdicates.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPrdicates.java
new file mode 100644
index 0000000000..6c3ed32149
--- /dev/null
+++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPrdicates.java
@@ -0,0 +1,23 @@
+package org.baeldung.persistence.dao;
+
+import org.baeldung.persistence.model.QMyUser;
+
+import com.mysema.query.types.expr.BooleanExpression;
+
+public class MyUserPrdicates {
+
+ public static BooleanExpression firstNameIsLike(final String term) {
+ final QMyUser user = QMyUser.myUser;
+ return user.firstName.containsIgnoreCase(term);
+ }
+
+ public static BooleanExpression lastNameIsLike(final String term) {
+ final QMyUser user = QMyUser.myUser;
+ return user.lastName.containsIgnoreCase(term);
+ }
+
+ public static BooleanExpression ageIsGreaterThan(final int minAge) {
+ final QMyUser user = QMyUser.myUser;
+ return user.age.goe(minAge);
+ }
+}
diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicatesBuilder.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicatesBuilder.java
new file mode 100644
index 0000000000..ef59011e45
--- /dev/null
+++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicatesBuilder.java
@@ -0,0 +1,43 @@
+package org.baeldung.persistence.dao;
+
+import static org.baeldung.persistence.dao.MyUserPrdicates.ageIsGreaterThan;
+import static org.baeldung.persistence.dao.MyUserPrdicates.firstNameIsLike;
+import static org.baeldung.persistence.dao.MyUserPrdicates.lastNameIsLike;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import com.mysema.query.types.expr.BooleanExpression;
+
+public class MyUserPredicatesBuilder {
+ public static BooleanExpression buildUserPredicates(final Map params) {
+ if (params.size() == 0)
+ return null;
+
+ final List predicates = new ArrayList();
+ String key, value;
+
+ for (final Map.Entry param : params.entrySet()) {
+ key = param.getKey();
+ value = param.getValue().toString();
+ if (key.equalsIgnoreCase("age")) {
+ predicates.add(ageIsGreaterThan(Integer.parseInt(value)));
+ } else if (key.equalsIgnoreCase("firstName")) {
+ predicates.add(firstNameIsLike(value));
+ } else if (key.equalsIgnoreCase("lastName")) {
+ predicates.add(lastNameIsLike(value));
+ }
+ }
+
+ if (predicates.size() == 0)
+ return null;
+
+ BooleanExpression result = predicates.get(0);
+ for (int i = 1; i < predicates.size(); i++) {
+ result = result.and(predicates.get(i));
+ }
+ return result;
+
+ }
+}
diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserRepository.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserRepository.java
new file mode 100644
index 0000000000..795f2c3c6c
--- /dev/null
+++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserRepository.java
@@ -0,0 +1,9 @@
+package org.baeldung.persistence.dao;
+
+import org.baeldung.persistence.model.MyUser;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.querydsl.QueryDslPredicateExecutor;
+
+public interface MyUserRepository extends JpaRepository, QueryDslPredicateExecutor {
+
+}
diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/model/MyUser.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/model/MyUser.java
new file mode 100644
index 0000000000..66fa90da9c
--- /dev/null
+++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/model/MyUser.java
@@ -0,0 +1,97 @@
+package org.baeldung.persistence.model;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+// used for Querydsl test
+
+@Entity
+public class MyUser {
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private Long id;
+
+ private String firstName;
+
+ private String lastName;
+
+ private String email;
+
+ private int age;
+
+ public MyUser() {
+ super();
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(final Long id) {
+ this.id = id;
+ }
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(final String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(final String lastName) {
+ this.lastName = lastName;
+ }
+
+ public String getEmail() {
+ return email;
+ }
+
+ public void setEmail(final String username) {
+ email = username;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(final int age) {
+ this.age = age;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((email == null) ? 0 : email.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final MyUser user = (MyUser) obj;
+ if (!email.equals(user.email))
+ return false;
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder builder = new StringBuilder();
+ builder.append("MyUser [firstName=").append(firstName).append("]").append("[lastName=").append(lastName).append("]").append("[username").append(email).append("]");
+ return builder.toString();
+ }
+
+}
diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/MyUserService.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/MyUserService.java
new file mode 100644
index 0000000000..e92c97e42c
--- /dev/null
+++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/MyUserService.java
@@ -0,0 +1,36 @@
+package org.baeldung.persistence.service.impl;
+
+import java.util.Map;
+
+import org.baeldung.persistence.dao.MyUserPredicatesBuilder;
+import org.baeldung.persistence.dao.MyUserRepository;
+import org.baeldung.persistence.model.MyUser;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import com.mysema.query.types.expr.BooleanExpression;
+
+@Service
+@Transactional
+public class MyUserService {
+
+ @Autowired
+ private MyUserRepository repository;
+
+ public MyUserService() {
+ super();
+ }
+
+ public Iterable search(final Map params) {
+ final BooleanExpression predicate = MyUserPredicatesBuilder.buildUserPredicates(params);
+ if (predicate == null)
+ return repository.findAll();
+ return repository.findAll(predicate);
+ }
+
+ public void save(final MyUser user) {
+ repository.save(user);
+ }
+}
+
diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/UserController.java b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/UserController.java
index be1bc25b76..674e8419ef 100644
--- a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/UserController.java
+++ b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/UserController.java
@@ -6,7 +6,9 @@ import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import org.baeldung.persistence.model.MyUser;
import org.baeldung.persistence.model.User;
+import org.baeldung.persistence.service.impl.MyUserService;
import org.baeldung.persistence.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@@ -21,6 +23,9 @@ public class UserController {
@Autowired
private UserService service;
+ @Autowired
+ private MyUserService myService;
+
public UserController() {
super();
}
@@ -55,6 +60,21 @@ public class UserController {
return service.searchBySpecification(params);
}
+ @RequestMapping(method = RequestMethod.GET, value = "/myusers")
+ @ResponseBody
+ public Iterable findAllByQuerydsl(@RequestParam(value = "search", required = false) final String search) {
+ final Map params = new HashMap();
+
+ if (search != null) {
+ final Pattern pattern = Pattern.compile("(\\w+?):(\\w+?),");
+ final Matcher matcher = pattern.matcher(search + ",");
+ while (matcher.find()) {
+ params.put(matcher.group(1), matcher.group(2));
+ }
+ }
+ return myService.search(params);
+ }
+
@RequestMapping(method = RequestMethod.GET, value = "/users/new")
@ResponseBody
public long addUser(@RequestParam("first") final String first, @RequestParam("last") final String last, @RequestParam("age") final int age) {
@@ -66,4 +86,16 @@ public class UserController {
service.saveUser(user);
return user.getId();
}
+
+ @RequestMapping(method = RequestMethod.GET, value = "/myusers/new")
+ @ResponseBody
+ public long addMyUser(@RequestParam("first") final String first, @RequestParam("last") final String last, @RequestParam("age") final int age) {
+ final MyUser user = new MyUser();
+ user.setFirstName(first);
+ user.setLastName(last);
+ user.setEmail("john@doe.com");
+ user.setAge(age);
+ myService.save(user);
+ return user.getId();
+ }
}
diff --git a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java
new file mode 100644
index 0000000000..fd0d83432d
--- /dev/null
+++ b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java
@@ -0,0 +1,90 @@
+package org.baeldung.persistence.query;
+
+import static org.baeldung.persistence.dao.MyUserPrdicates.ageIsGreaterThan;
+import static org.baeldung.persistence.dao.MyUserPrdicates.firstNameIsLike;
+import static org.baeldung.persistence.dao.MyUserPrdicates.lastNameIsLike;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.collection.IsEmptyIterable.emptyIterable;
+import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
+import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
+import static org.hamcrest.core.IsNot.not;
+
+import org.baeldung.persistence.dao.MyUserRepository;
+import org.baeldung.persistence.model.MyUser;
+import org.baeldung.spring.PersistenceConfig;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.support.AnnotationConfigContextLoader;
+import org.springframework.test.context.transaction.TransactionConfiguration;
+import org.springframework.transaction.annotation.Transactional;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
+@Transactional
+@TransactionConfiguration
+public class JPAQuerydslTest {
+
+ @Autowired
+ private MyUserRepository repository;
+
+ private MyUser userJohn;
+
+ private MyUser userTom;
+
+ @Before
+ public void init() {
+ userJohn = new MyUser();
+ userJohn.setFirstName("John");
+ userJohn.setLastName("Doe");
+ userJohn.setEmail("john@doe.com");
+ userJohn.setAge(22);
+ repository.save(userJohn);
+
+ userTom = new MyUser();
+ userTom.setFirstName("Tom");
+ userTom.setLastName("Doe");
+ userTom.setEmail("tom@doe.com");
+ userTom.setAge(26);
+ repository.save(userTom);
+ }
+
+ @Test
+ public void givenLast_whenGettingListOfUsers_thenCorrect() {
+ final Iterable result = repository.findAll(lastNameIsLike("doe"));
+ assertThat(result, containsInAnyOrder(userJohn, userTom));
+ }
+
+ @Test
+ public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
+ final Iterable result = repository.findAll(lastNameIsLike("doe").and(firstNameIsLike("john")));
+
+ assertThat(result, contains(userJohn));
+ assertThat(result, not(contains(userTom)));
+ }
+
+ @Test
+ public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() {
+ final Iterable result = repository.findAll(lastNameIsLike("doe").and(ageIsGreaterThan(25)));
+
+ assertThat(result, contains(userTom));
+ assertThat(result, not(contains(userJohn)));
+ }
+
+ @Test
+ public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() {
+ final Iterable result = repository.findAll(lastNameIsLike("Adam").and(firstNameIsLike("Fox")));
+ assertThat(result, emptyIterable());
+ }
+
+ @Test
+ public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() {
+ final Iterable result = repository.findAll(firstNameIsLike("jo"));
+
+ assertThat(result, contains(userJohn));
+ assertThat(result, not(contains(userTom)));
+ }
+}