Skip to content

Commit 2907fb5

Browse files
committed
增加Like查询
1 parent afc5722 commit 2907fb5

14 files changed

+363
-64
lines changed

src/main/java/org/durcframework/expression/ExpressionBuilder.java

+14-60
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
import java.util.List;
77

88
import org.durcframework.expression.annotation.AnnoExprStore;
9+
import org.durcframework.expression.annotation.LikeDoubleField;
10+
import org.durcframework.expression.annotation.LikeLeftField;
11+
import org.durcframework.expression.annotation.LikeRightField;
912
import org.durcframework.expression.annotation.ListField;
1013
import org.durcframework.expression.annotation.ValueField;
1114
import org.durcframework.expression.getter.ExpressionGetter;
12-
import org.durcframework.expression.subexpression.ListExpression;
13-
import org.durcframework.expression.subexpression.ValueExpression;
14-
import org.springframework.util.StringUtils;
15+
import org.durcframework.expression.getter.impl.LikeDoubleExpressionGetter;
16+
import org.durcframework.expression.getter.impl.LikeLeftExpressionGetter;
17+
import org.durcframework.expression.getter.impl.LikeRightExpressionGetter;
18+
import org.durcframework.expression.getter.impl.ListExpressionGetter;
19+
import org.durcframework.expression.getter.impl.ValueExpressionGetter;
1520

