add spring querydsl test
This commit is contained in:
parent
5bfbea1fff
commit
9adc48b9ea
|
@ -6,6 +6,7 @@
|
|||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="target/generated-sources/java"/>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
<wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
|
||||
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
|
||||
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
|
||||
<wb-resource deploy-path="/WEB-INF/classes" source-path="/target/generated-sources/java"/>
|
||||
<property name="context-root" value="spring-security-rest-full"/>
|
||||
<property name="java-output-path" value="/spring-security-rest-full/target/classes"/>
|
||||
</wb-module>
|
||||
|
|
|
@ -77,6 +77,26 @@
|
|||
<version>${org.springframework.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Querydsl -->
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysema.querydsl</groupId>
|
||||
<artifactId>querydsl-core</artifactId>
|
||||
<version>3.6.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysema.querydsl</groupId>
|
||||
<artifactId>querydsl-apt</artifactId>
|
||||
<version>3.6.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysema.querydsl</groupId>
|
||||
<artifactId>querydsl-jpa</artifactId>
|
||||
<version>3.6.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- web -->
|
||||
|
||||
<dependency>
|
||||
|
@ -307,6 +327,25 @@
|
|||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!-- Querydsl -->
|
||||
|
||||
<plugin>
|
||||
<groupId>com.mysema.maven</groupId>
|
||||
<artifactId>apt-maven-plugin</artifactId>
|
||||
<version>1.1.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>process</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>target/generated-sources/java</outputDirectory>
|
||||
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<String, Object> params) {
|
||||
if (params.size() == 0)
|
||||
return null;
|
||||
|
||||
final List<BooleanExpression> predicates = new ArrayList<BooleanExpression>();
|
||||
String key, value;
|
||||
|
||||
for (final Map.Entry<String, Object> 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;
|
||||
|
||||
}
|
||||
}
|
|
@ -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<MyUser, Long>, QueryDslPredicateExecutor<MyUser> {
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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<MyUser> search(final Map<String, Object> 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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<MyUser> findAllByQuerydsl(@RequestParam(value = "search", required = false) final String search) {
|
||||
final Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<MyUser> result = repository.findAll(lastNameIsLike("doe"));
|
||||
assertThat(result, containsInAnyOrder(userJohn, userTom));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
||||
final Iterable<MyUser> 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<MyUser> 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<MyUser> result = repository.findAll(lastNameIsLike("Adam").and(firstNameIsLike("Fox")));
|
||||
assertThat(result, emptyIterable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() {
|
||||
final Iterable<MyUser> result = repository.findAll(firstNameIsLike("jo"));
|
||||
|
||||
assertThat(result, contains(userJohn));
|
||||
assertThat(result, not(contains(userTom)));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue