#86 开始实现动态搜索条件组合的功能
This commit is contained in:
@@ -186,7 +186,7 @@
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [2006-2007] [www.springside.org.cn]
|
||||
Copyright [2006-2012] [www.springside.org.cn]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -3,6 +3,6 @@ echo [INFO] Package the war in target dir.
|
||||
|
||||
cd %~dp0
|
||||
cd ..
|
||||
call mvn package -Dmaven.test.skip=true
|
||||
call mvn clean package -Dmaven.test.skip=true
|
||||
cd bin
|
||||
pause
|
||||
@@ -431,7 +431,6 @@
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>${project.basedir}/src/main/java2</sourceDirectory>
|
||||
<plugins>
|
||||
<!-- compiler插件, 设定JDK版本 -->
|
||||
<plugin>
|
||||
|
||||
@@ -2,12 +2,13 @@ package org.springside.examples.quickstart.repository;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springside.examples.quickstart.entity.Task;
|
||||
|
||||
public interface TaskDao extends PagingAndSortingRepository<Task, Long> {
|
||||
public interface TaskDao extends PagingAndSortingRepository<Task, Long>, JpaSpecificationExecutor<Task> {
|
||||
|
||||
Page<Task> findByUserId(Long id, Pageable pageRequest);
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
|
||||
<!-- Spring Data Jpa配置 -->
|
||||
<jpa:repositories base-package="org.springside.examples.quickstart" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>
|
||||
<bean class="org.springside.modules.persistence.ByWebFilterSpecification"/>
|
||||
|
||||
<!-- Jpa 事务配置 -->
|
||||
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
|
||||
|
||||
@@ -2,6 +2,8 @@ package org.springside.examples.quickstart.repository;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -9,14 +11,22 @@ import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springside.examples.quickstart.entity.Task;
|
||||
import org.springside.modules.persistence.ByWebFilterSpecification;
|
||||
import org.springside.modules.persistence.SearchFilter;
|
||||
import org.springside.modules.persistence.SearchFilter.Operator;
|
||||
import org.springside.modules.test.spring.SpringTransactionalTestCase;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
@ContextConfiguration(locations = { "/applicationContext.xml" })
|
||||
public class TaskDaoTest extends SpringTransactionalTestCase {
|
||||
|
||||
@Autowired
|
||||
private TaskDao taskDao;
|
||||
|
||||
@Autowired
|
||||
private ByWebFilterSpecification spec;
|
||||
|
||||
@Test
|
||||
public void findTasksByUserId() throws Exception {
|
||||
Page<Task> tasks = taskDao.findByUserId(2L, new PageRequest(0, 100, Direction.ASC, "id"));
|
||||
@@ -26,4 +36,17 @@ public class TaskDaoTest extends SpringTransactionalTestCase {
|
||||
tasks = taskDao.findByUserId(99999L, new PageRequest(0, 100, Direction.ASC, "id"));
|
||||
assertEquals(0, tasks.getContent().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fineTasksByFilter() {
|
||||
SearchFilter filter = new SearchFilter("title", "Study PlayFramework 2.0", Operator.EQ);
|
||||
|
||||
List<Task> tasks = taskDao.findAll(spec.byWebFilter(Lists.newArrayList(filter), Task.class));
|
||||
assertEquals(1, tasks.size());
|
||||
|
||||
filter = new SearchFilter("description", "playframework", Operator.LIKE);
|
||||
tasks = taskDao.findAll(spec.byWebFilter(Lists.newArrayList(filter), Task.class));
|
||||
assertEquals(1, tasks.size());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,6 @@ echo [INFO] Package the war in target dir.
|
||||
|
||||
cd %~dp0
|
||||
cd ..
|
||||
call mvn package -Dmaven.test.skip=true
|
||||
call mvn clean package -Dmaven.test.skip=true
|
||||
cd bin
|
||||
pause
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.springside.modules.persistence;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Expression;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
import javax.persistence.metamodel.SingularAttribute;
|
||||
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springside.modules.persistence.SearchFilter.Operator;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class ByWebFilterSpecification {
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
public <T> Specification<T> byWebFilter(final List<SearchFilter> filters, final Class<T> clazz) {
|
||||
return new Specification<T>() {
|
||||
@Override
|
||||
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
||||
List<Predicate> predicates = Lists.newArrayList();
|
||||
for (SearchFilter filter : filters) {
|
||||
SingularAttribute<T, ?> attr = entityManager.getMetamodel().entity(clazz)
|
||||
.getDeclaredSingularAttribute(filter.fieldName);
|
||||
if (filter.operator.equals(Operator.EQ)) {
|
||||
predicates.add(builder.equal(root.get(attr), filter.value));
|
||||
}
|
||||
if (filter.operator.equals(Operator.LIKE)) {
|
||||
predicates.add(builder.like((Expression<String>) root.get(attr), "%" + filter.value + "%"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (predicates.size() > 0) {
|
||||
return builder.and(predicates.toArray(new Predicate[predicates.size()]));
|
||||
}
|
||||
|
||||
return builder.conjunction();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.springside.modules.persistence;
|
||||
|
||||
public class SearchFilter {
|
||||
|
||||
public enum Operator {
|
||||
EQ, LIKE
|
||||
}
|
||||
|
||||
public String fieldName;
|
||||
public Object value;
|
||||
public Operator operator;
|
||||
|
||||
public SearchFilter(String fieldName, Object value, Operator operator) {
|
||||
super();
|
||||
this.fieldName = fieldName;
|
||||
this.value = value;
|
||||
this.operator = operator;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user