-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
412 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/com/example/demo/persistence/dao/MyUserPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.example.demo.persistence.dao; | ||
|
||
import com.example.demo.persistence.model.MyUser; | ||
import com.example.demo.web.util.SearchCriteria; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.core.types.dsl.NumberPath; | ||
import com.querydsl.core.types.dsl.PathBuilder; | ||
import com.querydsl.core.types.dsl.StringPath; | ||
|
||
public class MyUserPredicate { | ||
|
||
private SearchCriteria criteria; | ||
|
||
public MyUserPredicate(final SearchCriteria criteria) { | ||
this.criteria = criteria; | ||
} | ||
|
||
public BooleanExpression getPredicate() { | ||
final PathBuilder<MyUser> entityPath = new PathBuilder<>(MyUser.class, "myUser"); | ||
|
||
if (isNumeric(criteria.getValue().toString())) { | ||
final NumberPath<Integer> path = entityPath.getNumber(criteria.getKey(), Integer.class); | ||
final int value = Integer.parseInt(criteria.getValue().toString()); | ||
switch (criteria.getOperation()) { | ||
case ":": | ||
return path.eq(value); | ||
case ">": | ||
return path.goe(value); | ||
case "<": | ||
return path.loe(value); | ||
} | ||
} else { | ||
final StringPath path = entityPath.getString(criteria.getKey()); | ||
if (criteria.getOperation().equalsIgnoreCase(":")) { | ||
return path.containsIgnoreCase(criteria.getValue().toString()); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public SearchCriteria getCriteria() { | ||
return criteria; | ||
} | ||
|
||
public void setCriteria(final SearchCriteria criteria) { | ||
this.criteria = criteria; | ||
} | ||
|
||
public static boolean isNumeric(final String str) { | ||
try { | ||
Integer.parseInt(str); | ||
} catch (final NumberFormatException e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/example/demo/persistence/dao/MyUserPredicateBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.example.demo.persistence.dao; | ||
|
||
import com.example.demo.web.util.SearchCriteria; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public final class MyUserPredicateBuilder { | ||
private final List<SearchCriteria> params; | ||
|
||
public MyUserPredicateBuilder() { | ||
params = new ArrayList<>(); | ||
} | ||
|
||
public MyUserPredicateBuilder with(final String key, final String operation, final Object value) { | ||
params.add(new SearchCriteria(key, operation, value)); | ||
return this; | ||
} | ||
|
||
public BooleanExpression build() { | ||
if (params.size() == 0) { | ||
return null; | ||
} | ||
|
||
final List<BooleanExpression> predicates = new ArrayList<>(); | ||
MyUserPredicate predicate; | ||
for (final SearchCriteria param : params) { | ||
predicate = new MyUserPredicate(param); | ||
final BooleanExpression exp = predicate.getPredicate(); | ||
if (exp != null) { | ||
predicates.add(exp); | ||
} | ||
} | ||
|
||
BooleanExpression result = predicates.get(0); | ||
for (int i = 1; i < predicates.size(); i++) { | ||
result = result.and(predicates.get(i)); | ||
} | ||
return result; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/example/demo/persistence/dao/repository/MyUserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.example.demo.persistence.dao.repository; | ||
|
||
import com.example.demo.persistence.model.MyUser; | ||
import com.example.demo.persistence.model.QMyUser; | ||
import com.querydsl.core.types.dsl.StringExpression; | ||
import com.querydsl.core.types.dsl.StringPath; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.querydsl.QueryDslPredicateExecutor; | ||
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer; | ||
import org.springframework.data.querydsl.binding.QuerydslBindings; | ||
import com.querydsl.core.types.dsl.StringExpression; | ||
|
||
import com.querydsl.core.types.dsl.StringPath; | ||
import org.springframework.data.querydsl.binding.SingleValueBinding; | ||
|
||
public interface MyUserRepository extends JpaRepository<MyUser, Long>, | ||
QueryDslPredicateExecutor<MyUser>, QuerydslBinderCustomizer<QMyUser> { | ||
|
||
@Override | ||
default void customize(final QuerydslBindings bindings, final QMyUser root) { | ||
bindings.bind(String.class) | ||
.first((SingleValueBinding<StringPath, String>) StringExpression::containsIgnoreCase); | ||
bindings.excluding(root.email); | ||
} | ||
} | ||
|
31 changes: 31 additions & 0 deletions
31
src/main/java/com/example/demo/persistence/model/MyUser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.example.demo.persistence.model; | ||
|
||
import lombok.*; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
|
||
@Entity | ||
@Setter | ||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@ToString | ||
@EqualsAndHashCode | ||
public class MyUser { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private int id; | ||
|
||
private String firstName; | ||
|
||
private String lastName; | ||
|
||
private String email; | ||
|
||
private int age; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.