1621
/**
1722
* 从bean中获取Expression
@@ -22,64 +27,13 @@ public class ExpressionBuilder {
2227

2328
private static final String PREFIX_GET = "get";
2429

25-
// =============内部类=============
26-
// 构建list查询条件
27-
private static class ListExpressionGetter implements ExpressionGetter {
28-
29-
@Override
30-
public Expression buildExpression(Annotation annotation, String column,
31-
Object value) {
32-
if (value == null) {
33-
return null;
34-
}
35-
ListField listValueField = (ListField) annotation;
36-
String joint = listValueField.joint();
37-
String equal = listValueField.equal();
38-
String field = listValueField.column();
39-
if (StringUtils.hasText(field)) {
40-
column = field;
41-
}
42-
if (value.getClass().isArray()) {
43-
return new ListExpression(joint, column, equal,
44-
(Object[]) value);
45-
}
46-
if (value instanceof List) {
47-
return new ListExpression(joint, column, equal, (List<?>) value);
48-
}
49-
return null;
50-
}
51-
}
52-
53-
// 构建单值查询条件工厂
54-
private static class ValueExpressionGetter implements ExpressionGetter {
55-
56-
@Override
57-
public Expression buildExpression(Annotation annotation, String column,
58-
Object value) {
59-
if (value == null) {
60-
return null;
61-
}
62-
if (value instanceof String) {
63-
if (!StringUtils.hasText((String) value)) {
64-
return null;
65-
}
66-
}
67-
ValueField valueField = (ValueField) annotation;
68-
String fieldColumn = valueField.column();
69-
if (StringUtils.hasText(fieldColumn)) {
70-
column = fieldColumn;
71-
}
72-
return new ValueExpression(valueField.joint(), column,
73-
valueField.equal(), value);
74-
}
75-
76-
}
77-
30+
// init
7831
static {
79-
AnnoExprStore.addExpressionGetter(ListField.class,
80-
new ListExpressionGetter());
81-
AnnoExprStore.addExpressionGetter(ValueField.class,
82-
new ValueExpressionGetter());
32+
AnnoExprStore.addExpressionGetter(ListField.class,new ListExpressionGetter());
33+
AnnoExprStore.addExpressionGetter(ValueField.class,new ValueExpressionGetter());
34+
AnnoExprStore.addExpressionGetter(LikeLeftField.class,new LikeLeftExpressionGetter());
35+
AnnoExprStore.addExpressionGetter(LikeRightField.class,new LikeRightExpressionGetter());
36+
AnnoExprStore.addExpressionGetter(LikeDoubleField.class,new LikeDoubleExpressionGetter());
8337
}
8438

8539
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.durcframework.expression.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Documented
11+
@Target({ ElementType.CONSTRUCTOR, ElementType.METHOD })
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Inherited
14+
public @interface LikeDoubleField {
15+
// 连接符
16+
String joint() default "AND";
17+
// 列名
18+
String column() default "";
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.durcframework.expression.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Documented
11+
@Target({ ElementType.CONSTRUCTOR, ElementType.METHOD })
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Inherited
14+
public @interface LikeLeftField {
15+
// 连接符
16+
String joint() default "AND";
17+
// 列名
18+
String column() default "";
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.durcframework.expression.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Documented
11+
@Target({ ElementType.CONSTRUCTOR, ElementType.METHOD })
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Inherited
14+
public @interface LikeRightField {
15+
// 连接符
16+
String joint() default "AND";
17+
// 列名
18+
String column() default "";
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.durcframework.expression.getter.impl;
2+
3+
import java.lang.annotation.Annotation;
4+
5+
import org.durcframework.expression.Expression;
6+
import org.durcframework.expression.annotation.LikeDoubleField;
7+
import org.durcframework.expression.getter.ExpressionGetter;
8+
import org.durcframework.expression.subexpression.LikeDoubleExpression;
9+
import org.springframework.util.StringUtils;
10+
11+
public class LikeDoubleExpressionGetter implements ExpressionGetter{
12+
13+
@Override
14+
public Expression buildExpression(Annotation annotation, String column,
15+
Object value) {
16+
if (value == null) {
17+
return null;
18+
}
19+
if (value instanceof String) {
20+
if (!StringUtils.hasText((String) value)) {
21+
return null;
22+
}
23+
}
24+
LikeDoubleField valueField = (LikeDoubleField) annotation;
25+
String fieldColumn = valueField.column();
26+
if (StringUtils.hasText(fieldColumn)) {
27+
column = fieldColumn;
28+
}
29+
return new LikeDoubleExpression(valueField.joint(),column, value);
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.durcframework.expression.getter.impl;
2+
3+
import java.lang.annotation.Annotation;
4+
5+
import org.durcframework.expression.Expression;
6+
import org.durcframework.expression.annotation.LikeLeftField;
7+
import org.durcframework.expression.getter.ExpressionGetter;
8+
import org.durcframework.expression.subexpression.LikeLeftExpression;
9+
import org.springframework.util.StringUtils;
10+
11+
public class LikeLeftExpressionGetter implements ExpressionGetter{
12+
13+
@Override
14+
public Expression buildExpression(Annotation annotation, String column,
15+
Object value) {
16+
if (value == null) {
17+
return null;
18+
}
19+
if (value instanceof String) {
20+
if (!StringUtils.hasText((String) value)) {
21+
return null;
22+
}
23+
}
24+
LikeLeftField valueField = (LikeLeftField) annotation;
25+
String fieldColumn = valueField.column();
26+
if (StringUtils.hasText(fieldColumn)) {
27+
column = fieldColumn;
28+
}
29+
return new LikeLeftExpression(valueField.joint(),column, value);
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.durcframework.expression.getter.impl;
2+
3+
import java.lang.annotation.Annotation;
4+
5+
import org.durcframework.expression.Expression;
6+
import org.durcframework.expression.annotation.LikeRightField;
7+
import org.durcframework.expression.getter.ExpressionGetter;
8+
import org.durcframework.expression.subexpression.LikeRightExpression;
9+
import org.springframework.util.StringUtils;
10+
11+
public class LikeRightExpressionGetter implements ExpressionGetter{
12+
13+
@Override
14+
public Expression buildExpression(Annotation annotation, String column,
15+
Object value) {
16+
if (value == null) {
17+
return null;
18+
}
19+
if (value instanceof String) {
20+
if (!StringUtils.hasText((String) value)) {
21+
return null;
22+
}
23+
}
24+
LikeRightField valueField = (LikeRightField) annotation;
25+
String fieldColumn = valueField.column();
26+
if (StringUtils.hasText(fieldColumn)) {
27+
column = fieldColumn;
28+
}
29+
return new LikeRightExpression(valueField.joint(),column, value);
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.durcframework.expression.getter.impl;
2+
3+
import java.lang.annotation.Annotation;
4+
import java.util.List;
5+
6+
import org.durcframework.expression.Expression;
7+
import org.durcframework.expression.annotation.ListField;
8+
import org.durcframework.expression.getter.ExpressionGetter;
9+
import org.durcframework.expression.subexpression.ListExpression;
10+
import org.springframework.util.StringUtils;
11+
12+
//构建list查询条件
13+
public class ListExpressionGetter implements ExpressionGetter {
14+
15+
@Override
16+
public Expression buildExpression(Annotation annotation, String column,
17+
Object value) {
18+
if (value == null) {
19+
return null;
20+
}
21+
ListField listValueField = (ListField) annotation;
22+
String joint = listValueField.joint();
23+
String equal = listValueField.equal();
24+
String field = listValueField.column();
25+
if (StringUtils.hasText(field)) {
26+
column = field;
27+
}
28+
if (value.getClass().isArray()) {
29+
return new ListExpression(joint, column, equal,
30+
(Object[]) value);
31+
}
32+
if (value instanceof List) {
33+
return new ListExpression(joint, column, equal, (List<?>) value);
34+
}
35+
return null;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.durcframework.expression.getter.impl;
2+
3+
import java.lang.annotation.Annotation;
4+
5+
import org.durcframework.expression.Expression;
6+
import org.durcframework.expression.annotation.ValueField;
7+
import org.durcframework.expression.getter.ExpressionGetter;
8+
import org.durcframework.expression.subexpression.ValueExpression;
9+
import org.springframework.util.StringUtils;
10+
11+
// 构建单值查询条件工厂
12+
public class ValueExpressionGetter implements ExpressionGetter {
13+
14+
@Override
15+
public Expression buildExpression(Annotation annotation, String column,
16+
Object value) {
17+
if (value == null) {
18+
return null;
19+
}
20+
if (value instanceof String) {
21+
if (!StringUtils.hasText((String) value)) {
22+
return null;
23+
}
24+
}
25+
ValueField valueField = (ValueField) annotation;
26+
String fieldColumn = valueField.column();
27+
if (StringUtils.hasText(fieldColumn)) {
28+
column = fieldColumn;
29+
}
30+
return new ValueExpression(valueField.joint(), column,
31+
valueField.equal(), value);
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.durcframework.expression.subexpression;
2+
3+
import org.durcframework.expression.SqlContent;
4+
5+
public abstract class AbstractLikeExpression extends ValueExpression {
6+
7+
public AbstractLikeExpression(String column, Object value) {
8+
super(column, value);
9+
}
10+
11+
public AbstractLikeExpression(String joint, String column, Object value) {
12+
super(joint, column, SqlContent.LIKE, value);
13+
}
14+
15+
@Override
16+
public String getEqual() {
17+
return SqlContent.LIKE;
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.durcframework.expression.subexpression;
2+
3+
4+
/**
5+
* Like条件查询,两边模糊匹配,即'%aaa%'
6+
* @author hc.tang
7+
*
8+
*/
9+
public class LikeDoubleExpression extends AbstractLikeExpression {
10+
11+
/**
12+
* Like条件查询,两边模糊匹配,即'%aaa%'
13+
* @param column 数据库字段名
14+
* @param value 查询的值
15+
*/
16+
public LikeDoubleExpression(String column, Object value) {
17+
super(column, value);
18+
}
19+
20+
/**
21+
* Like条件查询,两边模糊匹配,即'%aaa%'
22+
* @param joint 连接符,如AND或OR
23+
* @param column 数据库字段名
24+
* @param value 查询的值
25+
*/
26+
public LikeDoubleExpression(String joint, String column, Object value) {
27+
super(joint, column, value);
28+
}
29+
30+
@Override
31+
public String getValue() {
32+
return "%" + super.getValue() + "%";
33+
}
34+
35+
}

0 commit comments

Comments
 (0)