Skip to content

Commit

Permalink
add 完善service查询方法封装
Browse files Browse the repository at this point in the history
  • Loading branch information
enilu committed Aug 1, 2019
1 parent 5b1fdf3 commit 4cb7f09
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 16 deletions.
6 changes: 3 additions & 3 deletions guns-admin/src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## 开发环境配置
spring.datasource.url=jdbc:mysql://localhost:3306/guns-lite?useUnicode=true&characterEncoding=UTF8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/gunslite?useUnicode=true&characterEncoding=UTF8
spring.datasource.username=gunslite
spring.datasource.password=gunslite123


##实际开发和生产环境中注释掉下面配置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuild
case IN:
predicates.add(expression.in(filter.value));
break;
case ISNULL:
predicates.add(expression.isNull());
break;
case ISNOTNULL:
predicates.add(expression.isNotNull());
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,14 @@ public void addFilter(SearchFilter filter){
filters = Lists.newArrayList();
}
filters.add(filter);

}
public void addFilter(String fieldName, SearchFilter.Operator operator, Object value){
if(!StringUtils.isNullOrEmpty(value)){
addFilter(SearchFilter.build(fieldName,operator,value));
}
}
public void addFilter(String fieldName, SearchFilter.Operator operator){
addFilter(SearchFilter.build(fieldName,operator));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public class SearchFilter {
public enum Operator {
EQ, LIKE, GT, LT, GTE, LTE,IN
EQ, LIKE, GT, LT, GTE, LTE,IN,ISNULL,ISNOTNULL
}

public String fieldName;
Expand All @@ -22,6 +22,13 @@ public enum Operator {
public static SearchFilter build(String fieldName, Operator operator, Object value){
return new SearchFilter(fieldName,operator,value);
}
public static SearchFilter build(String fieldName, Operator operator){
return new SearchFilter(fieldName,operator);
}
public SearchFilter(String fieldName, Operator operator) {
this.fieldName = fieldName;
this.operator = operator;
}
public SearchFilter(String fieldName, Operator operator, Object value) {
this.fieldName = fieldName;
this.value = value;
Expand Down
26 changes: 17 additions & 9 deletions guns-service/src/main/java/cn/enilu/guns/service/BaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
*
Expand Down Expand Up @@ -48,8 +47,7 @@ public T insert(T record) {

@Override
public T get(ID id) {
Optional<T> optional = dao.findById(id);
return optional.isPresent()?optional.get():null;
return dao.getOne(id);
}

@Override
Expand All @@ -68,7 +66,7 @@ public Page<T> queryPage(Page<T> page) {
if(page.isOpenSort()) {
pageable = new PageRequest(page.getCurrent()-1, page.getSize(), page.isAsc() ? Sort.Direction.ASC : Sort.Direction.DESC, page.getOrderByField());
}else{
pageable = new PageRequest(page.getCurrent()-1,page.getSize(),Sort.Direction.DESC,"id");
pageable = new PageRequest(page.getCurrent()-1,page.getSize(), Sort.Direction.DESC,"id");
}
Specification<T> specification = DynamicSpecifications.bySearchFilter(page.getFilters(),dao.getDataClass());
org.springframework.data.domain.Page<T> pageResult = dao.findAll(specification,pageable);
Expand All @@ -79,16 +77,26 @@ public Page<T> queryPage(Page<T> page) {

@Override
public List<T> queryAll(List<SearchFilter> filters) {
Specification<T> specification = DynamicSpecifications.bySearchFilter(filters,dao.getDataClass());
return dao.findAll(specification);
return queryAll(filters,null);
}

@Override
public List<T> queryAll(SearchFilter filter) {
if(filter!=null) {
return queryAll(Lists.newArrayList(filter));
return queryAll(filter,null);
}

@Override
public List<T> queryAll(List<SearchFilter> filters, Sort sort) {
Specification<T> specification = DynamicSpecifications.bySearchFilter(filters,dao.getDataClass());
if(sort==null){
return dao.findAll(specification);
}
return queryAll();
return dao.findAll(specification,sort);
}

@Override
public List<T> queryAll(SearchFilter filter, Sort sort) {
return queryAll(Lists.newArrayList(filter),sort);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.enilu.guns.bean.vo.query.SearchFilter;
import cn.enilu.guns.bean.vo.query.Page;
import org.springframework.data.domain.Sort;

import java.util.List;

Expand Down Expand Up @@ -39,16 +40,32 @@ public interface SelectService <T, ID> {
Page<T> queryPage(Page<T> page);

/**
* 根据条件里欸报查询列表数据
* 根据多个条件查询列表数据
* @param filters
* @return
*/
List<T> queryAll(List<SearchFilter> filters);

/**
* 根据的那个条件查询列表数据
* 根据多个条件查询列表数据,并排序
* @param filters
* @param sort
* @return
*/
List<T> queryAll(List<SearchFilter> filters, Sort sort);

/**
* 根据的单个条件查询列表数据
* @param filter
* @return
*/
List<T> queryAll(SearchFilter filter);

/**
* 根据的单个条件查询列表数据
* @param filter
* @param sort
* @return
*/
List<T> queryAll(SearchFilter filter,Sort sort);
}

0 comments on commit 4cb7f09

Please sign in to comment.