Skip to content

Commit 76b7b8d

Browse files
committed
fix getExample() 2.7.2
1 parent a218dd0 commit 76b7b8d

File tree

10 files changed

+294
-52
lines changed

10 files changed

+294
-52
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>com.codingapi.springboot</groupId>
1414
<artifactId>springboot-parent</artifactId>
15-
<version>2.7.1</version>
15+
<version>2.7.2</version>
1616

1717
<url>https://github.com/codingapi/springboot-framewrok</url>
1818
<name>springboot-parent</name>

springboot-starter-data-fast/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>2.7.1</version>
8+
<version>2.7.2</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
package com.codingapi.springboot.fast.query;
22

33
import com.codingapi.springboot.framework.dto.request.PageRequest;
4+
import javax.persistence.criteria.Order;
5+
import javax.persistence.criteria.Predicate;
46
import org.springframework.core.ResolvableType;
57
import org.springframework.data.domain.Page;
8+
import org.springframework.data.jpa.domain.Specification;
69
import org.springframework.data.jpa.repository.JpaRepository;
710
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
811
import org.springframework.data.repository.NoRepositoryBean;
912

13+
import java.util.List;
14+
1015
@NoRepositoryBean
11-
public interface FastRepository<T,ID> extends JpaRepository<T,ID>, JpaSpecificationExecutor<T> {
16+
public interface FastRepository<T, ID> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {
1217

13-
default Page<T> findAll(PageRequest request){
14-
if(request.hasFilter()){
18+
default Page<T> findAll(PageRequest request) {
19+
if (request.hasFilter()) {
1520
Class<T> clazz = getDomainClass();
16-
return findAll(request.getExample(clazz),request);
21+
QueryRequest queryRequest = new QueryRequest(request, clazz);
22+
return findAll(queryRequest.getExample(), request);
1723
}
18-
return findAll((org.springframework.data.domain.PageRequest)request);
24+
return findAll((org.springframework.data.domain.PageRequest) request);
1925
}
2026

2127

@@ -25,4 +31,20 @@ default Class<T> getDomainClass() {
2531
return (Class<T>) resolvableType.getGeneric(0).resolve();
2632
}
2733

28-
}
34+
35+
default Page<T> findAllByRequest(PageRequest request) {
36+
if (request.hasFilter()) {
37+
Class<T> clazz = getDomainClass();
38+
Specification<T> specification = (root, query, criteriaBuilder) -> {
39+
QueryRequest queryRequest = new QueryRequest(request, clazz);
40+
List<Predicate> predicates = queryRequest.getPredicate(root, criteriaBuilder);
41+
List<Order> orderList = queryRequest.getOrder(root, criteriaBuilder);
42+
return query.where(predicates.toArray(new Predicate[0])).orderBy(orderList).getRestriction();
43+
};
44+
45+
return findAll(specification, request);
46+
}
47+
return findAll((org.springframework.data.domain.PageRequest) request);
48+
}
49+
50+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package com.codingapi.springboot.fast.query;
2+
3+
import com.codingapi.springboot.framework.dto.request.PageRequest;
4+
import javax.persistence.criteria.CriteriaBuilder;
5+
import javax.persistence.criteria.Order;
6+
import javax.persistence.criteria.Predicate;
7+
import javax.persistence.criteria.Root;
8+
import org.springframework.beans.BeanUtils;
9+
import org.springframework.data.domain.Example;
10+
11+
import java.beans.PropertyDescriptor;
12+
import java.util.ArrayList;
13+
import java.util.Date;
14+
import java.util.List;
15+
16+
public class QueryRequest {
17+
18+
private final PageRequest request;
19+
private final Class<?> clazz;
20+
21+
public QueryRequest(PageRequest request, Class<?> clazz) {
22+
this.request = request;
23+
this.clazz = clazz;
24+
}
25+
26+
public <T> Example<T> getExample() {
27+
if (!request.hasFilter()) {
28+
return null;
29+
}
30+
Object entity = null;
31+
try {
32+
entity = clazz.getDeclaredConstructor().newInstance();
33+
} catch (Exception e) {
34+
throw new RuntimeException(e);
35+
}
36+
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(clazz);
37+
for (PropertyDescriptor descriptor : descriptors) {
38+
String name = descriptor.getName();
39+
PageRequest.Filter value = request.getFilters().get(name);
40+
if (value != null) {
41+
try {
42+
descriptor.getWriteMethod().invoke(entity, value.getFilterValue(descriptor.getPropertyType()));
43+
} catch (Exception e) {
44+
}
45+
}
46+
}
47+
return (Example<T>) Example.of(entity);
48+
}
49+
50+
51+
public <T> List<Order> getOrder(Root<T> root, CriteriaBuilder criteriaBuilder) {
52+
List<Order> orderList = new ArrayList<>();
53+
request.getSort().forEach(sort -> {
54+
if (sort.getDirection().isAscending()) {
55+
orderList.add(criteriaBuilder.asc(root.get(sort.getProperty())));
56+
} else {
57+
orderList.add(criteriaBuilder.asc(root.get(sort.getProperty())));
58+
}
59+
});
60+
return orderList;
61+
}
62+
63+
public <T> List<Predicate> getPredicate(Root<T> root, CriteriaBuilder criteriaBuilder) {
64+
List<Predicate> predicates = new ArrayList<>();
65+
for (String key : request.getFilters().keySet()) {
66+
PageRequest.Filter filter = request.getFilters().get(key);
67+
if (filter.isEqual()) {
68+
predicates.add(criteriaBuilder.equal(root.get(key), filter.getValue()[0]));
69+
}
70+
71+
if (filter.isLike()) {
72+
String matchValue = (String) filter.getValue()[0];
73+
predicates.add(criteriaBuilder.like(root.get(key), "%" + matchValue + "%"));
74+
}
75+
76+
if (filter.isBetween()) {
77+
Object value1 = filter.getValue()[0];
78+
Object value2 = filter.getValue()[2];
79+
if (value1 instanceof Integer && value2 instanceof Integer) {
80+
predicates.add(criteriaBuilder.between(root.get(key), (Integer) value1, (Integer) value2));
81+
}
82+
83+
if (value1 instanceof Long && value2 instanceof Long) {
84+
predicates.add(criteriaBuilder.between(root.get(key), (Long) value1, (Long) value2));
85+
}
86+
87+
if (value1 instanceof Date && value2 instanceof Date) {
88+
predicates.add(criteriaBuilder.between(root.get(key), (Date) value1, (Date) value2));
89+
}
90+
}
91+
92+
if (filter.isGreaterThan()) {
93+
Object value = filter.getValue()[0];
94+
if (value instanceof Integer) {
95+
predicates.add(criteriaBuilder.greaterThan(root.get(key), (Integer) value));
96+
}
97+
if (value instanceof Long) {
98+
predicates.add(criteriaBuilder.greaterThan(root.get(key), (Long) value));
99+
}
100+
if (value instanceof Date) {
101+
predicates.add(criteriaBuilder.greaterThan(root.get(key), (Date) value));
102+
}
103+
}
104+
105+
if (filter.isGreaterThanEqual()) {
106+
Object value = filter.getValue()[0];
107+
if (value instanceof Integer) {
108+
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(key), (Integer) value));
109+
}
110+
if (value instanceof Long) {
111+
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(key), (Long) value));
112+
}
113+
if (value instanceof Date) {
114+
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(key), (Date) value));
115+
}
116+
}
117+
118+
if (filter.isLessThan()) {
119+
Object value = filter.getValue()[0];
120+
if (value instanceof Integer) {
121+
predicates.add(criteriaBuilder.lessThan(root.get(key), (Integer) value));
122+
}
123+
if (value instanceof Long) {
124+
predicates.add(criteriaBuilder.lessThan(root.get(key), (Long) value));
125+
}
126+
if (value instanceof Date) {
127+
predicates.add(criteriaBuilder.lessThan(root.get(key), (Date) value));
128+
}
129+
}
130+
131+
if (filter.isLessThanEqual()) {
132+
Object value = filter.getValue()[0];
133+
if (value instanceof Integer) {
134+
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(key), (Integer) value));
135+
}
136+
if (value instanceof Long) {
137+
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(key), (Long) value));
138+
}
139+
if (value instanceof Date) {
140+
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(key), (Date) value));
141+
}
142+
}
143+
}
144+
145+
return predicates;
146+
}
147+
}

springboot-starter-id-generator/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>2.7.1</version>
8+
<version>2.7.2</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-security-jwt/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>2.7.1</version>
9+
<version>2.7.2</version>
1010
</parent>
1111

1212
<artifactId>springboot-starter-security-jwt</artifactId>

springboot-starter/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.codingapi.springboot</groupId>
77
<artifactId>springboot-parent</artifactId>
8-
<version>2.7.1</version>
8+
<version>2.7.2</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

0 commit comments

Comments
 (0)